This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
│ └── test_todos.py # unit tests for the todos router
│ └── test_health.py # unit tests for the health router
└── functional
├── __init__.py
├── conftest.py # pytest fixtures for the functional tests
└── test_main.py # functional tests for the main lambda handler
```
### Dependency injection
You can use `Depends()` to declare dependencies that are automatically resolved and injected into your route handlers. This provides type-safe, composable, and testable dependency injection.
Expand All
@@ -1389,10 +1420,36 @@ Dependencies can depend on other dependencies, forming a composable tree. Shared
Dependencies that need access to the current request can declare a parameter typed as `Request`. It will be injected automatically.
```python hl_lines="5-6 12 20"
The `Request` object provides:
* **`headers`**, **`query_parameters`**, **`body`**, **`json_body`**: common request data
* **`resolved_event`**: the full Powertools proxy event with all helpers, cookies, request context, and path
* **`context`**: shared resolver context (`app.context`) for bridging data between middleware and dependencies
Middleware and `Depends()` are **complementary patterns**. Use middleware for request interception (auth gates, redirects, response modification) and `Depends()` for typed data injection.
The bridge between them is `request.context`: middleware writes to `app.context`, and dependencies read from `request.context`:
| Inject typed, testable data into handlers | No | **Yes** |
| Compose a dependency tree with caching | No | **Yes** |
| Override dependencies in tests | No | **Yes**, via `dependency_overrides` |
#### Testing with dependency overrides
Use `dependency_overrides` to replace any dependency with a mock or stub during testing - no monkeypatching needed.
Expand All
@@ -1407,37 +1464,6 @@ Use `dependency_overrides` to replace any dependency with a mock or stub during
???+ info "`append_context` vs `Depends()`"
`append_context` remains available for backward compatibility. `Depends()` is recommended for new code because it provides type safety, IDE autocomplete, composable dependency trees, and `dependency_overrides` for testing.
#### Sample layout
This is a sample project layout for a monolithic function with routes split in different files (`/todos`, `/health`).
│ └── test_todos.py # unit tests for the todos router
│ └── test_health.py # unit tests for the health router
└── functional
├── __init__.py
├── conftest.py # pytest fixtures for the functional tests
└── test_main.py # functional tests for the main lambda handler
```
### Considerations
This utility is optimized for fast startup, minimal feature set, and to quickly on-board customers familiar with frameworks like Flask — it's not meant to be a fully fledged framework.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
feat(event_handler): enrich request object #8153
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
Uh oh!
There was an error while loading. Please reload this page.
feat(event_handler): enrich request object #8153
Filter by extension
Viewed files
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.