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

Conversation

mbfreder
Copy link

@mbfreder mbfreder commented Jul 7, 2023

Issue #, if available:

Description of changes:

  • Added common response class for ApiGateway and Alb.
  • Added soem utility methods to work with request values such as headers and query string parameters.
  • Added an interface to abstract APIGWY and ALB request classes. (This would reduce duplicate code in serverless-java-container).

Target (OCI, Managed Runtime, both):

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@mbfreder mbfreder changed the title Common response Common response class for ApiGateway and ALB Jul 7, 2023
@smirnoal smirnoal requested a review from andclt July 17, 2023 16:35
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.

@mbfreder mbfreder requested a review from msailes September 12, 2023 03:49
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.

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.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Has this class come from aws-serverless-java-container? Why do you think it belongs in the events lib?

Copy link
Author

Choose a reason for hiding this comment

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

This was added based on this conversation with Dennis. The class does not come from SJC, the the methods inside do. We thought by bringing theses here, other customers might benefit from them.

/**
* Parses a header value using the default value separator "," and qualifier separator ";".
* @param headerValue The value to be parsed
* @return A list of SimpleMapEntry objects with all of the possible values for the header.
Copy link
Contributor

Choose a reason for hiding this comment

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

what is SimpleMapEntry? I see the method is returning List of HeaderValue

Copy link
Author

Choose a reason for hiding this comment

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

Old comment that I forgot to update. Fixed.


// sort list by preference
values.sort((HeaderValue first, HeaderValue second) -> {
if ((first.getPriority() - second.getPriority()) < .001f) {
Copy link
Contributor

Choose a reason for hiding this comment

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

where is this magic number coming from?

Copy link
Author

Choose a reason for hiding this comment

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

That's an epsilon. Instead of comparing 2 float values for equality (with ==), we check whether their difference is within some error bounds or epsilon value, like this: bool isEqual = fabs(f1 – f2) <= epsilon; This calculation allows us to express the concept of two floats being close enough that we want to consider them to be equal. Ref

return null;
}

public static String getFirstQueryParamValue(Map<String, List<String>> queryString, String key, boolean isCaseSensitive) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd avoid boolean parameter in this method. Looks like it is easy to split into 2 different methods

Copy link
Author

Choose a reason for hiding this comment

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

Fixed.

public HeaderValue() {
attributes = new HashMap<>();
}

Copy link
Contributor

Choose a reason for hiding this comment

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

NIT: the file doesn't look properly formatted, 2 blank lines between methods

Copy link
Author

Choose a reason for hiding this comment

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

Fixed.

/**
* Class that represents a header value.
*/
public static class HeaderValue {
Copy link
Contributor

Choose a reason for hiding this comment

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

if it ever end up in a HashMap, do you think it makes sense to implement equals and hashCode?

Copy link
Author

Choose a reason for hiding this comment

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

I can't think of a use case for that.

* This class exposes some utility methods to work with request values such as headers
* and query string parameters.
*/
public class HeaderUtils {
Copy link
Contributor

Choose a reason for hiding this comment

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

This utility class has a lot helper methods - worth covering basic scenarios with unit tests?

Copy link
Author

Choose a reason for hiding this comment

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

Added tests.

@mbfreder mbfreder requested a review from smirnoal September 30, 2023 07:04
/**
* Interface to abstract APIGWY and ALB request classes, reduce complexity and increase efficiency.
*/
public interface LambdaRequestEvent {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can this be renamed to something like HttpRequestEvent?

Copy link
Author

Choose a reason for hiding this comment

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

Done.

import com.amazonaws.services.lambda.runtime.events.apigateway.RequestSource;

/**
* Interface to abstract APIGWY and ALB request classes, reduce complexity and increase efficiency.
Copy link
Collaborator

Choose a reason for hiding this comment

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

"A common interface shared by event sources which produce HTTP as JSON"

Copy link
Author

Choose a reason for hiding this comment

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

Done.

Copy link
Collaborator

@msailes msailes left a comment

Choose a reason for hiding this comment

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

Looks good to me

@smirnoal smirnoal merged commit 6cec207 into aws:events-v4-serialization-v2 Oct 4, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants