Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
target
*.class

# IDE files
# Intellij
.idea/
*.iml
*.iws
*~

# Mobile Tools for Java (J2ME)
.mtj.tmp/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<String, String> headers, InputStream content) {
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);
Expand All @@ -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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this null check necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your unit tests fail without it.

request.setParameters(parameters);
}
return this.client.execute(request, responseHandler, errorResponseHandler, executionContext).getAwsResponse();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> headers;
private final Map<String, List<String>> parameters;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be called queryStringParameters?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parameters seemed to be in the same flavor of 'headers', 'body' etc. Also it is setParameters() in the DefaultRequest


public GenericApiGatewayRequest(HttpMethodName httpMethod, String resourcePath,
InputStream body, Map<String, String> headers) {
InputStream body, Map<String, String> headers,
Map<String, List<String>> parameters) {
this.httpMethod = httpMethod;
this.resourcePath = resourcePath;
this.body = body;
this.headers = headers;
this.parameters = parameters;
}

public HttpMethodName getHttpMethod() {
Expand All @@ -35,4 +40,8 @@ public InputStream getBody() {
public Map<String, String> getHeaders() {
return headers;
}

public Map<String, List<String>> getParameters() {
return parameters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import ca.ryangreen.apigateway.util.Validate;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

public class GenericApiGatewayRequestBuilder {
private HttpMethodName httpMethod;
private String resourcePath;
private InputStream body;
private Map<String, String> headers;
private Map<String, List<String>> parameters;

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

public GenericApiGatewayRequestBuilder withParameters(Map<String,List<String>> parameters) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be withQueryStringParameters?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above

this.parameters = parameters;
return this;
}

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this else block required? Isn't body null by default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because it is final, there are "Error:(17, 5) java: variable body might not have been initialized" without it

}
}

public HttpResponse getHttpResponse() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,6 +83,33 @@ public void testExecute_happy() throws IOException {
any(HttpContext.class));
}

@Test
public void testExecute_happy_parameters() throws IOException {
Map<String, String> headers = new HashMap<>();
headers.put("Account-Id", "fubar");
headers.put("Content-Type", "application/json");
Map<String,List<String>> 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()
Expand Down