Skip to content

Commit 831e875

Browse files
heitorlessarafaelgsr
authored andcommitted
docs(event_handler): improve compress example using Response class (aws-powertools#2426)
1 parent f867667 commit 831e875

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

docs/core/event_handler/api_gateway.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ For convenience, these are the default values when using `CORSConfig` to enable
327327
If you need to allow multiple origins, pass the additional origins using the `extra_origins` key.
328328

329329
| Key | Value | Note |
330-
|----------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
330+
| -------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
331331
| **[allow_origin](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin){target="_blank"}**: `str` | `*` | Only use the default value for development. **Never use `*` for production** unless your use case requires it |
332332
| **[extra_origins](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin){target="_blank"}**: `List[str]` | `[]` | Additional origins to be allowed, in addition to the one specified in `allow_origin` |
333333
| **[allow_headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers){target="_blank"}**: `List[str]` | `[Authorization, Content-Type, X-Amz-Date, X-Api-Key, X-Amz-Security-Token]` | Additional headers will be appended to the default list for your convenience |
@@ -367,7 +367,7 @@ You can compress with gzip and base64 encode your responses via `compress` param
367367

368368
=== "compressing_responses.py"
369369

370-
```python hl_lines="14"
370+
```python hl_lines="17 27"
371371
--8<-- "examples/event_handler_rest/src/compressing_responses.py"
372372
```
373373

@@ -486,7 +486,7 @@ When necessary, you can set a prefix when including a router object. This means
486486
You can use specialized router classes according to the type of event that you are resolving. This way you'll get type hints from your IDE as you access the `current_event` property.
487487

488488
| Router | Resolver | `current_event` type |
489-
|-------------------------|---------------------------|------------------------|
489+
| ----------------------- | ------------------------- | ---------------------- |
490490
| APIGatewayRouter | APIGatewayRestResolver | APIGatewayProxyEvent |
491491
| APIGatewayHttpRouter | APIGatewayHttpResolver | APIGatewayProxyEventV2 |
492492
| ALBRouter | ALBResolver | ALBEvent |

examples/event_handler_rest/src/compressing_responses.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import requests
2-
from requests import Response
32

43
from aws_lambda_powertools import Logger, Tracer
5-
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
4+
from aws_lambda_powertools.event_handler import (
5+
APIGatewayRestResolver,
6+
Response,
7+
content_types,
8+
)
69
from aws_lambda_powertools.logging import correlation_paths
710
from aws_lambda_powertools.utilities.typing import LambdaContext
811

@@ -14,13 +17,22 @@
1417
@app.get("/todos", compress=True)
1518
@tracer.capture_method
1619
def get_todos():
17-
todos: Response = requests.get("https://jsonplaceholder.typicode.com/todos")
20+
todos: requests.Response = requests.get("https://jsonplaceholder.typicode.com/todos")
1821
todos.raise_for_status()
1922

2023
# for brevity, we'll limit to the first 10 only
2124
return {"todos": todos.json()[:10]}
2225

2326

27+
@app.get("/todos/<todo_id>", compress=True)
28+
@tracer.capture_method
29+
def get_todo_by_id(todo_id: str): # same example using Response class
30+
todos: requests.Response = requests.get(f"https://jsonplaceholder.typicode.com/todos/{todo_id}")
31+
todos.raise_for_status()
32+
33+
return Response(status_code=200, content_type=content_types.APPLICATION_JSON, body=todos.json())
34+
35+
2436
# You can continue to use other utilities just as before
2537
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
2638
@tracer.capture_lambda_handler

0 commit comments

Comments
 (0)