feat(event_handler): add Request object for middleware access to resolved route and args#8036
Conversation
…lved route and args Introduce a Request class that provides structured access to the resolved route pattern, path parameters, HTTP method, headers, query parameters, and body. Available via app.request in middleware and via type-annotation injection in route handlers. Closes aws-powertools#7992, aws-powertools#4609
…YPE_CHECKING block
|
leandrodamascena
left a comment
There was a problem hiding this comment.
Hey @oyiz-michael, thanks for this PR! This is a great addition. The Request object is much cleaner than having middleware dig into app.context or deal with API Gateway's raw {proxy+} path parameters. Really nice work on the tests too, you covered a lot of ground.
I went through the code and made a few improvements on top of your work.
I'll open a follow-up issue for documentation updates after we merge this. The middleware docs should show app.request as the recommended way to access resolved route info.
Thanks again for the contribution!
APPROVED!
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #8036 +/- ##
===========================================
- Coverage 96.65% 96.63% -0.03%
===========================================
Files 282 283 +1
Lines 13798 13862 +64
Branches 1103 1111 +8
===========================================
+ Hits 13336 13395 +59
- Misses 339 342 +3
- Partials 123 125 +2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
7323c05
into
aws-powertools:develop



Issue number: closes #7992, closes #4609
Summary
Changes
Requestclass (aws_lambda_powertools/event_handler/request.py) that provides structured access to the resolved route pattern, path parameters, HTTP method, headers, query parameters, and bodyapp.requestproperty onBaseRouter— available inside middleware after route resolutionRequestinjection into route handlers via type annotation (declare a parameter typed asRequestand it gets injected automatically)_registered_api_adapterto detectRequest-annotated handler parameters and inject the object (with per-route caching to avoid repeated signature inspection)get_dependant()to skipRequest-typed parameters so they don't appear in generated schemasRequestfromaws_lambda_powertools.event_handlerUser experience
Before: Middleware had no access to the resolved route pattern or extracted path parameters.
app.current_event.path_parametersonly contained the raw API Gateway parameters (e.g.{"proxy": "..."}for{proxy+}routes), not the Powertools-resolved values.After: Middleware and route handlers can access a
Requestobject with the resolved route and parameters:By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.