README optimserver-client-resttemplate 2.1

Installation

Maven users

Add this dependency to your project’s POM:

<dependency>
  <groupId>com.decisionbrain</groupId>
  <artifactId>optimserver-client-resttemplate</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-resttemplate:2.5.0"
compile "org.keycloak:keycloak-authz-client:4.6.0.Final"

Getting Started

First follow the installation instructions. Then create a spring boot application class like the following one:


import com.decisionbrain.optimserver.client.java.resttemplate.ApiClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.apache.http.impl.client.HttpClients;
import org.keycloak.authorization.client.AuthzClient;
import org.keycloak.authorization.client.Configuration;
import java.util.Collections;

@SpringBootApplication
@ComponentScan({"com.decisionbrain.optimserver.client.java.resttemplate"})
public class OptimserverSampleSpringClientApplication {

    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";

    @Bean
    public ApiClient apiClient(@Value("${master.url}") String basePath) {
        ApiClient apiClient = new ApiClient();
        apiClient
            .setBasePath(basePath)
            .addDefaultHeader("Authorization", "Bearer " + getToken());
        return apiClient;
    }

    public static void main(String[] args) {
        SpringApplication.run(OptimserverSampleSpringClientApplication.class, args);
    }

    private static String getToken() {
        final Configuration configuration = new Configuration(KEYCLOAK_URL, KEYCLOAK_REALM, KEYCLOAK_CLIENT, Collections.singletonMap("secret", ""), HttpClients.createDefault());

        try {
            return AuthzClient.create(configuration).obtainAccessToken(KEYCLOAK_USER, KEYCLOAK_PASSWORD).getToken();
        } catch (Exception e) {
            throw new IllegalArgumentException("Token can't be obtained", e);
        }
    }
}

Property “master.url” has to be defined in a spring properties file. Finally, the following java code is able to create a bucket, using spring beanw autowiring.


import com.decisionbrain.optimserver.client.java.resttemplate.model.*;
import com.decisionbrain.optimserver.client.java.resttemplate.api.BucketApi;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.boot.CommandLineRunner;

@Service
public class BucketApiExampleService implements CommandLineRunner {

    private final BucketApi bucketClient;

    @Autowired
    public BucketApiExampleService(
            BucketApi bucketClient
    ) {
        this.bucketClient = bucketClient;
    }

    @Override
    public void run(String... args) throws Exception {
        BucketCreation bucketCreation = new BucketCreation(); // BucketCreation | 
        BucketDefinition result = bucketClient.createBucket(bucketCreation);
        System.out.println(result);
    }
}

Documentation for API Endpoints

All URIs are relative to https://dbos

Class Method HTTP request Description
BucketApi createBucket POST /buckets Creates a new Bucket, and returns its ID for later use.
BucketApi deleteBucket DELETE /buckets/{bucketId} Deletes this bucket.
BucketApi getBucket GET /buckets/{bucketId} Return a bucket object.
BucketApi getBucketContent GET /buckets/{bucketId}/content Returns the content of the requested bucket.
BucketApi getBuckets GET /buckets Returns requested bucket definitions.
BucketApi getBucketsContent GET /buckets/content Returns an archive containing the compressed content of requested buckets.
FlowApi createFlow POST /jobFlows Creates a new Flow, and returns its ID for later use.
FlowApi getJobDefinitionsByFlow GET /jobFlows/{flowId}/jobs Returns job definitions for the given flow ID.
FlowApi getJobFlow GET /jobFlows/{flowId} Returns a Flow with the given ID, if any.
FlowApi getJobFlows GET /jobFlows Returns all known flows.
JobApi createJob POST /jobs Creates a new asyncron Job (without starting it), and returns its ID for later use.
JobApi deleteJob DELETE /jobs/{jobId} Deletes this job and cleans all related data.
JobApi getJobDefinition GET /jobs/{jobId} Returns a job definition.
JobApi getJobs GET /jobs Returns all current job definitions.
JobExecutionApi getJobExecutionStatus GET /jobs/{jobId}/execution Gets the job execution status.
JobExecutionApi startAsyncJobExecution POST /jobs/{jobId}/execution Schedule an already created job for an asyncronous execution.
JobExecutionApi startSyncJobExecution POST /jobs/execution Schedule a syncron job execution.
JobExecutionApi stopJobExecution DELETE /jobs/{jobId}/execution Request this job to stop its execution.
ProjectInformationApi getProject GET /project Gets the project information.
TaskApi getTask GET /tasks/{taskId} Get a specific task details.
TaskApi getTasks GET /tasks Get all known tasks.
TaskExecutionApi getTaskExecution GET /tasks/{taskId}/execution Gets all the jobs execution status for this task.

Documentation for Models

Documentation for Authorization

Authentication schemes defined for the API:

BasicAuth

  • Type: HTTP basic authentication

BearerAuth

  • Type: HTTP basic authentication

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