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

io.github.dft.amazon.AmazonSPDestinationAPI 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.destination.CreateDestinationRequest;
import io.github.dft.amazon.model.destination.CreateDestinationResponse;
import io.github.dft.amazon.model.destination.GetDestinationsResponse;
import io.github.dft.amazon.model.handler.JsonBodyHandler;

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

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.NOTIFICATION_DESTINATIONS_API_V1;

public class AmazonSPDestinationAPI extends AmazonSellingPartnerSdk {

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

    public CreateDestinationResponse createDestination(CreateDestinationRequest destinationRequest) {
        URI uri = URI.create(sellingRegionEndpoint + NOTIFICATION_DESTINATIONS_API_V1);
        String requestBody = getString(destinationRequest);
        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(requestBody))
                .build();

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

    public GetDestinationsResponse getDestinations() {
        URI uri = URI.create(sellingRegionEndpoint + NOTIFICATION_DESTINATIONS_API_V1);

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

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy