-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
57 lines (47 loc) · 1.77 KB
/
main.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
from fastapi import FastAPI, Query, Path, HTTPException # type: ignore
from pydantic import BaseModel
from typing import Annotated
from enum import Enum
from decimal import Decimal
app = FastAPI()
# http://localhost:8080/docs
class Item(BaseModel):
name: str
description: str
price: float
tax: float| None = None
# 定義一個 Enum 來表示 sort_order 的合法值
class SortOrder(str, Enum):
asc = "asc"
desc = "desc"
@app.get("/")
async def root():
return {"message": "Hello World"}
# GET /items/{item_id} Endpoint
@app.get("/items/{item_id}")
async def read_items(
item_id: Annotated[Decimal, Path(ge=1, le=1000,
description="Item ID must be between 1 and 1000.")],
q: Annotated[str | None, Query(description = "Query 'q' must be between 3 and 50 characters.",
min_length=3,
max_length=50)] = None,
sort_order: SortOrder = SortOrder.asc
):
results = {"item_id": item_id}
if q:
results.update({"description": f"This is a sample item that matches the query {q}"})
else:
results.update({"description": "This is a sample item."})
if sort_order:
results.update({"sort_order": sort_order})
return results
@app.put("/items/{item_id}")
async def update_item(
item_id: Annotated[Decimal, Path(ge=1, le=1000, description="Item ID must be between 1 and 1000.")],
item: Item = None,
q: Annotated[str | None, Query(min_length=3, max_length=50, description="Query 'q' must be between 3 and 50 characters.")] = None
):
result = {"item_id": item_id, **item.dict()}
if q:
result.update({"q": q})
return result