-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
84 lines (61 loc) · 1.92 KB
/
main.py
File metadata and controls
84 lines (61 loc) · 1.92 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
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
import datetime
import fastapi
import httpx
import pydantic
import database
db = database.fake_db
app = fastapi.FastAPI()
class ValueIn(
pydantic.BaseModel,
):
id: str
value: str
class AllValues(
pydantic.BaseModel,
):
values: list[ValueIn]
@app.get("/{value_id}", response_model=str)
def get_value(value_id: str) -> str:
if (value := db.get_value(value_id)) is None:
raise fastapi.HTTPException(
status_code=404,
detail="Value not found",
)
return value
@app.post("/", response_model=ValueIn)
def add_value(value_in: ValueIn):
if db.get_value(value_in.id) is not None:
raise fastapi.HTTPException(
status_code=fastapi.status.HTTP_403_FORBIDDEN,
detail="Value already exists",
)
db.add_value(id_=value_in.id, value=value_in.value)
return value_in
@app.delete("/{value_id}", response_model=str)
def delete_value(value_id: str):
if db.get_value(value_id) is None:
raise fastapi.HTTPException(
status_code=fastapi.status.HTTP_404_NOT_FOUND,
detail=f"Value not found {value_id}",
)
db.remove_value(value_id)
return value_id
@app.get("/", response_model=AllValues)
def get_all_values():
values = db.get_all_values()
return AllValues(values=values)
@app.get("/long_running_task/", response_model=str)
def long_running_task(num_secs: int):
db.some_long_running_task(num_secs)
return "Done"
@app.get("/get_datetime_now/", response_model=str)
def get_date() -> str:
return datetime.datetime.now().isoformat()
@app.get("/async_route/", response_model=int)
async def async_route():
async with httpx.AsyncClient() as client:
response = await client.get("https://www.google.com/")
return response.status_code
@app.get("/this_route_has_some_thing_i_dont_want/", response_model=int)
async def this_route_doesnt_do_what_i_want():
return 1