[ 
https://issues.apache.org/jira/browse/NIFI-5022?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16507947#comment-16507947
 ] 

ASF GitHub Bot commented on NIFI-5022:
--------------------------------------

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

    https://github.com/apache/nifi/pull/2588#discussion_r194380548
  
    --- 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 --
    
    done


> Create an AWS Gateway Web API version of InvokeHTTP
> ---------------------------------------------------
>
>                 Key: NIFI-5022
>                 URL: https://issues.apache.org/jira/browse/NIFI-5022
>             Project: Apache NiFi
>          Issue Type: New Feature
>            Reporter: Otto Fowler
>            Assignee: Otto Fowler
>            Priority: Major
>
> Currently the AWS processors are lacking support to call AWS Gateway Web Apis.
> Nifi should provide support for calling this apis, including support for all 
> the same authentication methods available to the other AWS client processors.
> Since these APIs are web services, their expected use would require an 
> interface more like InvokeHTTP however, than the specialized interfaces for 
> the other AWS Services.
> What would be required then would be a new AWS Processor that exposed the 
> same interface as InvokeHTTP, but backed by the AWS client support ( and of 
> course modified to fit the differences between the OK http client and the 
> Amazon client ).
> This new processor should be able to pass all the applicable tests available 
> in the InvokeHttp test suite.
> The processor should also be factored in such a way as to make it possible 
> for the creation of custom processors for specific apis
>  
>  



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to