-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial1.py
More file actions
64 lines (45 loc) · 1.94 KB
/
tutorial1.py
File metadata and controls
64 lines (45 loc) · 1.94 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
from fastapi import FastAPI
from enum import Enum
from typing import Union
app = FastAPI()
class ModelName(str, Enum):
alexnet = "alexnet"
resnet = "resnet"
lenet = "lenet"
fake_items_db = [{"item_name": "Cricket"}, {"item_name": "Football"}, {"item_name": "Scoccer"}, {"item_name": "Tennis"},
{"item_name": "Table Tennis"}, {"item_name": "Hockey"}, {"item_name": "Swimming"},{"item_name": "Badminton"},
{"item_name": "Rugby"}, {"item_name": "Baseball"}, {"item_name": "Volleyball"}, {"item_name": "Basketball"},]
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.get("/items/sports")
async def read_item(skip: int = 0, limit: int = 10):
return fake_items_db[skip : skip + limit]
# item_id -> is the path parameter with type
@app.get("/items/{item_id}")
async def read_item(item_id: int):
return {"item_id": item_id}
@app.get("/users/me")
async def read_user_me():
return {"user_id": "the current user"}
@app.get("/models/{model_name}")
async def get_model(model_name: ModelName):
if model_name == ModelName.alexnet:
return {"model_name": model_name, "message": "Deep Learning FTW!"} # JSON respose
# if model_name.value == "lenet": # Get the enumeration value
if model_name == ModelName.lenet: # Compare enumeration members
return {"model_name": model_name, "message": "LeCNN all the images"}
return {"model_name": model_name, "message": "Have some residuals"}
# Multiple path and query parameters
@app.get("/users/{user_id}/items/{item_id}")
async def read_user_item(
user_id: int, item_id: str, q: Union[str, None] = None, short: bool = False
):
item = {"item_id": item_id, "owner_id": user_id}
if q:
item.update({"q": q})
if not short:
item.update(
{"description": "This is an amazing item that has a long description"}
)
return item