-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
44 lines (28 loc) · 963 Bytes
/
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
from typing import Optional
from pydantic import BaseModel, ValidationError, validator
from fastapi import FastAPI, HTTPException
app = FastAPI()
class UserStats(BaseModel):
user_id: int
winrate: float
nickname: str
@validator('winrate')
def winrate_from_0_to_1(cls, val: float):
if val < 0 or val > 1:
raise ValueError('must be between 0 and 1')
return val
@app.get("/")
def read_root():
return {"Hello": "World"}
data = {0: UserStats(user_id=0, winrate=1, nickname="admin") }
@app.post("/stats", status_code=201)
def write_stats(stats: UserStats):
if stats.user_id in data:
raise HTTPException(status_code=422, detail='cannot overwrite stats')
data[stats.user_id] = stats
return stats
@app.get("/stats/{user_id}")
def read_stats(user_id: int):
if user_id not in data:
raise HTTPException(status_code=404, detail='no user with such id')
return data[user_id]