Development
Datachain provides a set of APIs for interacting with the application, independently of the interface. API calls are subject to the same rights management policy. An initial call must be made to keycloak to obtain a token, which is then sent in business requests.
API documentation is available in two formats:
Sample code for obtaining a token from keycloak
import com.fasterxml.jackson.databind.JsonNode;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** A configuration object contains the information required for keycloak authentication:
Username, password, client id and client secret
*/
public String getToken() {
try {
OkHttpClient client = new OkHttpClient().newBuilder().build();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body =
RequestBody.create(
mediaType,
"grant_type=password&username="
+ config.getKc().getUsername()
+ "&password="
+ config.getKc().getPassword()
+ "&client_id="
+ config.getKc().getClientId()
+ "&client_secret="
+ config.getKc().getClientSecret());
Request request =
new Request.Builder()
.url(config.getKc().getUrl())
.method("POST", body)
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.build();
Response response = client.newCall(request).execute();
// the ObjectMapper below is a Jackson objectmapper
JsonNode json = ObjectMapper.getObjectMapper().readTree(response.body().bytes());
return json.get("access_token").asText();
} catch (Exception e) {
// exception handling
}
}
Example of api call code (here an exposure on a Datablock)
import com.adb.hdh.api.impl.DataBlockControllerApi;
import com.adb.hdh.api.impl.DataExposeControllerApi;
import com.adb.hdh.api.impl.DataFrameControllerApi;
import com.adb.hdh.api.invoker.ApiClient;
import com.adb.hdh.api.model.DataBlock;
import com.adb.hdh.api.model.DataFrameStatus;
import com.fasterxml.jackson.databind.JsonNode;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** A configuration object contains the information needed to access Datachain:
Datachain Url
*/
public String callApi(String token) {
try {
ApiClient defaultClient = new ApiClient();
defaultClient.setBasePath(config.getDc().getUrl());
defaultClient.setAccessToken(json.get("access_token").asText());
// datablock identifier
Long datablockId = 11L;
// client identifier (informative)
String clientId = "API Call";
// context identifier
Integer ctx = 1L;
// project identifier
Long project = 1L;
DataExposeControllerApi apiInstance = new DataExposeControllerApi(defaultClient);
apiInstance.exposeDatablock(datablockId, clientId, ctx, project);
} catch (Exception e) {
// exception handling
}
}