Expand my Community achievements bar.

SOLVED

Adobe Analytics client implementation of open api definition

Avatar

Level 1

Hi,

 

I am trying to implement in Java an Adobe Analytics 2.0 client using open api client generation with this definition 

 

https://adobedocs.github.io/analytics-2.0-apis/datasources.json

 

I need to use the POST JOB endpoint to upload a data file to an existing Data Srouces account. My issue is that I cannot find a way how could I provide the data file to the endpoint createJob.

 

I believe there's an issue with the above api definition for the data sources because also in the swagger page there are errors:

 

Go to  https://adobedocs.github.io/analytics-2.0-apis/?urls.primaryName=Data%20Sources%202.0%20APIs

 

Select a definition : Data Sources 2.0 API

Under JOB definition there's a POST endpoint that doesn't work (there are also errors in the console).

 

Am I doing something wrong? Did anybody face this issue before?

I'm trying to achive this: https://developer.adobe.com/analytics-apis/docs/2.0/guides/endpoints/data-sources/#post-data 

 

Here is my client implementation. 

 

@Slf4j
@Service
public class AdobeAnalyticsClient {

  JobApi jobApi;
  AdobeAnalyticsProperties adobeAnalyticsProperties;
  StatisticsProperties statisticsProperties;

  public AdobeAnalyticsClient(
      final JobApi jobApi,
      final AdobeAnalyticsProperties adobeAnalyticsProperties,
      final ApiClientProperties apiClientProperties,
      final StatisticsProperties statisticsProperties) {
    this.jobApi = jobApi;
    this.adobeAnalyticsProperties = adobeAnalyticsProperties;
    this.statisticsProperties = statisticsProperties;
    setupClient(apiClientProperties);
  }

  public DataSourcesJob importStatisticsData(File file) throws ApiException {
// how should can I provide the input file here?
    return jobApi.createJob(statisticsProperties.getReportSuiteId(), adobeAnalyticsProperties.getConfigDataSourceId());
  }

  private void setupClient(final ApiClientProperties apiClientProperties) {
    log.info("Setting up adobe analytics client: {}", adobeAnalyticsProperties.getBaseUrl());

    final ApiClient apiClient = new ApiClient();

    apiClient.setBasePath(adobeAnalyticsProperties.getBaseUrl());
    apiClient.setApiKey(adobeAnalyticsProperties.getGlobalCompanyId());
    apiClient.setAccessToken(adobeAnalyticsProperties.getAccessToken());

    apiClient.setConnectTimeout(apiClientProperties.getDefaultConnectTimeoutMs());
    apiClient.setReadTimeout(apiClientProperties.getDefaultReadTimeoutMs());
    apiClient.setWriteTimeout(apiClientProperties.getDefaultWriteTimeoutMs());

    jobApi.setApiClient(apiClient);
  }
}

 

 

1 Accepted Solution

Avatar

Correct answer by
Level 1

Hi, thanks for the feedback,

 

I managed to upload the file using a rest template, e.g.

    String url = String.format(adobeAnalyticsProperties.getBaseUrl(), adobeAnalyticsProperties.getGlobalCompanyId(),
        statisticsProperties.getReportSuiteId() , adobeAnalyticsProperties.getConfigDataSourceId());
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    headers.add(ACCEPT, "application/json");
    headers.add(X_API_KEY, adobeAnalyticsProperties.getClientId());
    headers.add(AUTHORIZATION, "Bearer " + access_token);

    File file = new File("C:\\Dev\\statistics\\upload_data.txt");
    FileSystemResource fileResource = new FileSystemResource(file);

    // Create multipart body
    MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
    body.add("file", fileResource);

    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
    var response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);

    log.info("response {}", response.getBody());

And it works, file is uploaded, and processesed by the data source on adobe analytics side.

I did not use the jobApi.createJob anymore. Not sure what is it's purpose 

View solution in original post

2 Replies

Avatar

Employee Advisor

I think you have to modify this code to something like below -

 

    public DataSourcesJob importStatisticsData(File file) throws ApiException {
        log.info("Uploading file to Adobe Analytics: {}", file.getName());

        // Create API URL for the job
        String url = adobeAnalyticsProperties.getBaseUrl() + "/api/"
                + adobeAnalyticsProperties.getGlobalCompanyId() + "/datasources/job/"
                + statisticsProperties.getReportSuiteId() + "/"
                + adobeAnalyticsProperties.getConfigDataSourceId();

        try {
            String response = uploadFile(url, adobeAnalyticsProperties.getApiKey(), adobeAnalyticsProperties.getAccessToken(), file);
            log.info("Adobe Analytics API Response: {}", response);
        } catch (IOException e) {
            log.error("Error uploading file to Adobe Analytics", e);
        }

        // Call createJob API after file upload
        return jobApi.createJob(statisticsProperties.getReportSuiteId(), adobeAnalyticsProperties.getConfigDataSourceId());
    }

    /**
     * Uploads file using HTTPClient
     */
    private String uploadFile(String url, String apiKey, String accessToken, File file) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);

        // Set request headers
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("x-api-key", apiKey);
        httpPost.setHeader("Authorization", "Bearer " + accessToken);

        // Build multipart form-data request
        HttpEntity multipartEntity = MultipartEntityBuilder.create()
                .addBinaryBody("file", file, ContentType.DEFAULT_BINARY, file.getName())
                .build();

        httpPost.setEntity(multipartEntity);

        // Execute request
        return EntityUtils.toString(httpClient.execute(httpPost).getEntity());
    }

You can try something similar and see if this works.

Avatar

Correct answer by
Level 1

Hi, thanks for the feedback,

 

I managed to upload the file using a rest template, e.g.

    String url = String.format(adobeAnalyticsProperties.getBaseUrl(), adobeAnalyticsProperties.getGlobalCompanyId(),
        statisticsProperties.getReportSuiteId() , adobeAnalyticsProperties.getConfigDataSourceId());
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    headers.add(ACCEPT, "application/json");
    headers.add(X_API_KEY, adobeAnalyticsProperties.getClientId());
    headers.add(AUTHORIZATION, "Bearer " + access_token);

    File file = new File("C:\\Dev\\statistics\\upload_data.txt");
    FileSystemResource fileResource = new FileSystemResource(file);

    // Create multipart body
    MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
    body.add("file", fileResource);

    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
    var response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);

    log.info("response {}", response.getBody());

And it works, file is uploaded, and processesed by the data source on adobe analytics side.

I did not use the jobApi.createJob anymore. Not sure what is it's purpose