-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
40 lines (35 loc) · 1.26 KB
/
lambda_function.py
File metadata and controls
40 lines (35 loc) · 1.26 KB
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
import json
import logging
from typing import Any, Dict
from aws_lambda_typing.context import Context
from aws_lambda_typing.events import APIGatewayProxyEventV1
from my_lib.calculator import Calculator
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def handler(event: APIGatewayProxyEventV1, context: Context) -> Dict[str, Any]:
event.setdefault("body", "{}")
body: Dict[str, str] = json.loads(event.get("body"))
body.setdefault("operation", "none")
body.setdefault("a", "0")
body.setdefault("b", "0")
logger.info(f"operation={body.get('operation')}, a={body.get('a')}, b={body.get('b')}")
operation = body.get("operation")
a = int(body.get("a"))
b = int(body.get("b"))
calculator = Calculator()
try:
match operation:
case "add":
result = calculator.add(a, b)
case "divide":
result = calculator.divide(a, b)
case _:
raise ValueError(f"Invalid operation: {operation}")
except ValueError as e:
return {"statusCode": 400, "body": str(e)}
return {
"statusCode": 200,
"body": json.dumps({"result": result}),
"headers": {"Content-Type": "application/json"},
"isBase64Encoded": False,
}