secretsanta/apps/http/sls/app.py
2023-12-09 11:15:39 +01:00

43 lines
1.4 KiB
Python

import json
import anyio
from secretsanta.domain import Group
from secretsanta.repo import S3Repo
from secretsanta.service import (
CreateGroupService,
GetGroupParticipantsService,
GetPairService,
)
from secretsanta.settings import get_config
config = get_config()
repo = S3Repo()
def get_participants(event, _):
group_uuid = event["pathParameters"]["group_uuid"]
service = GetGroupParticipantsService(repo)
participants = anyio.run(service.run, group_uuid)
return {"statusCode": 200, "body": json.dumps(participants)}
def get_pair(event, _):
group_uuid = event["pathParameters"]["group_uuid"]
participant = event["pathParameters"]["participant"]
query_params = event["queryStringParameters"] or {}
response_type = query_params.get("type", "plain")
service = GetPairService(repo)
pair = anyio.run(service.run, group_uuid, participant)
if response_type == "json":
return {"statusCode": 200, "body": json.dumps({"pair": pair})}
return {"statusCode": 200, "body": pair, "headers": {"Content-Type": "text/plain"}}
def create_group(event, _):
group_uuid = event["pathParameters"]["group_uuid"]
body = json.loads(event["body"])
service = CreateGroupService(repo)
participants = anyio.run(service.run, Group(uuid=group_uuid, **body))
return {"statusCode": 200, "body": json.dumps(participants)}