-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
136 lines (108 loc) · 3.66 KB
/
Copy pathmain.py
File metadata and controls
136 lines (108 loc) · 3.66 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# https://stackoverflow.com/questions/63511413/fastapi-redirection-for-trailing-slash-returns-non-ssl-link
# Fix for a problem with FastAPI and trailing slashes
from enum import Enum
from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn
class ModelName(str, Enum):
alexnet = "alexnet"
resnet = "resnet"
lenet = "lenet"
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI()
# Path Parameters
@app.get("/")
async def root():
return {"message": "Hello World"}
# @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("/users/{user_id}")
async def read_user(user_id: str):
return {"user_id": user_id}
@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"}
@app.get("/files/{file_path:path}")
async def read_file(file_path: str):
return {"file_path": file_path}
# Quary Parameters
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
# @app.get("/items/")
# async def read_item(skip: int = 0, limit: int = 2):
# return fake_items_db[skip + limit]
# @app.get("/items/{item_id}")
# async def read_item(item_id: str, q: str | None = None):
# if q:
# return {"item_id": item_id, "q": q}
# return {"item_id": item_id}
# @app.get("/items/{item_id}")
# async def read_item(item_id: str, q: str | None = None, short: bool = False):
# item = {"item_id": item_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
@app.get("/users/{user_id}/items/{item_id}")
async def read_user_item(
user_id: int, item_id: str, q: 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
# @app.get("/items/{item_id}")
# async def read_user_item(item_id: str, needy: str):
# item = {"item_id": item_id, "needy": needy}
# return item
# @app.get("/items/{item_id}")
# async def read_user_item(item_id: str, needy: str, skip: int = 0, limit: int | None = None):
# item = {"item_id": item_id, "needy": needy, "skip": skip, "limit": limit}
# return item
# Rwquest Body
# Sample JSON
# {
# "name": "Foo",
# "description": "An optional description",
# "price": 45.2,
# "tax": 3.5
# }
# @app.post("/items/")
# async def create_item(item: Item):
# return item
@app.post("/items/")
async def create_item(item: Item):
item_dict = item.dict()
if item.tax:
price_with_tax = item.price + item.tax
item_dict.update({"price_with_tax": price_with_tax})
return item_dict
# @app.put("/items/{item_id}")
# async def create_item(item_id: int, item: Item):
# return {"item_id": item_id, **item.dict()}
@app.put("/items/{item_id}")
async def create_item(item_id: int, item: Item, q: str | None = None):
result = {"item_id": item_id, **item.dict()}
if q:
result.update({"q": q})
return result
if __name__ == '__main__':
uvicorn.run('main:app', reload=True)