Skip to content

docs(event_handler): improve compress example using Response class #2426

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/core/event_handler/api_gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ For convenience, these are the default values when using `CORSConfig` to enable
If you need to allow multiple origins, pass the additional origins using the `extra_origins` key.

| Key | Value | Note |
|----------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| -------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **[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 |
| **[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` |
| **[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 |
Expand Down Expand Up @@ -367,7 +367,7 @@ You can compress with gzip and base64 encode your responses via `compress` param

=== "compressing_responses.py"

```python hl_lines="14"
```python hl_lines="17 27"
--8<-- "examples/event_handler_rest/src/compressing_responses.py"
```

Expand Down Expand Up @@ -486,7 +486,7 @@ When necessary, you can set a prefix when including a router object. This means
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.

| Router | Resolver | `current_event` type |
|-------------------------|---------------------------|------------------------|
| ----------------------- | ------------------------- | ---------------------- |
| APIGatewayRouter | APIGatewayRestResolver | APIGatewayProxyEvent |
| APIGatewayHttpRouter | APIGatewayHttpResolver | APIGatewayProxyEventV2 |
| ALBRouter | ALBResolver | ALBEvent |
Expand Down
18 changes: 15 additions & 3 deletions examples/event_handler_rest/src/compressing_responses.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import requests
from requests import Response

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.event_handler import (
APIGatewayRestResolver,
Response,
content_types,
)
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.utilities.typing import LambdaContext

Expand All @@ -14,13 +17,22 @@
@app.get("/todos", compress=True)
@tracer.capture_method
def get_todos():
todos: Response = requests.get("https://jsonplaceholder.typicode.com/todos")
todos: requests.Response = requests.get("https://jsonplaceholder.typicode.com/todos")
todos.raise_for_status()

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


@app.get("/todos/<todo_id>", compress=True)
@tracer.capture_method
def get_todo_by_id(todo_id: str): # same example using Response class
todos: requests.Response = requests.get(f"https://jsonplaceholder.typicode.com/todos/{todo_id}")
todos.raise_for_status()

return Response(status_code=200, content_type=content_types.APPLICATION_JSON, body=todos.json())


# You can continue to use other utilities just as before
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
@tracer.capture_lambda_handler
Expand Down