A Java SDK is available for the Kibo Unified Commerce Platform, allowing the user to interface with UCP APIs within their Java environment. This SDK exposes the complete functionality of the Unified Commerce REST APIs and can be used to authenticate with Kibo and send requests for the user to interact with their tenant once they have created an application.
This guide details the steps of installing the Kibo Java SDK, authenticating a project with Kibo credentials, and making a call to the Kibo APIs (using Inventory APIs as an example).
The Java SDK can be accessed here.
Prerequisites
Before using the Java SDK, the tool Maven must be installed on the PATH.
If Maven is not yet installed, follow the steps at the following link: Maven Installation Instructions
https://www.tutorialspoint.com/maven/maven_environment_setup.htm
Set Up Dependencies
All dependencies must be included in the POM file. Include the Auth artifact, with the latest SNAPSHOT version.
<dependency> <groupId>com.kibocommerce</groupId> <artifactId>auth</artifactId> <version>1.0.1-SNAPSHOT</version> </dependency>
Any other SDKs should also be included. Available ones are listed here. The example below shows how to identify a dependency for the Inventory SDK.
<dependency> <groupId>com.kibocommerce</groupId> <artifactId>inventory-sdk</artifactId> <version>1.0.1-SNAPSHOT</version> </dependency>
Ensure that all dependencies are downloaded successfully. The IDE may have an option to click a “download” button to download all of the Maven dependencies listed in the POM. Otherwise, run the following Maven command in the terminal: mvn clean install
Set Up Authentication
There are three steps to setting up the authentication client.
- Create a new instance of the auth client.
- Set the base path.
- The auth endpoint format is: {BASE_URL}/platform/applications/authtickets/oauth
- The SDK method will take care of part of the path after the {BASE_URL}
- For example, if the full endpoint in the production sandbox is https://home.mozu.com/api/platform/applications/authtickets/oauth then the base path would be https://home.mozu.com/api
- Set the read timeout. It is recommended to set this to a time >= 15000 milliseconds (or 15 seconds). It is also recommended to store any configs in a properties file on the application.
com.kibocommerce.sdk.auth.ApiClient authApiClient = new com.kibocommerce.sdk.auth.ApiClient(); authApiClient.setBasePath(“https://home.mozu.com/api”); authApiClient.setReadTimeout(15000);
Create an Instance of the API Class
Create a new object of the AppAuthTicketsApi class with the API client created in the last step as the sole argument, as shown below.
AppAuthTicketsApi authApi = new AppAuthTicketsApi(authApiClient);
Get Access Token
Create an instance of MozuAppDevContractsOauthAuthRequest and set the following request properties:
- ClientID
- ClientSecret
- Grant Type - this should be “client_credentials”
Using the API Object that was created, call the method oauthAuthenticateApp() with the first 2 arguments set to “null” and the third one with the MozuAppDevContractsOauthAuthRequest object from the previous step.
MozuAppDevContractsOauthAuthRequest mozuAppDevContractsOauthAuthRequest = new MozuAppDevContractsOauthAuthRequest(); mozuAppDevContractsOauthAuthRequest.clientId(“some_client_id”); mozuAppDevContractsOauthAuthRequest.clientSecret(“some_client_secret”); mozuAppDevContractsOauthAuthRequest.setGrantType("client_credentials"); MozuAppDevContractsOAuthAccessTokenResponse response = appAuthTicketsApi.oauthAuthenticateApp(null, null, mozuAppDevContractsOauthAuthRequest);
The returned object will be of type MozuAppDevContractsOAuthAccessTokenResponse. This returned object will provide an access token and an expiration (measured in minutes). This token will be used for any subsequent API requests made using the Kibo SDK, so it must be saved for future use.
Reuse this token until it expires, trying to avoid making unnecessary calls to the auth service to get new tokens during that time. If the token is close to expiring, request a new one by following the method in this guide that was used to get the initial token.
Authenticate the App
After a token has been obtained, the app must be authenticated. Start by creating a new ApiClient for the API. This example will use the Inventory API.
- Create an instance of the Inventory ApiClient. Note that the Inventory API client package is com.kibocommerce.sdk.inventory instead of com.kibocommerce.sdk.auth as for the Auth client.
- Set the Inventory base path.
- Set the Read Timeout. This value should be >= 15 seconds (15000 milliseconds).
- Set the Authorization Header. Using the addDefaultHeader() method, set the first argument to “Authorization” and the second one to “Bearer ” + the Access_Token.
com.kibocommerce.sdk.inventory.ApiClient inventoryApiClient = new com.kibocommerce.sdk.inventory.ApiClient(); inventoryApiClient.setBasePath(“https://t12345.sandbox.mozu.com/api/commerce/inventory”); inventoryApiClient.setReadTimeout(15000); inventoryApiClient.addDefaultHeader("Authorization", "Bearer " + authAccessToken.getAccessToken());
Use the App
Now that the API Client is authenticated, use it for any of the APIs that need to be accessed for the given service. For example, get a new instance of the InventoryControllerAPI class by passing in the Inventory API Client such as in the below example. This instance will then allow any of the API calls that it supports to be made.
InventoryControllerApi inventoryControllerAPI = new InventoryControllerApi(inventoryApiClient)
Create new API Clients for any other services that may be needed. For a simple Spring example of how to create API Clients and API Objects as Spring Beans, see the following resource: https://github.com/KiboSoftware/sdkdemo/blob/master/src/main/java/com/kibocommerce/sdkdemo/config/ApiClientConfig.java
For an example on how to use an API like the Inventory API, see: https://github.com/KiboSoftware/sdkdemo/blob/master/src/main/java/com/kibocommerce/sdkdemo/SdkdemoApplication.java