Github user jzonthemtn commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2588#discussion_r194370513
  
    --- Diff: 
nifi-nar-bundles/nifi-aws-bundle/nifi-aws-abstract-processors/src/main/java/org/apache/nifi/processors/aws/wag/client/GenericApiGatewayClient.java
 ---
    @@ -0,0 +1,134 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.nifi.processors.aws.wag.client;
    +
    +import com.amazonaws.AmazonServiceException;
    +import com.amazonaws.AmazonWebServiceClient;
    +import com.amazonaws.ClientConfiguration;
    +import com.amazonaws.DefaultRequest;
    +import com.amazonaws.auth.AWS4Signer;
    +import com.amazonaws.auth.AWSCredentialsProvider;
    +import com.amazonaws.http.AmazonHttpClient;
    +import com.amazonaws.http.ExecutionContext;
    +import com.amazonaws.http.HttpMethodName;
    +import com.amazonaws.http.HttpResponseHandler;
    +import com.amazonaws.http.JsonResponseHandler;
    +import com.amazonaws.internal.auth.DefaultSignerProvider;
    +import com.amazonaws.protocol.json.JsonOperationMetadata;
    +import com.amazonaws.protocol.json.SdkStructuredPlainJsonFactory;
    +import com.amazonaws.regions.Region;
    +import com.amazonaws.transform.JsonErrorUnmarshaller;
    +import com.amazonaws.transform.JsonUnmarshallerContext;
    +import com.amazonaws.transform.Unmarshaller;
    +import com.fasterxml.jackson.databind.JsonNode;
    +import java.io.InputStream;
    +import java.net.URI;
    +import java.util.Collections;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +
    +public class GenericApiGatewayClient extends AmazonWebServiceClient {
    +    private static final String API_GATEWAY_SERVICE_NAME = "execute-api";
    +    private static final String API_KEY_HEADER = "x-api-key";
    +
    +    private final JsonResponseHandler<GenericApiGatewayResponse> 
responseHandler;
    +    private final HttpResponseHandler<AmazonServiceException> 
errorResponseHandler;
    +    private final AWSCredentialsProvider credentials;
    +    private String apiKey;
    +    private final AWS4Signer signer;
    +
    +    GenericApiGatewayClient(ClientConfiguration clientConfiguration, 
String endpoint, Region region,
    +                            AWSCredentialsProvider credentials, String 
apiKey, AmazonHttpClient httpClient) {
    +        super(clientConfiguration);
    +        setRegion(region);
    +        setEndpoint(endpoint);
    +        this.credentials = credentials;
    +        this.apiKey = apiKey;
    +        this.signer = new AWS4Signer();
    +        this.signer.setServiceName(API_GATEWAY_SERVICE_NAME);
    +        this.signer.setRegionName(region.getName());
    +
    +        final JsonOperationMetadata metadata = new 
JsonOperationMetadata().withHasStreamingSuccessResponse(false).withPayloadJson(false);
    +        final Unmarshaller<GenericApiGatewayResponse, 
JsonUnmarshallerContext> responseUnmarshaller = in -> new 
GenericApiGatewayResponse(in.getHttpResponse());
    +        this.responseHandler = 
SdkStructuredPlainJsonFactory.SDK_JSON_FACTORY.createResponseHandler(metadata, 
responseUnmarshaller);
    +        JsonErrorUnmarshaller defaultErrorUnmarshaller = new 
JsonErrorUnmarshaller(GenericApiGatewayException.class, null) {
    +            @Override
    +            public AmazonServiceException unmarshall(JsonNode jsonContent) 
throws Exception {
    +                return new 
GenericApiGatewayException(jsonContent.toString());
    +            }
    +        };
    +        this.errorResponseHandler = 
SdkStructuredPlainJsonFactory.SDK_JSON_FACTORY.createErrorResponseHandler(
    +                Collections.singletonList(defaultErrorUnmarshaller), null);
    +
    +        if (httpClient != null) {
    +            super.client = httpClient;
    +        }
    +    }
    +
    +    public GenericApiGatewayResponse execute(GenericApiGatewayRequest 
request) {
    +        return execute(request.getHttpMethod(), request.getResourcePath(), 
request.getHeaders(), request.getParameters(), request.getBody());
    +    }
    +
    +    private GenericApiGatewayResponse execute(HttpMethodName method, 
String resourcePath, Map<String, String> headers, Map<String,List<String>> 
parameters, InputStream content) {
    +        final ExecutionContext executionContext = buildExecutionContext();
    +
    +        DefaultRequest request = new 
DefaultRequest(API_GATEWAY_SERVICE_NAME);
    +        request.setHttpMethod(method);
    +        request.setContent(content);
    +        request.setEndpoint(this.endpoint);
    +        request.setResourcePath(resourcePath);
    +        request.setHeaders(buildRequestHeaders(headers, apiKey));
    +        if (parameters != null) {
    +            request.setParameters(parameters);
    +        }
    +        return this.client.execute(request, responseHandler, 
errorResponseHandler, executionContext).getAwsResponse();
    +    }
    +
    +    private ExecutionContext buildExecutionContext() {
    +        final ExecutionContext executionContext = 
ExecutionContext.builder().withSignerProvider(
    +                new DefaultSignerProvider(this, signer)).build();
    +        executionContext.setCredentialsProvider(credentials);
    +        executionContext.setSigner(signer);
    +        return executionContext;
    +    }
    +
    +    private Map<String, String> buildRequestHeaders(Map<String, String> 
headers, String apiKey) {
    +        if (headers == null) {
    +            headers = new HashMap<>();
    +        }
    +        if (apiKey != null) {
    +            final Map<String, String> headersWithApiKey = new HashMap<>();
    --- End diff --
    
    Is it possible to just add the API key to `headers` without making a new 
map and then always `return headers;`?


---

Reply via email to