-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfastapi_request_example_data.py
More file actions
100 lines (83 loc) · 2.53 KB
/
fastapi_request_example_data.py
File metadata and controls
100 lines (83 loc) · 2.53 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
from typing import Optional
from fastapi import FastAPI, Body
from pydantic import BaseModel, Field
app = FastAPI()
class Item(BaseModel):
name: str
description: Optional[str] = None
price: float
tax: Optional[float] = None
class Config:
schema_extra = {
"example": {
"name": "Foo",
"description": "A very good Item",
"price": 25.4,
"tax": 3.4,
}
}
class Item2(BaseModel):
name: str = Field(..., example="Foo")
description: Optional[str] = Field(None, example="A very good Item")
price: float = Field(..., example=33.4)
tax: Optional[float] = Field(None, example=3.3)
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
@app.put("/items2/{item_id}")
async def update_item2(item_id: int, item: Item2):
results = {"item_id": item_id, "item": item}
return results
@app.put("/items3/{item_id}")
async def update_item3(
item_id: int,
item: Item = Body(
...,
example={
"name": "Foo",
"description": "A very good Item !!!",
"price": 34.2,
"tax": 3.2,
},
),
):
results = {"item_id": item_id, "item": item}
return results
@app.put("/items4/{item_id}")
async def update_item3(
item_id: int,
item: Item = Body(
...,
examples={
"normal": {
"summary": "A normal example",
"description": "A **normal** item works correctly.",
"value": {
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},
},
"converted": {
"summary": "An example with converted data",
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
"value": {
"name": "Bar",
"price": "35.4",
},
},
"invalid": {
"summary": "Invalid data is rejected with an error",
"value": {
"name": "Baz",
"price": "thirty five point four",
},
},
},
),
):
results = {"item_id": item_id, "item": item}
return results
# PYTHONPATH=module_fastapi uvicorn fastapi_request_example_data:app --reload