Add this dependency to your project’s POM:
<dependency>
<groupId>com.decisionbrain</groupId>
<artifactId>optimserver-client-feign</artifactId>
<version>3.2.1-RC1</version>
<scope>compile</scope>
</dependency>
Add this dependency to your project’s build file:
compile "com.decisionbrain:optimserver-client-feign:3.2.1-RC1"
compile "org.keycloak:keycloak-authz-client:4.6.0.Final"
Please follow the installation instruction and execute the following Java code:
import com.decisionbrain.optimserver.client.java.feign.*;
import com.decisionbrain.optimserver.client.java.feign.auth.*;
import com.decisionbrain.optimserver.client.java.feign.model.*;
import com.decisionbrain.optimserver.client.java.feign.api.BucketApi;
import java.io.File;
import java.util.*;
import org.apache.http.impl.client.HttpClients;
import org.keycloak.authorization.client.AuthzClient;
import org.keycloak.authorization.client.Configuration;
public class BucketApiExample {
private static final String API_URL = "https://OPTIMSERVER/";
private static final String KEYCLOAK_URL = "https://OPTIMSERVER_AUTHENTICATION/auth";
private static final String KEYCLOAK_REALM = "decisionbrain";
private static final String KEYCLOAK_CLIENT = "optimserver";
private static final String KEYCLOAK_USER = "optimserver";
private static final String KEYCLOAK_PASSWORD = "optimserver";
public static void main(String[] args) {
ApiClient apiClient = new ApiClient()
.setBasePath(API_URL);
apiClient.addAuthorization("oauth", getRequestInterceptor());
AccessTokenResponse accessToken = getAccessToken();
apiClient.setAccessToken(accessToken.getToken(), accessToken.getExpiresIn());
BucketApi apiInstance = apiClient.buildClient( BucketApi );
BucketCreation bucketCreation = new BucketCreation(); // BucketCreation |
try {
BucketDefinition result = apiInstance.createBucket(bucketCreation);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling BucketApi#createBucket");
e.printStackTrace();
}
}
public static AccessTokenResponse getAccessToken() {
final Configuration configuration = new Configuration(KEYCLOAK_URL, KEYCLOAK_REALM, KEYCLOAK_CLIENT, Collections.singletonMap("secret", ""), HttpClients.createDefault());
return AuthzClient.create(configuration).obtainAccessToken(KEYCLOAK_USER, KEYCLOAK_PASSWORD);
}
public static RequestInterceptor getRequestInterceptor() {
return new OAuth(new Client.Default(null, null), new OAuthClientRequest.TokenRequestBuilder(TokenUtils.KEYCLOAK_URL));
}
}
It’s recommended to create an instance of ApiClient
per thread in a multithreaded environment to avoid any potential issues.