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

io.github.dft.amazon.AmazonSPOrders 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.orders.v0.GetOrderAddressResponse;
import io.github.dft.amazon.model.orders.v0.GetOrderBuyerInfoResponse;
import io.github.dft.amazon.model.orders.v0.GetOrderItemsBuyerInfoResponse;
import io.github.dft.amazon.model.orders.v0.GetOrderItemsResponse;
import io.github.dft.amazon.model.orders.v0.GetOrderRegulatedInfoResponse;
import io.github.dft.amazon.model.orders.v0.GetOrderResponse;
import io.github.dft.amazon.model.orders.v0.GetOrdersResponse;
import io.github.dft.amazon.model.tokens.v202103.CreateRestrictedDataTokenRequest;
import io.github.dft.amazon.model.tokens.v202103.CreateRestrictedDataTokenResponse;
import io.github.dft.amazon.model.tokens.v202103.RestrictedResource;
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.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class AmazonSPOrders extends AmazonSellingPartnerSdk {

    private static final String AMAZON_ORDERS_PII_PATH = "/orders/v0/orders";
    private static final List AMAZON_ORDERS_PII_DATA_ELEMENTS = Arrays.asList("buyerInfo", "shippingAddress");

    private String restrictedAccessToken;
    private ZonedDateTime restrictedAccessTokenExpireIn;
    private final AmazonSPTokens amazonSPTokens;

    @SneakyThrows
    public AmazonSPOrders(AmazonCredentials amazonCredentials) {
        super(amazonCredentials);
        this.amazonSPTokens = new AmazonSPTokens(amazonCredentials);
    }

    @SneakyThrows
    public GetOrdersResponse getOrders(HashMap params, boolean isPII) {

        refreshAccessToken(false);
        String token = isPII ? getPIIAccessToken(createRestrictedDataToken(AMAZON_ORDERS_PII_PATH)) : amazonCredentials.getAccessToken();
        URIBuilder uriBuilder = new URIBuilder(sellingRegionEndpoint + ConstantCodes.ORDERS_API_V0);
        addParameters(uriBuilder, params);

        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, token)
                .GET()
                .build();

        HttpResponse.BodyHandler handler = new JsonBodyHandler<>(GetOrdersResponse.class);

        return getRequestWrapped(request, handler);
    }

    @SneakyThrows
    public GetOrdersResponse getOrders(HashMap params) {
        return getOrders(params, false);
    }

    public CreateRestrictedDataTokenRequest createRestrictedDataToken(String restrictedTokenPath) {
        CreateRestrictedDataTokenRequest createRestrictedDataTokenRequest = new CreateRestrictedDataTokenRequest();
        List restrictedResources = new ArrayList<>();

        RestrictedResource restrictedResource = new RestrictedResource();
        restrictedResource.setMethod("GET");
        restrictedResource.setPath(restrictedTokenPath);
        restrictedResource.setDataElements(AMAZON_ORDERS_PII_DATA_ELEMENTS);

        restrictedResources.add(restrictedResource);
        createRestrictedDataTokenRequest.setRestrictedResources(restrictedResources);

        return createRestrictedDataTokenRequest;
    }

    private String getPIIAccessToken(CreateRestrictedDataTokenRequest createRestrictedDataTokenRequest) {
        if (restrictedAccessToken == null || ZonedDateTime.now(ZoneOffset.UTC).isAfter(this.restrictedAccessTokenExpireIn)) {
            CreateRestrictedDataTokenResponse createRestrictedDataTokenResponse = amazonSPTokens.createRestrictedDataToken(createRestrictedDataTokenRequest);
            this.restrictedAccessToken = createRestrictedDataTokenResponse.getRestrictedDataToken();
            this.restrictedAccessTokenExpireIn = ZonedDateTime.now(ZoneOffset.UTC).plusSeconds(createRestrictedDataTokenResponse.getExpiresIn());
        }

        return restrictedAccessToken;
    }

    @SneakyThrows
    public GetOrderResponse getOrder(String orderId) {
        return getOrder(orderId, false);
    }

    @SneakyThrows
    public GetOrderResponse getOrder(String orderId, boolean isPII) {
        orderId = StringUtils.isEmpty(orderId) ? "" : orderId;
        String token = isPII ? getPIIAccessToken(createRestrictedDataToken(AMAZON_ORDERS_PII_PATH + "/" + orderId)) : amazonCredentials.getAccessToken();
        refreshAccessToken(false);

        HttpRequest request = HttpRequest.newBuilder(new URI(sellingRegionEndpoint + ConstantCodes.ORDERS_API_V0 + "/" + orderId))
                .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, token)
                .GET()
                .build();

        HttpResponse.BodyHandler handler = new JsonBodyHandler<>(GetOrderResponse.class);

        return getRequestWrapped(request, handler);
    }

    @SneakyThrows
    public GetOrderBuyerInfoResponse getOrderBuyerInfo(String orderId) {
        orderId = StringUtils.isEmpty(orderId) ? "" : orderId;

        refreshAccessToken(false);
        HttpRequest request = HttpRequest.newBuilder(new URI(sellingRegionEndpoint + ConstantCodes.ORDER_BUYER_INFO_API_V0.replace("{orderId}", orderId)))
                .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<>(GetOrderBuyerInfoResponse.class);

        return getRequestWrapped(request, handler);
    }

    @SneakyThrows
    public GetOrderAddressResponse getAddress(String orderId) {
        orderId = StringUtils.isEmpty(orderId) ? "" : orderId;

        refreshAccessToken(false);

        HttpRequest request = HttpRequest.newBuilder(new URI(sellingRegionEndpoint + ConstantCodes.ORDER_ADDRESS_API_V0.replace("{orderId}", orderId)))
                .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<>(GetOrderAddressResponse.class);

        return getRequestWrapped(request, handler);
    }

    @SneakyThrows
    public GetOrderItemsResponse getOrderItems(String orderId) {
        orderId = StringUtils.isEmpty(orderId) ? "" : orderId;

        refreshAccessToken(false);

        HttpRequest request = HttpRequest.newBuilder(new URI(sellingRegionEndpoint + ConstantCodes.ORDER_ITEMS_API_V0.replace("{orderId}", orderId)))
                .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<>(GetOrderItemsResponse.class);

        return getRequestWrapped(request, handler);
    }

    @SneakyThrows
    public GetOrderItemsBuyerInfoResponse getOrderItemsBuyerInfo(String orderId) {
        orderId = StringUtils.isEmpty(orderId) ? "" : orderId;

        refreshAccessToken(false);

        HttpRequest request = HttpRequest.newBuilder(new URI(sellingRegionEndpoint + ConstantCodes.ORDER_ITEMS_BUYER_INFO_API_V0.replace("{orderId}", orderId)))
                .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<>(GetOrderItemsBuyerInfoResponse.class);

        return getRequestWrapped(request, handler);
    }

    @SneakyThrows
    public GetOrderRegulatedInfoResponse getOrderRegulatedInfo(String orderId) {
        orderId = StringUtils.isEmpty(orderId) ? "" : orderId;

        refreshAccessToken(false);

        HttpRequest request = HttpRequest.newBuilder(new URI(sellingRegionEndpoint + ConstantCodes.ORDER_REGULATED_INFO_API_V0.replace("{orderId}", orderId)))
                .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<>(GetOrderRegulatedInfoResponse.class);

        return getRequestWrapped(request, handler);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy