-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
88 lines (69 loc) · 2.9 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import os
from fastapi import FastAPI, Body, Depends, Header
from redis.client import Redis
from starlette.responses import JSONResponse
import bootstrap
from request.handlers.game_request_handlers import (
GameStateUpdateHandler,
GameCreateHandler,
GameGetHandler,
GamesGetHandler,
)
from request.handlers.player_request_handlers import PlayerRequestHandler
app = FastAPI()
@app.get("/api/players/{player_id}")
def get_player(
player_id: str,
request_handler: PlayerRequestHandler = Depends(bootstrap.player_request_handler),
) -> JSONResponse:
status_code, response_data = request_handler.handle_request(player_id)
return JSONResponse(content=response_data, status_code=status_code)
@app.get("/api/players")
def get_players(
request_handler: PlayerRequestHandler = Depends(bootstrap.player_request_handler),
) -> JSONResponse:
status_code, response_data = request_handler.handle_request(None)
return JSONResponse(content=response_data, status_code=status_code)
@app.get("/api/players/{player_id}/games")
def get_player_games(
player_id: str,
request_handler: GamesGetHandler = Depends(bootstrap.games_get_handler),
) -> JSONResponse:
status_code, response_data = request_handler.handle_request(player_id)
return JSONResponse(content=response_data, status_code=status_code)
@app.post("/api/players")
def create_player(
data: dict = Body(),
request_handler: PlayerRequestHandler = Depends(bootstrap.player_request_handler),
) -> JSONResponse:
status_code, response_data = request_handler.handle_request(data)
return JSONResponse(content=response_data, status_code=status_code)
@app.get("/api/games")
def get_games(
request_handler: GamesGetHandler = Depends(bootstrap.games_get_handler),
) -> JSONResponse:
status_code, response_data = request_handler.handle_request(None)
return JSONResponse(content=response_data, status_code=status_code)
@app.get("/api/games/{game_id}")
def get_game(
game_id: str,
request_handler: GameGetHandler = Depends(bootstrap.game_get_handler),
) -> JSONResponse:
status_code, response_data = request_handler.handle_request(game_id)
return JSONResponse(content=response_data, status_code=status_code)
@app.post("/api/games")
def create_game(
data: dict = Body(),
user_id: str | None = Header(default=None),
request_handler: GameCreateHandler = Depends(bootstrap.game_create_handler),
) -> JSONResponse:
status_code, response_data = request_handler.handle_request(user_id, data)
return JSONResponse(content=response_data, status_code=status_code)
@app.patch("/api/games/{game_id}")
def update_game(
game_id: str,
data: dict = Body(),
request_handler: GameStateUpdateHandler = Depends(bootstrap.game_update_handler),
) -> JSONResponse:
status_code, response_data = request_handler.handle_request(game_id, data)
return JSONResponse(content=response_data, status_code=status_code)