secretsanta/apps/http/chalice/app.py
2023-12-08 17:00:22 +01:00

52 lines
1.5 KiB
Python

import anyio
from chalice import Chalice
from secretsanta.domain import Group
from secretsanta.repo import S3Repo
from secretsanta.service import (
GetGroupParticipantsService,
CreateGroupService,
GetPairService,
)
from secretsanta.settings import get_config
config = get_config()
app = Chalice(app_name="secretsanta", debug=config.environment == "local")
repo = S3Repo()
def _participants2url(participants: list[str], group_uuid: str) -> list[str]:
return [
f"{config.server_url}/api/v1/groups/{group_uuid}/pair/{participant_name}"
for participant_name in participants
]
@app.route("/api/v1/groups/{group_uuid}")
def get_participants(group_uuid: str):
service = GetGroupParticipantsService(repo)
return anyio.run(service.run, group_uuid)
@app.route("/api/v1/groups/{group_uuid}", methods=["PUT"])
def create_group(group_uuid: str):
request = app.current_request
body = request.json_body
service = CreateGroupService(repo)
participants = anyio.run(service.run, Group(uuid=group_uuid, **body)).participants
return _participants2url(participants, group_uuid)
@app.route("/api/v1/groups/{group_uuid}/pair/{participant}")
def get_pair(group_uuid: str, participant: str):
service = GetPairService(repo)
pair = anyio.run(service.run, group_uuid, participant)
return {"pair": pair}
if config.environment == "local":
@app.route("/introspect")
def introspect():
return app.current_request.to_dict()