Java · Track 1 · Tutorial 1 of 3
Set up your Java project
Get a Java project ready for the BrizoConsol API: check Java and Gradle, create the project every tutorial in this series builds on, and keep your credential where your code can read it but nobody else can.
1Check Java and Gradle
You need Java 17 or later, and Gradle 8 or later to build and run the scripts. The scripts use Java's own HttpClient, and one library, Jackson, to read and write JSON; Gradle fetches it for you.
java -version
gradle --versionCheck
Java says 17 or later, and Gradle prints its version. If either isn't found, install it first.
2Create the project
Make a folder called brizo-tutorial with a folder src/main/java inside it. Every script in this series goes in src/main/java, next to one shared class. In brizo-tutorial itself, create settings.gradle:
settings.gradle
rootProject.name = 'brizo-tutorial'and build.gradle, which brings in Jackson and lets you name the script to run:
build.gradle
plugins {
id 'application'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'com.fasterxml.jackson.core:jackson-databind:2.18.2'
}
application {
// The script to run, for example: gradle -q run -Pmain=Companies
mainClass = providers.gradleProperty('main').getOrElse('Setup')
}3Keep your settings out of the code
Your client secret is a password: keep it out of any file you might share. Set the four settings as environment variables in the terminal you'll run the scripts from. In bash or zsh:
export BRIZO_BASE_URL=https://sg.brizoconsol.com/api/v1
export BRIZO_CLIENT_ID=your-client-id
export BRIZO_CLIENT_SECRET=your-client-secret
export BRIZO_COMPANY_ID=149In PowerShell:
$env:BRIZO_BASE_URL = "https://sg.brizoconsol.com/api/v1"
$env:BRIZO_CLIENT_ID = "your-client-id"
$env:BRIZO_CLIENT_SECRET = "your-client-secret"
$env:BRIZO_COMPANY_ID = "149"Use your own region's address, the client ID and secret from Getting started, and your company's ID. You'll find the ID in tutorial 3; until then leave 149.
4Add the shared client
Create Brizo.java in src/main/java with the Brizo class and the part that reads the settings. The next two tutorials add signing in and sending requests inside the same class.
Brizo.java
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
public final class Brizo {
static final Duration TIMEOUT = Duration.ofSeconds(60);
public static final ObjectMapper JSON = new ObjectMapper();
public static String setting(String name) {
String value = System.getenv(name);
if (value == null || value.isEmpty()) {
System.err.println("Set the environment variable " + name + " first.");
System.exit(1);
}
return value;
}
public static final String BASE_URL = setting("BRIZO_BASE_URL").replaceAll("/+$", ""); // e.g. https://sg.brizoconsol.com/api/v1
static final String CLIENT_ID = setting("BRIZO_CLIENT_ID");
static final String CLIENT_SECRET = setting("BRIZO_CLIENT_SECRET");
public static final String COMPANY_ID = setting("BRIZO_COMPANY_ID");
// An amount as 1,234.56, whether the answer holds it as a number or as text.
public static String money(JsonNode amount) {
return String.format(Locale.US, "%,.2f", Double.parseDouble(amount.asText()));
}
}Brizo.java reads it from the environment.5Run the check
Create Setup.java:
Setup.java
// Setup.java - checks that Java, Jackson and your settings are ready.
public class Setup {
public static void main(String[] args) {
int feature = Runtime.version().feature();
System.out.println("Java " + System.getProperty("java.version") + (feature >= 17 ? " - ready" : " - needs 17 or later"));
System.out.println("Jackson: " + com.fasterxml.jackson.databind.cfg.PackageVersion.VERSION);
System.out.println("Base URL: " + Brizo.BASE_URL);
System.out.println("Company: " + Brizo.COMPANY_ID);
}
}gradle -q run -Pmain=SetupCheck
Java 21.0.12.1 - ready
Jackson: 2.16.1
Base URL: https://sg.brizoconsol.com/api/v1
Company: 149Your Java and Jackson versions and your address will be your own. If a setting is missing, the script names it and stops.
Want more detail? See the documentation