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

REST API middleware #1175

Closed
marcioemiranda opened this issue Apr 27, 2021 · 12 comments
Closed

REST API middleware #1175

marcioemiranda opened this issue Apr 27, 2021 · 12 comments
Labels
feature-request feature request help wanted Could use a second pair of eyes/hands

Comments

@marcioemiranda
Copy link

Is your feature request related to a problem? Please describe.
In previous projects working with API Proxy and Lambda I found myself reusing a lot of repetitive code by copy and pasting. Tasks such as:

  • routing different paths to functions
  • validating a body against a model
  • HTTP status code handling, like 4XX for validation errors, 5XX for generic errors, 2XX for success
  • CORS handling
  • Handling common set of errors like ResourceNotFound (404), InvalidRequest (400)

This HTTP boilerplate code should be more streamlined to the developer.

PS: I am willing to take this feature with your guidance.

Describe the solution you'd like
A middleware decorator abstracting many of the HTTP boilerplate code.

Describe alternatives you've considered
The before part of the middleware could handle model validation.

The middleware could assume a default correlation id based on the API request id.

The routing part is being handled by a different ticket #325. Maybe the functionality described here could be part of the same class. It could also handle an invalid path (not able to map to a handler function)
If a model is associated with a resource, the routing functionality could provide the validated model to the function as an extra param (or instead of the usual lambda handler params).
PS: Usually we map one resource to a lambda function with different methods. For example a CRUD of an Order resource with GET, POST, PUT and DELETE all go to the same function. It is a general best practice to have lambda functions with a single responsibility, but this could also lead to hundreds of lambda functions. We try to find a balance between LambdaLyth and too many Lambda functions.

The exception handler could expect a set of predefined error classes, like InvalidRequest, ResourceNotFound, etc. These errors might be thrown by the lambda handler implementation (Resource not found) or by the middleware per se (Invalid request, model validation). The errors are mapped to HTTP requests.

The error handlers can add logs with additional metadata like the error name, status code, correlation_id, stack trace and any other data that could be relevant in CW Log Insights or other analytical process.

Any HTTP boilerplate like headers required by CORS are added by the code after execution or error handling. A CORS handling flag could be passed to the decorator.

Additional context

@michaelbrewer
Copy link
Contributor

Some of these have been covered in a recent PR:

@marcioemiranda
Copy link
Author

marcioemiranda commented Apr 28, 2021 via email

@michaelbrewer
Copy link
Contributor

@marcioemiranda - sure. I think we need to stabilize the api and UX for it, and then we can see how easy it would be to add the missing features. :)

@michaelbrewer
Copy link
Contributor

@marcioemiranda v1.15.0 is out now, maybe play with this and see what is missing. From my initial discussions we might want to add:

  • A standard set of exceptions (InternalServerError)
  • A standard set of responses (NotFoundResponse)

@heitorlessa heitorlessa transferred this issue from aws-powertools/powertools-lambda-python May 13, 2021
@michaelbrewer
Copy link
Contributor

@heitorlessa should i make a PR with some of the standard exception types like InternalServerError.

@heitorlessa
Copy link
Contributor

heitorlessa commented Jun 17, 2021 via email

@michaelbrewer
Copy link
Contributor

@heitorlessa i added a PR for the exception stuff

@marcioemiranda
Copy link
Author

marcioemiranda commented Jul 7, 2021 via email

@michaelbrewer
Copy link
Contributor

Hello Michael,

I wonder if the APIGatewayResolver supports the validation feature.
For example, validate a request body against a schema and, if
invalid, throw a 400 error.
If not I assume one would have to grab the request body from
app.current_event and call the validation standalone function. Am I
correct?

Em seg., 5 de jul. de 2021 às 02:02, Michael Brewer <
@.***> escreveu:

@heitorlessa https://github.com/heitorlessa i added a PR for the
exception stuff


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1175>,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AG62T2OE6VVON72SBIEOMZ3TWE4GRANCNFSM442RID2A
.

--

Atenciosamente,

Marcio Miranda | Gerente de Desenvolvimento | @.*** |
+55 21 3535.9137

DRUID | Rio de Janeiro - RJ
CEO Office | Avenida João Cabral de Mello Neto, Nº 850 - Bloco III - 4º
Andar - Sala 302 | Barra da Tijuca | CEP 22.775-057
Tel - 55 21 3535.9136 | Fax - 55 21 3535.9145

That's right. You can cool the standalone validate util. Or use the decorator

@heitorlessa
Copy link
Contributor

Update: all requested features launched except the Middleware piece tracked here: #953

We'll revisit it after we end our pause

@heitorlessa heitorlessa transferred this issue from aws-powertools/powertools-lambda Apr 28, 2022
@heitorlessa heitorlessa added the help wanted Could use a second pair of eyes/hands label Aug 1, 2022
@heitorlessa
Copy link
Contributor

Closing in favour of #953 as we'll resume design discussion there

@github-actions
Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request feature request help wanted Could use a second pair of eyes/hands
Projects
None yet
Development

No branches or pull requests

3 participants