README optimserver-client-feign 2.1

Installation

Maven users

Add this dependency to your project’s POM:

<dependency>
  <groupId>com.decisionbrain</groupId>
  <artifactId>optimserver-client-feign</artifactId>
  <version>2.5.0</version>
  <scope>compile</scope>
</dependency>

Gradle users

Add this dependency to your project’s build file:

compile "com.decisionbrain:optimserver-client-feign:2.5.0"
compile "org.keycloak:keycloak-authz-client:4.6.0.Final"

Getting Started

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));
    }
}

Recommendation

It’s recommended to create an instance of ApiClient per thread in a multithreaded environment to avoid any potential issues.

Author

optim-server@decisionbrain.com