
package com.amazonservices.mws.FulfillmentInboundShipment.samples;

import java.util.*;
import java.util.concurrent.*;

import com.amazonservices.mws.FulfillmentInboundShipment.FBAInboundServiceMWSAsync;
import com.amazonservices.mws.FulfillmentInboundShipment.FBAInboundServiceMWSAsyncClient;
import com.amazonservices.mws.FulfillmentInboundShipment.FBAInboundServiceMWSException;
import com.amazonservices.mws.FulfillmentInboundShipment.model.*;

/** Sample async call for GetPalletLabels. */
public class GetPalletLabelsAsyncSample {

    /**
     * Call the service, log response and exceptions.
     *
     * @param client
     * @param request
     *
     * @return The response.
     */
    public static List<Object> invokeGetPalletLabels(
            FBAInboundServiceMWSAsync client,
            List<GetPalletLabelsRequest> requestList) {
        // Call the service async.
        List<Future<GetPalletLabelsResponse>> futureList = 
            new ArrayList<Future<GetPalletLabelsResponse>>();
        for (GetPalletLabelsRequest request : requestList) {
            Future<GetPalletLabelsResponse> future = 
                client.getPalletLabelsAsync(request);
            futureList.add(future);
        }
        List<Object> responseList = new ArrayList<Object>();
        for (Future<GetPalletLabelsResponse> future : futureList) {
            Object xresponse;
            try {
                GetPalletLabelsResponse response = future.get();
                ResponseHeaderMetadata rhmd = response.getResponseHeaderMetadata();
                // We recommend logging every the request id and timestamp of every call.
                System.out.println("Response:");
                System.out.println("RequestId: "+rhmd.getRequestId());
                System.out.println("Timestamp: "+rhmd.getTimestamp());
                String responseXml = response.toXML();
                System.out.println(responseXml);
                xresponse = response;
            } catch (ExecutionException ee) {
                Throwable cause = ee.getCause();
                if (cause instanceof FBAInboundServiceMWSException) {
                    // Exception properties are important for diagnostics.
                    FBAInboundServiceMWSException ex = 
                        (FBAInboundServiceMWSException)cause;
                    ResponseHeaderMetadata rhmd = ex.getResponseHeaderMetadata();
                    System.out.println("Service Exception:");
                    System.out.println("RequestId: "+rhmd.getRequestId());
                    System.out.println("Timestamp: "+rhmd.getTimestamp());
                    System.out.println("Message: "+ex.getMessage());
                    System.out.println("StatusCode: "+ex.getStatusCode());
                    System.out.println("ErrorCode: "+ex.getErrorCode());
                    System.out.println("ErrorType: "+ex.getErrorType());
                    xresponse = ex;
                } else {
                    xresponse = cause;
                }
            } catch (Exception e) {
                xresponse = e;
            }
            responseList.add(xresponse);
        }
        return responseList;
    }

    /**
     *  Command line entry point.
     */
    public static void main(String[] args) {

        // Get a client connection.
        FBAInboundServiceMWSAsyncClient client = FBAInboundServiceMWSSampleConfig.getAsyncClient();

        // Create a request list.
        List<GetPalletLabelsRequest> requestList = new ArrayList<GetPalletLabelsRequest>();
        GetPalletLabelsRequest request = new GetPalletLabelsRequest();
        String sellerId = "example";
        request.setSellerId(sellerId);
        String mwsAuthToken = "example";
        request.setMWSAuthToken(mwsAuthToken);
        String shipmentId = "example";
        request.setShipmentId(shipmentId);
        String pageType = "example";
        request.setPageType(pageType);
        int numberOfPallets = 1;
        request.setNumberOfPallets(numberOfPallets);
        requestList.add(request);

        // Make the calls.
        GetPalletLabelsAsyncSample.invokeGetPalletLabels(client, requestList);

    }

}
