From 187edac137d922e6159df20505b0a93717a329bf Mon Sep 17 00:00:00 2001 From: heitorlessa Date: Thu, 8 Jun 2023 17:23:20 +0200 Subject: [PATCH] docs(event_handler): improve compress example using Response class --- docs/core/event_handler/api_gateway.md | 6 +++--- .../src/compressing_responses.py | 18 +++++++++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/docs/core/event_handler/api_gateway.md b/docs/core/event_handler/api_gateway.md index cfc6f4125d2..956b7cab6bd 100644 --- a/docs/core/event_handler/api_gateway.md +++ b/docs/core/event_handler/api_gateway.md @@ -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 | @@ -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" ``` @@ -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 | diff --git a/examples/event_handler_rest/src/compressing_responses.py b/examples/event_handler_rest/src/compressing_responses.py index 1af4b9a58b2..52369c59cca 100644 --- a/examples/event_handler_rest/src/compressing_responses.py +++ b/examples/event_handler_rest/src/compressing_responses.py @@ -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 @@ -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/", 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