-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_parameter.py
67 lines (39 loc) · 1.26 KB
/
path_parameter.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
from enum import Enum
from fastapi import FastAPI
import uvicorn
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.get("/items/{item_id}")
async def read_item(item_id):
return {"item_id": item_id}
# automatic request parsing
@app.get("/items/{item_id}")
async def read_item(item_id: int):
return {"item_id": item_id}
# path parameters
@app.get("/users/me")
async def read_user_me():
return {"user_id": "the current user"}
@app.get("/users/{user_id}")
async def read_user(user_id: str):
return {"user_id": user_id}
# Predefined values
class ModelName(str, Enum):
alexnet = "alexnet"
resnet = "resnet"
lenet = "lenet"
@app.get("/models/{model_name}")
async def get_model(model_name: ModelName):
if model_name is ModelName.alexnet:
return {"model_name": model_name, "message": "Deep Learning FTW!"}
if model_name.value == "lenet":
return {"model_name": model_name, "message": "LeCNN all the images"}
return {"model_name": model_name, "message": "Have some residuals"}
# Path convertor
@app.get("/files/{file_path:path}")
async def read_file(file_path: str):
return {"file_path": file_path}
if __name__ == '__main__':
uvicorn.run(app, port=8000, host='127.0.0.1')