Skip to content

Commit ddb544e

Browse files
Merge branch 'develop' into logger-current-keys
2 parents 7c3ffd0 + c2d89b3 commit ddb544e

File tree

6 files changed

+88
-42
lines changed

6 files changed

+88
-42
lines changed

docs/core/event_handler/api_gateway.md

+7
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,13 @@ You can use the `Response` class to have full control over the response. For exa
874874
--8<-- "examples/event_handler_rest/src/fine_grained_responses_output.json"
875875
```
876876

877+
???- note "Using `Response` with data validation?"
878+
When using the [data validation](#data-validation) feature with `enable_validation=True`, you must specify the concrete type for the `Response` class. This allows the validation middleware to infer the underlying type and perform validation correctly.
879+
880+
```python hl_lines="8 26 32"
881+
--8<-- "examples/event_handler_rest/src/data_validation_fine_grained_response.py"
882+
```
883+
877884
### Compress
878885

879886
You can compress with gzip and base64 encode your responses via `compress` parameter. You have the option to pass the `compress` parameter when working with a specific route or using the Response object.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from http import HTTPStatus
2+
from typing import Optional
3+
4+
import requests
5+
from pydantic import BaseModel, Field
6+
7+
from aws_lambda_powertools import Logger, Tracer
8+
from aws_lambda_powertools.event_handler import APIGatewayRestResolver, Response, content_types
9+
from aws_lambda_powertools.logging import correlation_paths
10+
from aws_lambda_powertools.utilities.typing import LambdaContext
11+
12+
tracer = Tracer()
13+
logger = Logger()
14+
app = APIGatewayRestResolver(enable_validation=True)
15+
16+
17+
class Todo(BaseModel):
18+
userId: int
19+
id_: Optional[int] = Field(alias="id", default=None)
20+
title: str
21+
completed: bool
22+
23+
24+
@app.get("/todos/<todo_id>")
25+
@tracer.capture_method
26+
def get_todo_by_id(todo_id: int) -> Response[Todo]:
27+
todo = requests.get(f"https://jsonplaceholder.typicode.com/todos/{todo_id}")
28+
todo.raise_for_status()
29+
return Response(
30+
status_code=HTTPStatus.OK.value,
31+
content_type=content_types.APPLICATION_JSON,
32+
body=todo.json(),
33+
)
34+
35+
36+
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
37+
@tracer.capture_lambda_handler
38+
def lambda_handler(event: dict, context: LambdaContext) -> dict:
39+
return app.resolve(event, context)

package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "aws-lambda-powertools-python-e2e",
33
"version": "1.0.0",
44
"devDependencies": {
5-
"aws-cdk": "^2.136.0"
5+
"aws-cdk": "^2.137.0"
66
},
77
"dependencies": {
88
"package-lock.json": "^1.0.0"

poetry.lock

+33-33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,16 @@ xenon = "^0.9.1"
6565
mkdocs-git-revision-date-plugin = "^0.3.2"
6666
mike = "^1.1.2"
6767
pytest-xdist = "^3.5.0"
68-
aws-cdk-lib = "^2.136.0"
68+
aws-cdk-lib = "^2.136.1"
6969
"aws-cdk.aws-apigatewayv2-alpha" = "^2.38.1-alpha.0"
7070
"aws-cdk.aws-apigatewayv2-integrations-alpha" = "^2.38.1-alpha.0"
7171
"aws-cdk.aws-apigatewayv2-authorizers-alpha" = "^2.38.1-alpha.0"
7272
"aws-cdk.aws-lambda-python-alpha" = "^2.136.0a0"
73-
"cdklabs.generative-ai-cdk-constructs" = "^0.1.110"
73+
"cdklabs.generative-ai-cdk-constructs" = "^0.1.112"
7474
pytest-benchmark = "^4.0.0"
7575
mypy-boto3-appconfig = "^1.34.58"
7676
mypy-boto3-cloudformation = "^1.34.77"
77-
mypy-boto3-cloudwatch = "^1.34.75"
77+
mypy-boto3-cloudwatch = "^1.34.83"
7878
mypy-boto3-dynamodb = "^1.34.67"
7979
mypy-boto3-lambda = "^1.34.77"
8080
mypy-boto3-logs = "^1.34.66"
@@ -115,7 +115,7 @@ mypy = "^1.1.1"
115115
types-python-dateutil = "^2.8.19.6"
116116
httpx = ">=0.23.3,<0.28.0"
117117
sentry-sdk = "^1.22.2"
118-
ruff = ">=0.0.272,<0.3.6"
118+
ruff = ">=0.0.272,<0.3.8"
119119
retry2 = "^0.9.5"
120120
pytest-socket = ">=0.6,<0.8"
121121
types-redis = "^4.6.0.7"

0 commit comments

Comments
 (0)