Skip to content

Commit 32eea44

Browse files
authored
Merge pull request #7 from ottobackwards/params_null_body
Handle null response body and support Parameters
2 parents dac3e3c + 2cf2584 commit 32eea44

6 files changed

Lines changed: 66 additions & 6 deletions

File tree

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
target
12
*.class
23

4+
# IDE files
5+
# Intellij
6+
.idea/
7+
*.iml
8+
*.iws
9+
*~
10+
311
# Mobile Tools for Java (J2ME)
412
.mtj.tmp/
513

src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayClient.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.InputStream;
2424
import java.util.Collections;
2525
import java.util.HashMap;
26+
import java.util.List;
2627
import java.util.Map;
2728

2829
public class GenericApiGatewayClient extends AmazonWebServiceClient {
@@ -64,10 +65,10 @@ public AmazonServiceException unmarshall(JsonNode jsonContent) throws Exception
6465
}
6566

6667
public GenericApiGatewayResponse execute(GenericApiGatewayRequest request) {
67-
return execute(request.getHttpMethod(), request.getResourcePath(), request.getHeaders(), request.getBody());
68+
return execute(request.getHttpMethod(), request.getResourcePath(), request.getHeaders(), request.getParameters(), request.getBody());
6869
}
6970

70-
private GenericApiGatewayResponse execute(HttpMethodName method, String resourcePath, Map<String, String> headers, InputStream content) {
71+
private GenericApiGatewayResponse execute(HttpMethodName method, String resourcePath, Map<String, String> headers, Map<String,List<String>> parameters, InputStream content) {
7172
final ExecutionContext executionContext = buildExecutionContext();
7273

7374
DefaultRequest request = new DefaultRequest(API_GATEWAY_SERVICE_NAME);
@@ -76,7 +77,9 @@ private GenericApiGatewayResponse execute(HttpMethodName method, String resource
7677
request.setEndpoint(this.endpoint);
7778
request.setResourcePath(resourcePath);
7879
request.setHeaders(buildRequestHeaders(headers, apiKey));
79-
80+
if (parameters != null) {
81+
request.setParameters(parameters);
82+
}
8083
return this.client.execute(request, responseHandler, errorResponseHandler, executionContext).getAwsResponse();
8184
}
8285

src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayRequest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,25 @@
44

55
import java.io.IOException;
66
import java.io.InputStream;
7+
import java.util.List;
78
import java.util.Map;
89

910
public class GenericApiGatewayRequest {
11+
1012
private final HttpMethodName httpMethod;
1113
private final String resourcePath;
1214
private final InputStream body;
1315
private final Map<String, String> headers;
16+
private final Map<String, List<String>> parameters;
1417

1518
public GenericApiGatewayRequest(HttpMethodName httpMethod, String resourcePath,
16-
InputStream body, Map<String, String> headers) {
19+
InputStream body, Map<String, String> headers,
20+
Map<String, List<String>> parameters) {
1721
this.httpMethod = httpMethod;
1822
this.resourcePath = resourcePath;
1923
this.body = body;
2024
this.headers = headers;
25+
this.parameters = parameters;
2126
}
2227

2328
public HttpMethodName getHttpMethod() {
@@ -35,4 +40,8 @@ public InputStream getBody() {
3540
public Map<String, String> getHeaders() {
3641
return headers;
3742
}
43+
44+
public Map<String, List<String>> getParameters() {
45+
return parameters;
46+
}
3847
}

src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayRequestBuilder.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
import ca.ryangreen.apigateway.util.Validate;
55

66
import java.io.InputStream;
7+
import java.util.List;
78
import java.util.Map;
89

910
public class GenericApiGatewayRequestBuilder {
1011
private HttpMethodName httpMethod;
1112
private String resourcePath;
1213
private InputStream body;
1314
private Map<String, String> headers;
15+
private Map<String, List<String>> parameters;
1416

1517
public GenericApiGatewayRequestBuilder withHttpMethod(HttpMethodName name) {
1618
httpMethod = name;
@@ -32,9 +34,14 @@ public GenericApiGatewayRequestBuilder withHeaders(Map<String, String> headers)
3234
return this;
3335
}
3436

37+
public GenericApiGatewayRequestBuilder withParameters(Map<String,List<String>> parameters) {
38+
this.parameters = parameters;
39+
return this;
40+
}
41+
3542
public GenericApiGatewayRequest build() {
3643
Validate.notNull(httpMethod, "HTTP method");
3744
Validate.notEmpty(resourcePath, "Resource path");
38-
return new GenericApiGatewayRequest(httpMethod, resourcePath, body, headers);
45+
return new GenericApiGatewayRequest(httpMethod, resourcePath, body, headers, parameters);
3946
}
4047
}

src/main/java/ca/ryangreen/apigateway/generic/GenericApiGatewayResponse.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ public class GenericApiGatewayResponse {
1111

1212
public GenericApiGatewayResponse(HttpResponse httpResponse) throws IOException {
1313
this.httpResponse = httpResponse;
14-
this.body = IOUtils.toString(httpResponse.getContent());
14+
if (httpResponse.getContent()!= null) {
15+
this.body = IOUtils.toString(httpResponse.getContent());
16+
} else {
17+
this.body = null;
18+
}
1519
}
1620

1721
public HttpResponse getHttpResponse() {

src/test/java/ca/ryangreen/apigateway/generic/GenericApiGatewayClientTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import com.amazonaws.http.apache.client.impl.SdkHttpClient;
1010
import com.amazonaws.regions.Region;
1111
import com.amazonaws.regions.Regions;
12+
import java.util.Arrays;
13+
import java.util.List;
1214
import org.apache.http.HttpResponse;
1315
import org.apache.http.HttpVersion;
1416
import org.apache.http.client.methods.HttpUriRequest;
@@ -81,6 +83,33 @@ public void testExecute_happy() throws IOException {
8183
any(HttpContext.class));
8284
}
8385

86+
@Test
87+
public void testExecute_happy_parameters() throws IOException {
88+
Map<String, String> headers = new HashMap<>();
89+
headers.put("Account-Id", "fubar");
90+
headers.put("Content-Type", "application/json");
91+
Map<String,List<String>> parameters = new HashMap<>();
92+
parameters.put("MyParam", Arrays.asList("MyParamValue"));
93+
GenericApiGatewayResponse response = client.execute(
94+
new GenericApiGatewayRequestBuilder()
95+
.withBody(new ByteArrayInputStream("test request".getBytes()))
96+
.withHttpMethod(HttpMethodName.POST)
97+
.withHeaders(headers)
98+
.withParameters(parameters)
99+
.withResourcePath("/test/orders").build());
100+
101+
assertEquals("Wrong response body", "test payload", response.getBody());
102+
assertEquals("Wrong response status", 200, response.getHttpResponse().getStatusCode());
103+
104+
Mockito.verify(mockClient, times(1)).execute(argThat(new LambdaMatcher<>(
105+
x -> (x.getMethod().equals("POST")
106+
&& x.getFirstHeader("Account-Id").getValue().equals("fubar")
107+
&& x.getFirstHeader("x-api-key").getValue().equals("12345")
108+
&& x.getFirstHeader("Authorization").getValue().startsWith("AWS4")
109+
&& x.getURI().toString().equals("https://foobar.execute-api.us-east-1.amazonaws.com/test/orders?MyParam=MyParamValue")))),
110+
any(HttpContext.class));
111+
}
112+
84113
@Test
85114
public void testExecute_noApiKey_noCreds() throws IOException {
86115
client = new GenericApiGatewayClientBuilder()

0 commit comments

Comments
 (0)