forked from aws-powertools/powertools-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfine_grained_responses.py
38 lines (29 loc) · 1.22 KB
/
fine_grained_responses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from http import HTTPStatus
from uuid import uuid4
import requests
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import APIGatewayRestResolver, Response, content_types
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.shared.cookies import Cookie
from aws_lambda_powertools.utilities.typing import LambdaContext
tracer = Tracer()
logger = Logger()
app = APIGatewayRestResolver()
@app.get("/todos")
@tracer.capture_method
def get_todos():
todos: requests.Response = requests.get("https://jsonplaceholder.typicode.com/todos")
todos.raise_for_status()
custom_headers = {"X-Transaction-Id": [f"{uuid4()}"]}
return Response(
status_code=HTTPStatus.OK.value, # 200
content_type=content_types.APPLICATION_JSON,
body=todos.json()[:10],
headers=custom_headers,
cookies=[Cookie(name="session_id", value="12345", secure=True)],
)
# 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
def lambda_handler(event: dict, context: LambdaContext) -> dict:
return app.resolve(event, context)