Calling an HTTPS Endpoint with a Java Client

You can also call the HTTPS endpoints directly using a Java client.

This task shows you how to:

Prepare the Call to the HTTPs Endpoint

  1. To use the Java client, you must add the following runtime dependencies.

    For Gradle:

    // Runtime dependencies
     runtimeOnly 'com.fasterxml.jackson.core:jackson-databind:2.13.2.2'
     runtimeOnly 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.2'
     runtimeOnly 'org.openapitools:jackson-databind-nullable:0.2.2'
     runtimeonly 'com.exalead.apollo:data-factory-client-apache:sgi-v3.4'
     runtimeonly 'com.fasterxml.jackson.core:jackson-core:2.13.4'
     runtimeonly 'com.fasterxml.jackson.core:jackson-annotations:2.13.4'
     runtimeonly 'org.apache.httpcomponents:httpclient:4.5.13'
     runtimeonly 'org.apache.httpcomponents:httpcore:4.4.16'
     runtimeonly 'commons-logging:commons-logging:1.2'
            

    For Maven:

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.13.2.2</version>
    </dependency>
    
    <dependency>
      <groupId>com.fasterxml.jackson.datatype</groupId>
      <artifactId>jackson-datatype-jsr310</artifactId>
      <version>2.13.2</version>
    </dependency>
    
    <dependency>
      <groupId>org.openapitools</groupId>
      <artifactId>jackson-databind-nullable</artifactId>
      <version>0.2.2</version>
    </dependency>
     
    <dependency>
      <groupId>com.exalead.apollo</groupId>
      <artifactId>data-factory-client-apache</artifactId>
      <version>sgi-v3.4</version>
    </dependency>
              
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.13.4</version>
    </dependency>
              
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.13.4</version>
    </dependency>
              
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.5.13</version>
    </dependency>
              
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpcore</artifactId>
      <version>4.4.16</version>
    </dependency>
              
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>        
            

  2. Initialize the Java client as shown in the following code sample:

    package org.example;
    
    import com.ds.apollo.dfs.client.apache.generated.invoke.ApiClient;
    import com.ds.apollo.dfs.client.apache.generated.invoke.ServerConfiguration;
    import org.apache.http.impl.client.HttpClientBuilder;
    
    import java.nio.charset.StandardCharsets;
    import java.util.Base64;
    import java.util.List;
    import java.util.Map;
    
    public class DataFactoryClient {
    
      private static final String url =
              "https://devopsf4xzesw2613048-euw1-devprol36-sgi.3dx-staging.3ds.com/data-factory/resources/v1:443";
    
      // CLM Credentials
      private static final String cmlAgentID = "7cd3a65c-ada3-4729-a6d4-6bd6b918927a";
      private static final String cmlAgentPassword = "D(~VD<L\\z->(g;awr5vJmc0@";
    
    
      public static ApiClient build() {
        ApiClient apiClient = new ApiClient();
    
        apiClient.setServers(List.of(
                new ServerConfiguration(url, "Data Factory endpoint", Map.of())
        ));
        apiClient.setServerIndex(0);
    
        apiClient.addDefaultHeader("Authorization", "Basic " +
                Base64.getEncoder().encodeToString((cmlAgentID + ":" + cmlAgentPassword)
                        .getBytes(StandardCharsets.UTF_8)));
    
        apiClient.setHttpClient(HttpClientBuilder
                .create()
                .addInterceptorLast(new HttpResponseInterceptor() {
                  @Override
                  public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
                    if (500 <= response.getStatusLine().getStatusCode() &&  response.getStatusLine().getStatusCode() < 600) {
                      throw new IOException("HTTP 5XX error code, will be retried");
                    }
                  }
                })
                .setRetryHandler(new DefaultHttpRequestRetryHandler(3, true))
                .build());
        return apiClient;
      }
    
    }
    
    public class Example {
    
        // Then access API for example :
        var apiClient = DataFactoryClient.build();
        StorageApi storageApi = new StorageApi(apiClient);
        PipelineApi pipelineApi = new PipelineApi(apiClient);
    
    }

    In the first lines, define the host, the CLM agent ID, and the CLM Agent password. You then create the client node of the API.

Use the ingest Endpoint with the Java Client

You can call the ingest endpoint to ingest data and retrieve warning and error notifications.

Before you begin: See the Prepare the Call to the HTTPs Endpoint section.
  1. Create the list of events that you want to push by API call.

    For example, you can create a single event to add a class into the data model:

    List<Event> eventAddClass = new ArrayList<Event>();
        
    AddOrReplaceClass addClass = new AddOrReplaceClass();
    addClass.setAction("AddOrReplaceClass");
        
    DataModelClassDefinition classToAdd = new DataModelClassDefinition();
    classToAdd.setName("com.`endpoint2.totoClass`"); //data model class full name prefix with the package name
    classToAdd.setAbstract(Boolean.valueOf(false));
        
    List<DataModelAttribute> classAttributes = new ArrayList<DataModelAttribute>();
        
    DataModelAttribute datamodelAttrribute = new DataModelAttribute();
    datamodelAttrribute.setName("name");
    datamodelAttrribute.setType("String");
        
    classAttributes.add(datamodelAttrribute);
        
    classToAdd.setAttributes(classAttributes);
        
    addClass.setClassDefinition(classToAdd);
        
    eventAddClass.add(addClass);

  2. Make the API call to ingest the data:

    Notifications notificationsAddClass = indexunitApi.ingest(indexUnitId, eventAddClass);
    

    The notifications list is empty, unless there are errors or warnings.

Use the ingestJob and getIngestJobStatus Endpoints with the Java Client

You can also call an endpoint to both ingest data and retrieve a job id, and then call another endpoint using this job id to retrieve the status and notifications of the ingestion job.

Before you begin: See the Prepare the Call to the HTTPs Endpoint section.
  1. Create the list of events that you want to push by API call.

    // create your Event list as previously
    List<Event> eventAddClass = new ArrayList<Event>();
    
    AddOrReplaceClass addClass = new AddOrReplaceClass();
    
    DataModelClassDefinition classToAdd = new DataModelClassDefinition();
    classToAdd.setName("com.`endpoint2.totoClass`");    //data model class full name prefix with the package name
    classToAdd.setAbstract(Boolean.valueOf(false));
    
    List<DataModelAttribute> classAttributes = new ArrayList<DataModelAttribute>();
    
    DataModelAttribute datamodelAttrribute = new DataModelAttribute();
    datamodelAttribute.setName("name");
    datamodelAttribute.setType("String");
    
    classAttributes.add(datamodelAttrribute);
    classToAdd.setAttributes(classAttributes);
    addClass.setClassDefinition(classToAdd);
    
    eventAddClass.add(addClass);
        
    //The parameters in input are the list of Events plus optional metadata, like the source name
    IngestJobPayload body = new IngestJobPayload();
    body.setEvents(eventAddClass);
    IngestMetadatas metadata = new IngestMetadatas();
    metadata.setSource("mySource");
    body.setMetadatas(metadata);

  2. Make the API call to ingest the data:

    IngestJobSubmit resp = indexunitApi.ingestJob(indexUnitResourceUUID, body);
    

  3. With the job id returned by the ingestJob endpoint, you can then call the getIngestJobStatus endpoint to retrieve the status and notifications of the ingestion job.

    // Get the job ID from the response
    String ingestJobId = resp.getJobId();
    
    long sleep = 10000; // 10 seconds
    long backoff = 5000; // 5 seconds
    while (isRunning) {
        sleep += backoff;
        Thread.sleep(sleep); // Sleep for the increasing backoff time
        
        // Fetch the current status of the ingest job
        // The notifications are paged, here we ask for the notifications page number 0 with a page size of 100
        IngestJobStatus status = indexunitApi.getIngestJobStatus(indexUnitResourceUUID, ingestJobId, 0 , 100);
        
        // Job is still running if the status is INDEXING
        isRunning = status.getStatus() == IngestJobStatus.StatusEnum.INDEXING;
    }
    
    // Final status check after the job finishes
    IngestJobStatus finalStatus = indexunitApi.getIngestJobStatus(indexUnitResourceUUID, ingestJobId, 0 , 100);
    
    // Handle the job's final status
    switch (finalStatus.getStatus()) {
    case INDEXING:
      // Job is still indexing, handle accordingly
      log.info("Ingest job is still indexing...");
      break;
    case COMPLETED:
      // Job completed successfully
      log.info("Ingest successfully ingested with {} notifications", finalStatus.getNotificationCount());
      break;
    case FAILED:
      // Job failed
      // Look at the job notifications
      log.error("Ingest failed with notifications : {}", finalStatus.getNotifications());
      break;
    default:
        log.error("Unexpected ingestion job status: {}", finalStatus.getStatus());
    }