io.github.dft.amazon.AmazonSPCatalogItems Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of amazon-sp-api Show documentation
Show all versions of amazon-sp-api Show documentation
Amazon SP API using JDK 11
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.catalogitems.v202204.CatalogCategoriesResponse;
import io.github.dft.amazon.model.catalogitems.v202204.Item;
import io.github.dft.amazon.model.catalogitems.v202204.ItemSearchResults;
import io.github.dft.amazon.model.handler.JsonBodyHandler;
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;
public class AmazonSPCatalogItems extends AmazonSellingPartnerSdk {
public AmazonSPCatalogItems(AmazonCredentials amazonCredentials) {
super(amazonCredentials);
}
@SneakyThrows
public Item getCatalogItem(String asin, HashMap params) {
refreshAccessToken(false);
URIBuilder uriBuilder = new URIBuilder(sellingRegionEndpoint + ConstantCodes.CATALOG_ITEMS_API_V202204 + asin);
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<>(Item.class);
return getRequestWrapped(request, handler);
}
@SneakyThrows
public ItemSearchResults searchCatalogItems(HashMap
params) {
refreshAccessToken(false);
URIBuilder uriBuilder = new URIBuilder(sellingRegionEndpoint + ConstantCodes.CATALOG_ITEMS_API_V202204);
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<>(ItemSearchResults.class);
return getRequestWrapped(request, handler);
}
@SneakyThrows
public CatalogCategoriesResponse listCatalogCategories(HashMap params) {
refreshAccessToken(false);
URIBuilder uriBuilder = new URIBuilder(sellingRegionEndpoint + ConstantCodes.CATALOG_CATEGORIES_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, amazonCredentials.getAccessToken())
.GET()
.build();
HttpResponse.BodyHandler handler = new JsonBodyHandler<>(CatalogCategoriesResponse.class);
return getRequestWrapped(request, handler);
}
}