forked from aws-powertools/powertools-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicro_function_all_users_route.py
56 lines (44 loc) · 1.33 KB
/
micro_function_all_users_route.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import json
from dataclasses import dataclass
from http import HTTPStatus
from aws_lambda_powertools import Logger
from aws_lambda_powertools.event_handler import APIGatewayRestResolver, Response
from aws_lambda_powertools.utilities.typing import LambdaContext
logger = Logger()
# This would likely be a db lookup
users = [
{
"user_id": "b0b2a5bf-ee1e-4c5e-9a86-91074052739e",
"email": "[email protected]",
"active": True,
},
{
"user_id": "3a9df6b1-938c-4e80-bd4a-0c966f4b1c1e",
"email": "[email protected]",
"active": False,
},
{
"user_id": "aa0d3d09-9cb9-42b9-9e63-1fb17ea52981",
"email": "[email protected]",
"active": True,
},
]
@dataclass
class User:
user_id: str
email: str
active: bool
app = APIGatewayRestResolver()
@app.get("/users")
def all_active_users():
"""HTTP Response for all active users"""
all_users = [User(**user) for user in users]
all_active_users = [user.__dict__ for user in all_users if user.active]
return Response(
status_code=HTTPStatus.OK.value,
content_type="application/json",
body=json.dumps(all_active_users),
)
@logger.inject_lambda_context()
def lambda_handler(event: dict, context: LambdaContext) -> dict:
return app.resolve(event, context)