From 3092a37ad6c7e683e17e98f5aea8b1c37e2069c2 Mon Sep 17 00:00:00 2001 From: Otto Fowler Date: Thu, 29 Mar 2018 09:09:56 -0400 Subject: [PATCH 1/2] - Handle null response body - add support for request parameters - add to git ignore for intellj files and target directory --- .gitignore | 8 +++++ .../generic/GenericApiGatewayClient.java | 9 ++++-- .../generic/GenericApiGatewayRequest.java | 11 ++++++- .../GenericApiGatewayRequestBuilder.java | 13 ++++++++- .../generic/GenericApiGatewayResponse.java | 6 +++- .../generic/GenericApiGatewayClientTest.java | 29 +++++++++++++++++++ 6 files changed, 70 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 32858aa..43e3193 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,13 @@ +target *.class +# IDE files +# Intellij +.idea/ +*.iml +*.iws +*~ + # Mobile Tools for Java (J2ME) .mtj.tmp/ diff --git a/src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayClient.java b/src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayClient.java index c8f3364..4825653 100644 --- a/src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayClient.java +++ b/src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayClient.java @@ -23,6 +23,7 @@ import java.io.InputStream; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; public class GenericApiGatewayClient extends AmazonWebServiceClient { @@ -64,10 +65,10 @@ public AmazonServiceException unmarshall(JsonNode jsonContent) throws Exception } public GenericApiGatewayResponse execute(GenericApiGatewayRequest request) { - return execute(request.getHttpMethod(), request.getResourcePath(), request.getHeaders(), request.getBody()); + return execute(request.getHttpMethod(), request.getResourcePath(), request.getHeaders(), request.getParameters(), request.getBody()); } - private GenericApiGatewayResponse execute(HttpMethodName method, String resourcePath, Map headers, InputStream content) { + private GenericApiGatewayResponse execute(HttpMethodName method, String resourcePath, Map headers, Map> parameters, InputStream content) { final ExecutionContext executionContext = buildExecutionContext(); DefaultRequest request = new DefaultRequest(API_GATEWAY_SERVICE_NAME); @@ -76,7 +77,9 @@ private GenericApiGatewayResponse execute(HttpMethodName method, String resource 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(); } diff --git a/src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayRequest.java b/src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayRequest.java index 7faceb4..c919af2 100644 --- a/src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayRequest.java +++ b/src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayRequest.java @@ -4,20 +4,25 @@ import java.io.IOException; import java.io.InputStream; +import java.util.List; import java.util.Map; public class GenericApiGatewayRequest { + private final HttpMethodName httpMethod; private final String resourcePath; private final InputStream body; private final Map headers; + private final Map> parameters; public GenericApiGatewayRequest(HttpMethodName httpMethod, String resourcePath, - InputStream body, Map headers) { + InputStream body, Map headers, + Map> parameters) { this.httpMethod = httpMethod; this.resourcePath = resourcePath; this.body = body; this.headers = headers; + this.parameters = parameters; } public HttpMethodName getHttpMethod() { @@ -35,4 +40,8 @@ public InputStream getBody() { public Map getHeaders() { return headers; } + + public Map> getParameters() { + return parameters; + } } diff --git a/src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayRequestBuilder.java b/src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayRequestBuilder.java index a908b77..d1551b1 100644 --- a/src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayRequestBuilder.java +++ b/src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayRequestBuilder.java @@ -4,6 +4,7 @@ import ca.ryangreen.apigateway.util.Validate; import java.io.InputStream; +import java.util.List; import java.util.Map; public class GenericApiGatewayRequestBuilder { @@ -11,6 +12,7 @@ public class GenericApiGatewayRequestBuilder { private String resourcePath; private InputStream body; private Map headers; + private Map> parameters; public GenericApiGatewayRequestBuilder withHttpMethod(HttpMethodName name) { httpMethod = name; @@ -32,9 +34,18 @@ public GenericApiGatewayRequestBuilder withHeaders(Map headers) return this; } + public GenericApiGatewayRequestBuilder withParameters(Map> parameters) { + this.parameters = parameters; + return this; + } + + public boolean hasBody() { + return this.body != null; + } + public GenericApiGatewayRequest build() { Validate.notNull(httpMethod, "HTTP method"); Validate.notEmpty(resourcePath, "Resource path"); - return new GenericApiGatewayRequest(httpMethod, resourcePath, body, headers); + return new GenericApiGatewayRequest(httpMethod, resourcePath, body, headers, parameters); } } \ No newline at end of file diff --git a/src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayResponse.java b/src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayResponse.java index aa3a83e..708bec0 100644 --- a/src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayResponse.java +++ b/src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayResponse.java @@ -11,7 +11,11 @@ public class GenericApiGatewayResponse { public GenericApiGatewayResponse(HttpResponse httpResponse) throws IOException { this.httpResponse = httpResponse; - this.body = IOUtils.toString(httpResponse.getContent()); + if (httpResponse.getContent()!= null) { + this.body = IOUtils.toString(httpResponse.getContent()); + } else { + this.body = null; + } } public HttpResponse getHttpResponse() { diff --git a/src/test/java/ca/ryangreen/apigateway/generic/GenericApiGatewayClientTest.java b/src/test/java/ca/ryangreen/apigateway/generic/GenericApiGatewayClientTest.java index 3670a9a..a42cc6d 100644 --- a/src/test/java/ca/ryangreen/apigateway/generic/GenericApiGatewayClientTest.java +++ b/src/test/java/ca/ryangreen/apigateway/generic/GenericApiGatewayClientTest.java @@ -9,6 +9,8 @@ import com.amazonaws.http.apache.client.impl.SdkHttpClient; import com.amazonaws.regions.Region; import com.amazonaws.regions.Regions; +import java.util.Arrays; +import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.HttpVersion; import org.apache.http.client.methods.HttpUriRequest; @@ -81,6 +83,33 @@ public void testExecute_happy() throws IOException { any(HttpContext.class)); } + @Test + public void testExecute_happy_parameters() throws IOException { + Map headers = new HashMap<>(); + headers.put("Account-Id", "fubar"); + headers.put("Content-Type", "application/json"); + Map> parameters = new HashMap<>(); + parameters.put("MyParam", Arrays.asList("MyParamValue")); + GenericApiGatewayResponse response = client.execute( + new GenericApiGatewayRequestBuilder() + .withBody(new ByteArrayInputStream("test request".getBytes())) + .withHttpMethod(HttpMethodName.POST) + .withHeaders(headers) + .withParameters(parameters) + .withResourcePath("/test/orders").build()); + + assertEquals("Wrong response body", "test payload", response.getBody()); + assertEquals("Wrong response status", 200, response.getHttpResponse().getStatusCode()); + + Mockito.verify(mockClient, times(1)).execute(argThat(new LambdaMatcher<>( + x -> (x.getMethod().equals("POST") + && x.getFirstHeader("Account-Id").getValue().equals("fubar") + && x.getFirstHeader("x-api-key").getValue().equals("12345") + && x.getFirstHeader("Authorization").getValue().startsWith("AWS4") + && x.getURI().toString().equals("https://foobar.execute-api.us-east-1.amazonaws.com/test/orders?MyParam=MyParamValue")))), + any(HttpContext.class)); + } + @Test public void testExecute_noApiKey_noCreds() throws IOException { client = new GenericApiGatewayClientBuilder() From 2cf258403263882ac8086e530f48814914050186 Mon Sep 17 00:00:00 2001 From: Otto Fowler Date: Thu, 29 Mar 2018 14:43:47 -0400 Subject: [PATCH 2/2] remove hasBody per review --- .../apigateway/generic/GenericApiGatewayRequestBuilder.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayRequestBuilder.java b/src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayRequestBuilder.java index d1551b1..e476ac4 100644 --- a/src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayRequestBuilder.java +++ b/src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayRequestBuilder.java @@ -39,10 +39,6 @@ public GenericApiGatewayRequestBuilder withParameters(Map> p return this; } - public boolean hasBody() { - return this.body != null; - } - public GenericApiGatewayRequest build() { Validate.notNull(httpMethod, "HTTP method"); Validate.notEmpty(resourcePath, "Resource path");