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

io.github.dft.amazon.AmazonSPSubscriptionsAPI 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.subscription.v1.SubscriptionRequest;
import io.github.dft.amazon.model.subscription.v1.SubscriptionWrapper;

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

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

public class AmazonSPSubscriptionsAPI extends AmazonSellingPartnerSdk {

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

    public SubscriptionWrapper createSubscription(String notificationType, SubscriptionRequest subscriptionRequest) {
        URI uri = URI.create(sellingRegionEndpoint + NOTIFICATION_SUBSCRIPTION_API_V1 + FORWARD_SLASH + notificationType);
        String requestBody = getString(subscriptionRequest);

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

    public SubscriptionWrapper getAllSubscriptionByNotificationType(String notificationType) {
        URI uri = URI.create(sellingRegionEndpoint + NOTIFICATION_SUBSCRIPTION_API_V1 + FORWARD_SLASH + notificationType);

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

    public SubscriptionWrapper getByNotificationTypeAndSubscriptionId(String notificationType, String subscriptionId) {
        URI uri = URI.create(sellingRegionEndpoint + NOTIFICATION_SUBSCRIPTION_API_V1 + FORWARD_SLASH + notificationType + FORWARD_SLASH + subscriptionId);

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

    public void deleteByNotificationTypeAndSubscriptionId(String notificationType, String subscriptionId) {
        URI uri = URI.create(sellingRegionEndpoint + NOTIFICATION_SUBSCRIPTION_API_V1 + FORWARD_SLASH + notificationType + FORWARD_SLASH + subscriptionId);

        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())
                .DELETE()
                .build();

        getRequestWrapped(request, HttpResponse.BodyHandlers.ofString());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy