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

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

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

import io.github.dft.amazon.model.AmazonCredentials;
import io.github.dft.amazon.model.handler.JsonBodyHandler;
import io.github.dft.amazon.model.productprice.GetCompetitivePriceResponse;
import io.github.dft.amazon.model.productprice.batch.BatchOffersRequest;
import io.github.dft.amazon.model.productprice.batch.GetListingOffersBatchResponse;
import io.github.dft.amazon.model.productprice.listinganditemoffer.GetOffersResponse;
import lombok.SneakyThrows;
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;

import static io.github.dft.amazon.constantcode.ConstantCodes.BATCH_PRODUCTS_PRICING_ITEMS_LISTINGS_V0;
import static io.github.dft.amazon.constantcode.ConstantCodes.BATCH_PRODUCTS_PRICING_ITEMS_OFFERS_V0;
import static io.github.dft.amazon.constantcode.ConstantCodes.HTTP_HEADER_ACCEPTS;
import static io.github.dft.amazon.constantcode.ConstantCodes.HTTP_HEADER_CONTENT_TYPE;
import static io.github.dft.amazon.constantcode.ConstantCodes.HTTP_HEADER_VALUE_APPLICATION_JSON;
import static io.github.dft.amazon.constantcode.ConstantCodes.HTTP_HEADER_X_AMZ_ACCESS_TOKEN;
import static io.github.dft.amazon.constantcode.ConstantCodes.OFFERS_ENDPOINT;
import static io.github.dft.amazon.constantcode.ConstantCodes.PRODUCTS_PRICING_COMPETITIVE_PRICE_API_V0;
import static io.github.dft.amazon.constantcode.ConstantCodes.PRODUCTS_PRICING_ITEMS_V0;
import static io.github.dft.amazon.constantcode.ConstantCodes.PRODUCTS_PRICING_LISTINGS_V0;

public class AmazonSPProductPricing extends AmazonSellingPartnerSdk {

    public AmazonSPProductPricing(AmazonCredentials amazonCredentials) {
        super(amazonCredentials);
    }

    @SneakyThrows
    public GetCompetitivePriceResponse getCompetitivePricing(HashMap params) {
        refreshAccessToken(false);

        URIBuilder uriBuilder = new URIBuilder(sellingRegionEndpoint + PRODUCTS_PRICING_COMPETITIVE_PRICE_API_V0);
        addParameters(uriBuilder, params);
        URI uri = uriBuilder.build();

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

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

    public GetOffersResponse getListingOffersBySku(String sellerSku, HashMap params) {
        URI uri = URI.create(sellingRegionEndpoint + PRODUCTS_PRICING_LISTINGS_V0 + sellerSku + OFFERS_ENDPOINT);
        uri = addParameters(uri, params);

        refreshAccessToken(false);

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

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

    public GetOffersResponse getItemOfferByAsin(String asin, HashMap params) {
        URI uri = URI.create(sellingRegionEndpoint + PRODUCTS_PRICING_ITEMS_V0 + asin + OFFERS_ENDPOINT);
        uri = addParameters(uri, params);

        refreshAccessToken(false);

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

        HttpResponse.BodyHandler handler = new JsonBodyHandler<>(GetOffersResponse.class);
        return getRequestWrapped(request, handler);
    }
    
    public GetListingOffersBatchResponse getItemOffersBatch(BatchOffersRequest batchOffersRequest) {
        URI uri = URI.create(sellingRegionEndpoint + BATCH_PRODUCTS_PRICING_ITEMS_OFFERS_V0);

        refreshAccessToken(false);

        HttpRequest request = HttpRequest.newBuilder(uri)
                                         .header(HTTP_HEADER_ACCEPTS, HTTP_HEADER_VALUE_APPLICATION_JSON)
                                         .header(HTTP_HEADER_CONTENT_TYPE, HTTP_HEADER_VALUE_APPLICATION_JSON)
                                         .header(HTTP_HEADER_X_AMZ_ACCESS_TOKEN, amazonCredentials.getAccessToken())
                                         .POST(HttpRequest.BodyPublishers.ofString(getString(batchOffersRequest)))
                                         .build();

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

    public GetListingOffersBatchResponse getListingOffersBatch(BatchOffersRequest batchOffersRequest) {
        URI uri = URI.create(sellingRegionEndpoint + BATCH_PRODUCTS_PRICING_ITEMS_LISTINGS_V0);

        refreshAccessToken(false);
        String payload = getString(batchOffersRequest);
        HttpRequest request = HttpRequest.newBuilder(uri)
                                         .header(HTTP_HEADER_ACCEPTS, HTTP_HEADER_VALUE_APPLICATION_JSON)
                                         .header(HTTP_HEADER_CONTENT_TYPE, HTTP_HEADER_VALUE_APPLICATION_JSON)
                                         .header(HTTP_HEADER_X_AMZ_ACCESS_TOKEN, amazonCredentials.getAccessToken())
                                         .POST(HttpRequest.BodyPublishers.ofString(payload))
                                         .build();

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy