Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Common response class for ApiGateway and ALB #427

Merged
merged 9 commits into from
Oct 4, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

package com.amazonaws.services.lambda.runtime.events;

import com.amazonaws.services.lambda.runtime.events.helper.AwsLambdaRequestEvent;
import com.amazonaws.services.lambda.runtime.events.helper.RequestSource;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -34,7 +37,13 @@
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
public class ApplicationLoadBalancerRequestEvent implements Serializable {
public class ApplicationLoadBalancerRequestEvent implements Serializable, AwsLambdaRequestEvent {

@Override
@JsonIgnore
public RequestSource getRequestSource() {
return RequestSource.ALB;
}

@Data
@Builder(setterPrefix = "with")
Expand Down
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think we need an extra response object. I think we need to pick one, and if they're all the same, remove the others.

Copy link
Author

@mbfreder mbfreder Sep 7, 2023

Choose a reason for hiding this comment

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

I think customers should still be able to use the current response classes. AwsProxyResponseEvent is just meant simplify things; It would be very helpful for the Serverless-java-container use-case, but I'm not sure everybody would want that. That's why I kept the current classes.

Copy link
Collaborator

@msailes msailes Sep 12, 2023

Choose a reason for hiding this comment

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

Is there an existing class which already covers this response?

Copy link
Author

Choose a reason for hiding this comment

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

No. This response class is similar to the one we currently have on aws-serverless-java-container. Having it here allows us to maintain a single response class on the serverless java container, which will remove a lot of duplicated code.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.amazonaws.services.lambda.runtime.events;


import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;

/**
Common response class for APIGateway and ALB
*/
@NoArgsConstructor
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class AwsProxyResponseEvent {
private static final long serialVersionUID = 2263167344670024138L;

private Integer statusCode;

private String statusDescription;

private List<String> cookies;

private Map<String, String> headers;

private Map<String, List<String>> multiValueHeaders;

private String body;

private Boolean isBase64Encoded;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

package com.amazonaws.services.lambda.runtime.events.apigateway;

import com.amazonaws.services.lambda.runtime.events.helper.AwsLambdaRequestEvent;
import com.amazonaws.services.lambda.runtime.events.helper.RequestSource;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -30,7 +33,7 @@
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
public class APIGatewayProxyRequestEvent implements Serializable {
public class APIGatewayProxyRequestEvent implements Serializable, AwsLambdaRequestEvent {

private static final long serialVersionUID = 4189228800688527467L;

Expand All @@ -47,6 +50,12 @@ public class APIGatewayProxyRequestEvent implements Serializable {
private String body;
private Boolean isBase64Encoded;

@Override
@JsonIgnore
public RequestSource getRequestSource() {
return RequestSource.API_GATEWAY_REST;
}

/**
* class that represents proxy request context
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

package com.amazonaws.services.lambda.runtime.events.apigateway;

import com.amazonaws.services.lambda.runtime.events.helper.AwsLambdaRequestEvent;
import com.amazonaws.services.lambda.runtime.events.helper.RequestSource;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -30,7 +33,7 @@
@Builder(setterPrefix = "with")
@Data
@NoArgsConstructor
public class APIGatewayV2HTTPEvent {
public class APIGatewayV2HTTPEvent implements AwsLambdaRequestEvent {

private String version;
private String routeKey;
Expand All @@ -45,6 +48,12 @@ public class APIGatewayV2HTTPEvent {
private boolean isBase64Encoded;
private RequestContext requestContext;

@Override
@JsonIgnore
public RequestSource getRequestSource() {
return RequestSource.API_GATEWAY_HTTP;
}

@AllArgsConstructor
@Builder(setterPrefix = "with")
@Data
Expand Down
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure which package this should be in, but it doesn't feel like it should be in helper.

Copy link
Author

Choose a reason for hiding this comment

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

Right. Moving it to the events folder.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.amazonaws.services.lambda.runtime.events.helper;

/**
* Interface to abstract APIGWY and ALB request classes, reduce complexity and increase efficiency.
*/
public interface AwsLambdaRequestEvent {
RequestSource getRequestSource();
}
Loading