All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.dft.amazon.AmazonSPReports Maven / Gradle / Ivy

There is a newer version: 2.1.9
Show newest version
package io.github.dft.amazon;

import io.github.dft.amazon.constantcode.ConstantCodes;
import io.github.dft.amazon.model.AmazonCredentials;
import io.github.dft.amazon.model.handler.JsonBodyHandler;
import io.github.dft.amazon.model.reports.v202106.CancelResponse;
import io.github.dft.amazon.model.reports.v202106.CreateReportResponse;
import io.github.dft.amazon.model.reports.v202106.CreateReportScheduleResponse;
import io.github.dft.amazon.model.reports.v202106.CreateReportScheduleSpecification;
import io.github.dft.amazon.model.reports.v202106.CreateReportSpecification;
import io.github.dft.amazon.model.reports.v202106.GetReportsResponse;
import io.github.dft.amazon.model.reports.v202106.Report;
import io.github.dft.amazon.model.reports.v202106.ReportDocument;
import io.github.dft.amazon.model.reports.v202106.ReportSchedule;
import io.github.dft.amazon.model.reports.v202106.ReportScheduleList;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.URIBuilder;

import java.net.URI;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;

public class AmazonSPReports extends AmazonSellingPartnerSdk {

    @SneakyThrows
    public AmazonSPReports(AmazonCredentials amazonCredentials) {
        super(amazonCredentials);
    }

    @SneakyThrows
    public CreateReportResponse createReport(CreateReportSpecification createReportSpecification) {
        String requestBody = getString(createReportSpecification);

        HttpRequest request = HttpRequest.newBuilder(new URI(sellingRegionEndpoint + ConstantCodes.REPORTS_API_V202106))
                                         .header(ConstantCodes.HTTP_HEADER_ACCEPTS, ConstantCodes.HTTP_HEADER_VALUE_APPLICATION_JSON)
                                         .header(ConstantCodes.HTTP_HEADER_CONTENT_TYPE, ConstantCodes.HTTP_HEADER_VALUE_APPLICATION_JSON)
                                         .header(ConstantCodes.HTTP_HEADER_X_AMZ_ACCESS_TOKEN, amazonCredentials.getAccessToken())
                                         .POST(HttpRequest.BodyPublishers.ofString(requestBody))
                                         .build();

        HttpResponse.BodyHandler handler = new JsonBodyHandler<>(CreateReportResponse.class);
        return client
            .sendAsync(request, handler)
            .thenComposeAsync(response -> tryResend(client, request, handler, response, 1))
            .get()
            .body();
    }

    @SneakyThrows
    public Report getReport(String reportId) {

        refreshAccessToken(false);

        HttpRequest request = HttpRequest.newBuilder(new URI(sellingRegionEndpoint + ConstantCodes.REPORTS_API_V202106.concat("/").concat(reportId)))
                                         .header(ConstantCodes.HTTP_HEADER_ACCEPTS, ConstantCodes.HTTP_HEADER_VALUE_APPLICATION_JSON)
                                         .header(ConstantCodes.HTTP_HEADER_CONTENT_TYPE, ConstantCodes.HTTP_HEADER_VALUE_APPLICATION_JSON)
                                         .header(ConstantCodes.HTTP_HEADER_X_AMZ_ACCESS_TOKEN, amazonCredentials.getAccessToken())
                                         .GET()
                                         .build();

        HttpResponse.BodyHandler handler = new JsonBodyHandler<>(Report.class);
        return client
            .sendAsync(request, handler)
            .thenComposeAsync(response -> tryResend(client, request, handler, response, 1))
            .get()
            .body();
    }

    @SneakyThrows
    public ReportDocument getReportDocument(String reportDocumentId) {
        reportDocumentId = StringUtils.isEmpty(reportDocumentId) ? "" : reportDocumentId;

        refreshAccessToken(false);

        HttpRequest request = HttpRequest.newBuilder(new URI(sellingRegionEndpoint + ConstantCodes.REPORT_DOCUMENTS_API_V202106.concat("/").concat(reportDocumentId)))
                                         .header(ConstantCodes.HTTP_HEADER_ACCEPTS, ConstantCodes.HTTP_HEADER_VALUE_APPLICATION_JSON)
                                         .header(ConstantCodes.HTTP_HEADER_CONTENT_TYPE, ConstantCodes.HTTP_HEADER_VALUE_APPLICATION_JSON)
                                         .header(ConstantCodes.HTTP_HEADER_X_AMZ_ACCESS_TOKEN, amazonCredentials.getAccessToken())
                                         .GET()
                                         .build();

        HttpResponse.BodyHandler handler = new JsonBodyHandler<>(ReportDocument.class);
        return getRequestWrapped(request, handler);
    }

    @SneakyThrows
    public GetReportsResponse getReports(HashMap params) {
        refreshAccessToken(false);
        URIBuilder uriBuilder = new URIBuilder(sellingRegionEndpoint + ConstantCodes.REPORTS_API_V202106);
        if (params != null && !params.isEmpty()) {
            for (String key : params.keySet()) {
                uriBuilder.addParameter(key, params.get(key));
            }
        }

        URI uri = uriBuilder.build();
        HttpRequest request = HttpRequest.newBuilder(uri)
                                         .header(ConstantCodes.HTTP_HEADER_ACCEPTS, ConstantCodes.HTTP_HEADER_VALUE_APPLICATION_JSON)
                                         .header(ConstantCodes.HTTP_HEADER_CONTENT_TYPE, ConstantCodes.HTTP_HEADER_VALUE_APPLICATION_JSON)
                                         .header(ConstantCodes.HTTP_HEADER_X_AMZ_ACCESS_TOKEN, amazonCredentials.getAccessToken())
                                         .GET()
                                         .build();

        HttpResponse.BodyHandler handler = new JsonBodyHandler<>(GetReportsResponse.class);
        return client
            .sendAsync(request, handler)
            .thenComposeAsync(response -> tryResend(client, request, handler, response, 1))
            .get()
            .body();
    }

    @SneakyThrows
    public CancelResponse cancelReport(String reportId) {
        reportId = StringUtils.isEmpty(reportId) ? "" : reportId;

        refreshAccessToken(false);
        HttpRequest request = HttpRequest.newBuilder(new URI(sellingRegionEndpoint + ConstantCodes.REPORTS_API_V202106.concat("/").concat(reportId)))
                                         .header(ConstantCodes.HTTP_HEADER_ACCEPTS, ConstantCodes.HTTP_HEADER_VALUE_APPLICATION_JSON)
                                         .header(ConstantCodes.HTTP_HEADER_CONTENT_TYPE, ConstantCodes.HTTP_HEADER_VALUE_APPLICATION_JSON)
                                         .header(ConstantCodes.HTTP_HEADER_X_AMZ_ACCESS_TOKEN, amazonCredentials.getAccessToken())
                                         .DELETE()
                                         .build();

        HttpResponse.BodyHandler handler = new JsonBodyHandler<>(CancelResponse.class);
        return getRequestWrapped(request, handler);
    }

    @SneakyThrows
    public CreateReportScheduleResponse createReportSchedule(CreateReportScheduleSpecification createReportScheduleSpecification) {
        String requestBody = getString(createReportScheduleSpecification);

        refreshAccessToken(false);

        HttpRequest request = HttpRequest.newBuilder(new URI(sellingRegionEndpoint + ConstantCodes.REPORT_SCHEDULES_API_V202106))
                                         .header(ConstantCodes.HTTP_HEADER_ACCEPTS, ConstantCodes.HTTP_HEADER_VALUE_APPLICATION_JSON)
                                         .header(ConstantCodes.HTTP_HEADER_CONTENT_TYPE, ConstantCodes.HTTP_HEADER_VALUE_APPLICATION_JSON)
                                         .header(ConstantCodes.HTTP_HEADER_X_AMZ_ACCESS_TOKEN, amazonCredentials.getAccessToken())
                                         .POST(HttpRequest.BodyPublishers.ofString(requestBody))
                                         .build();

        HttpResponse.BodyHandler handler = new JsonBodyHandler<>(CreateReportScheduleResponse.class);
        return getRequestWrapped(request, handler);
    }

    @SneakyThrows
    public ReportSchedule getReportSchedule(String reportScheduleId) {
        reportScheduleId = StringUtils.isEmpty(reportScheduleId) ? "" : reportScheduleId;

        refreshAccessToken(false);

        HttpRequest request = HttpRequest.newBuilder(new URI(sellingRegionEndpoint + ConstantCodes.REPORT_SCHEDULES_API_V202106.concat("/").concat(reportScheduleId)))
                                         .header(ConstantCodes.HTTP_HEADER_ACCEPTS, ConstantCodes.HTTP_HEADER_VALUE_APPLICATION_JSON)
                                         .header(ConstantCodes.HTTP_HEADER_CONTENT_TYPE, ConstantCodes.HTTP_HEADER_VALUE_APPLICATION_JSON)
                                         .header(ConstantCodes.HTTP_HEADER_X_AMZ_ACCESS_TOKEN, amazonCredentials.getAccessToken())
                                         .build();

        HttpResponse.BodyHandler handler = new JsonBodyHandler<>(ReportSchedule.class);
        return getRequestWrapped(request, handler);
    }

    @SneakyThrows
    public ReportScheduleList getReportSchedules(HashMap params) {
        refreshAccessToken(false);

        URIBuilder uriBuilder = new URIBuilder(sellingRegionEndpoint + ConstantCodes.REPORT_SCHEDULES_API_V202106);
        if (params != null && !params.isEmpty()) {
            for (String key : params.keySet()) {
                uriBuilder.addParameter(key, params.get(key));
            }
        }

        URI uri = uriBuilder.build();
        HttpRequest request = HttpRequest.newBuilder(uri)
                                         .header(ConstantCodes.HTTP_HEADER_ACCEPTS, ConstantCodes.HTTP_HEADER_VALUE_APPLICATION_JSON)
                                         .header(ConstantCodes.HTTP_HEADER_CONTENT_TYPE, ConstantCodes.HTTP_HEADER_VALUE_APPLICATION_JSON)
                                         .header(ConstantCodes.HTTP_HEADER_X_AMZ_ACCESS_TOKEN, amazonCredentials.getAccessToken())
                                         .build();

        HttpResponse.BodyHandler handler = new JsonBodyHandler<>(ReportScheduleList.class);
        return getRequestWrapped(request, handler);
    }

    @SneakyThrows
    public CancelResponse cancelReportSchedule(String reportScheduleId) {
        reportScheduleId = StringUtils.isEmpty(reportScheduleId) ? "" : reportScheduleId;

        refreshAccessToken(false);

        HttpRequest request = HttpRequest.newBuilder(new URI(sellingRegionEndpoint + ConstantCodes.REPORT_SCHEDULES_API_V202106.concat("/").concat(reportScheduleId)))
                                         .header(ConstantCodes.HTTP_HEADER_ACCEPTS, ConstantCodes.HTTP_HEADER_VALUE_APPLICATION_JSON)
                                         .header(ConstantCodes.HTTP_HEADER_CONTENT_TYPE, ConstantCodes.HTTP_HEADER_VALUE_APPLICATION_JSON)
                                         .header(ConstantCodes.HTTP_HEADER_X_AMZ_ACCESS_TOKEN, amazonCredentials.getAccessToken())
                                         .DELETE()
                                         .build();

        HttpResponse.BodyHandler handler = new JsonBodyHandler<>(CancelResponse.class);
        return getRequestWrapped(request, handler);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy