@app.get("/users/",
tags=["users"] summary="Create an item",
description="",
response_description=""
deprecated=True,
)
async def read_users():
return [{"username": "johndoe"}]
# tags, summary, description, response_description, deprecated 등은 docs swagger에 보여준다.
# description이 너무 길 경우
@app.post("/items/", response_model=Item, summary="Create an item")
async def create_item(item: Item):
"""
Create an item with all the information:
- **name**: each item must have a name
- **description**: a long description
- **price**: required
- **tax**: if the item doesn't have tax, you can omit this
- **tags**: a set of unique tag strings for this item
"""
return item
# markdown가능.
class Item(BaseModel):
title: str
timestamp: datetime
description: Optional[str] = None
json_compatible_item_data = jsonable_encoder(item)
# it would convert the Pydantic model to a dict, and the datetime to a str.
# == json.dumps()
class Item(BaseModel):
name: Optional[str] = None
description: Optional[str] = None
price: Optional[float] = None
tax: float = 10.5
tags: List[str] = []
items = {
"foo": {"name": "Foo", "price": 50.2},
"bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
"baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}
@app.put("/items/{item_id}", response_model=Item)
async def update_item(item_id: str, item: Item):
update_item_encoded = jsonable_encoder(item)
items[item_id] = update_item_encoded
return update_item_encoded
# {
# "name": "Barz",
# "price": 3,
# "description": None,
# } 로 보내면 default tax가 10.5로 저장된다.
# partial updates
@app.patch("/items/{item_id}", response_model=Item)
async def update_item(item_id: str, item: Item):
stored_item_data = items[item_id]
stored_item_model = Item(**stored_item_data)
update_data = item.dict(exclude_unset=True)
updated_item = stored_item_model.copy(update=update_data)
items[item_id] = jsonable_encoder(updated_item)
return updated_item
# Then you can use this to generate a dict with only the data
# that was set (sent in the request), omitting default values
'Fastapi' 카테고리의 다른 글
내맘대로 Fastapi Document summary[6] (0) | 2021.11.22 |
---|---|
내맘대로 Fastapi Document summary[5] (0) | 2021.11.20 |
내맘대로 Fastapi Document summary[3] (0) | 2021.11.20 |
내맘대로 Fastapi docs 정리(Response Model, Extra Model) (0) | 2021.11.19 |
내맘대로 Fastapi docs 정리(Path, query parameters, request body) (0) | 2021.11.17 |