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

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

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

import com.amazonaws.http.HttpMethodName;
import io.github.dft.amazon.model.AmazonCredentials;
import io.github.dft.amazon.model.handler.JsonBodyHandler;
import io.github.dft.amazon.model.listing.v20210801.ListingItemsWrapper;
import io.github.dft.amazon.model.listing.v20210801.ListingsItemPatchRequest;
import io.github.dft.amazon.model.listing.v20210801.ListingsItemSubmissionResponse;
import lombok.SneakyThrows;
import org.apache.http.client.utils.URIBuilder;

import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

import static io.github.dft.amazon.constantcode.ConstantCodes.FORWARD_SLASH;
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.LISTING_ITEMS_V20210801;
import static java.nio.charset.StandardCharsets.UTF_8;

public class AmazonSPListingItemsAPI extends AmazonSellingPartnerSdk {

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

    @SneakyThrows
    public ListingItemsWrapper getListingItemsBySellerIdAndSku(String sellerId, String sku, HashMap params) {
        sku = URLEncoder.encode(sku, UTF_8).replace("+", "%20");
        URIBuilder uriBuilder = new URIBuilder(sellingRegionEndpoint + LISTING_ITEMS_V20210801 + FORWARD_SLASH + sellerId + FORWARD_SLASH + sku);
        addParameters(uriBuilder, params);
        URI uri = uriBuilder.build();

        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<>(ListingItemsWrapper.class);
        return getRequestWrapped(request, handler);
    }

    public ListingsItemSubmissionResponse patchListingsItem(String sellerId, String sku, HashMap params, ListingsItemPatchRequest patchRequest) {
        sku = URLEncoder.encode(sku, StandardCharsets.UTF_8);
        URI uri = addParameters(sellingRegionEndpoint + LISTING_ITEMS_V20210801 + FORWARD_SLASH + sellerId + FORWARD_SLASH + sku, params);

        refreshAccessToken(false);

        String payload = getString(patchRequest);

        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())
                .method(HttpMethodName.PATCH.name(), HttpRequest.BodyPublishers.ofString(payload))
                .build();

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy