-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_parameter.py
76 lines (51 loc) · 1.73 KB
/
query_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
68
69
70
71
72
73
74
75
76
from fastapi import FastAPI
import uvicorn
app = FastAPI()
# Query 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 = 10):
return fake_items_db[skip: skip + limit]
# Optional query parameters
@app.get("/items/{item_id}")
async def read_item1(item_id: str, q: str | None = None):
if q:
return {"item_id": item_id, "q": q}
return {"item_id": item_id}
# Query parameter type conversion
@app.get("/items/{item_id}")
async def read_item2(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
# 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: 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
# Required query parameters
@app.get("/items/{item_id}")
async def read_user_item1(item_id: str, needy: str):
item = {"item_id": item_id, "needy": needy}
return item
@app.get("/items/{item_id}")
async def read_user_item2(
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
if __name__ == '__main__':
uvicorn.run(app, port=8000, host='127.0.0.1')