โก ๐ ๏ธ ๐ณ¶
๐ค ๐ ๐ข ๐ ๐ ๐ช ๐ถโโ๏ธ ๐ โก ๐ ๏ธ ๐จโ๐จ ๐ โซ๏ธ.
Warning
๐ ๐ ๐ซ ๐ข ๐ถโโ๏ธ ๐ โก ๐ ๏ธ ๐จโ๐จ, ๐ซ ๐ โก ๐ ๏ธ ๐ข.
๐จ ๐ ๐¶
๐ ๐ช ๐ฌ (๐บ๐ธ๐) status_code
โ๏ธ ๐จ ๐ โก ๐ ๏ธ.
๐ ๐ช ๐ถโโ๏ธ ๐ int
๐, ๐ 404
.
โ๏ธ ๐ฅ ๐ ๐ซ ๐ญ โซ๏ธโ ๐ ๐ข ๐, ๐ ๐ช โ๏ธ โจ ๐ status
:
from typing import Set, Union
from fastapi import FastAPI, status
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: Set[str] = set()
@app.post("/items/", response_model=Item, status_code=status.HTTP_201_CREATED)
async def create_item(item: Item):
return item
from typing import Union
from fastapi import FastAPI, status
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: set[str] = set()
@app.post("/items/", response_model=Item, status_code=status.HTTP_201_CREATED)
async def create_item(item: Item):
return item
from fastapi import FastAPI, status
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
tags: set[str] = set()
@app.post("/items/", response_model=Item, status_code=status.HTTP_201_CREATED)
async def create_item(item: Item):
return item
๐ ๐ ๐ ๐ โ๏ธ ๐จ & ๐ ๐ฎ ๐ ๐.
๐ก โน
๐ ๐ช โ๏ธ from starlette import status
.
FastAPI ๐ ๐ starlette.status
fastapi.status
๐ช ๐, ๐ฉโ๐ป. โ๏ธ โซ๏ธ ๐ ๐ โช๏ธโก๏ธ ๐.
๐¶
๐ ๐ช ๐ฎ ๐ ๐ โก ๐ ๏ธ, ๐ถโโ๏ธ ๐ข tags
โฎ๏ธ list
str
(๐ 1๏ธโฃ str
):
from typing import Set, Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: Set[str] = set()
@app.post("/items/", response_model=Item, tags=["items"])
async def create_item(item: Item):
return item
@app.get("/items/", tags=["items"])
async def read_items():
return [{"name": "Foo", "price": 42}]
@app.get("/users/", tags=["users"])
async def read_users():
return [{"username": "johndoe"}]
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: set[str] = set()
@app.post("/items/", response_model=Item, tags=["items"])
async def create_item(item: Item):
return item
@app.get("/items/", tags=["items"])
async def read_items():
return [{"name": "Foo", "price": 42}]
@app.get("/users/", tags=["users"])
async def read_users():
return [{"username": "johndoe"}]
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
tags: set[str] = set()
@app.post("/items/", response_model=Item, tags=["items"])
async def create_item(item: Item):
return item
@app.get("/items/", tags=["items"])
async def read_items():
return [{"name": "Foo", "price": 42}]
@app.get("/users/", tags=["users"])
async def read_users():
return [{"username": "johndoe"}]
๐ซ ๐ ๐ฎ ๐ ๐ & โ๏ธ ๐ง ๐งพ ๐ข:
๐ โฎ๏ธ ๐ข¶
๐ฅ ๐ โ๏ธ ๐ฆ ๐ธ, ๐ 5๏ธโฃ๐ ๐ ๐ ๐ ๐ ๐, & ๐ ๐ ๐ โ ๐ญ ๐ ๐ง โ๏ธ ๐ ๐ ๐ โก ๐ ๏ธ.
๐ซ ๐ผ, โซ๏ธ ๐ช โ ๐ ๐ช ๐ Enum
.
FastAPI ๐โ๐ฆบ ๐ ๐ ๐ โฎ๏ธ โ ๐ป:
from enum import Enum
from fastapi import FastAPI
app = FastAPI()
class Tags(Enum):
items = "items"
users = "users"
@app.get("/items/", tags=[Tags.items])
async def get_items():
return ["Portal gun", "Plumbus"]
@app.get("/users/", tags=[Tags.users])
async def read_users():
return ["Rick", "Morty"]
๐ & ๐¶
๐ ๐ช ๐ฎ summary
& description
:
from typing import Set, Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: Set[str] = set()
@app.post(
"/items/",
response_model=Item,
summary="Create an item",
description="Create an item with all the information, name, description, price, tax and a set of unique tags",
)
async def create_item(item: Item):
return item
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: set[str] = set()
@app.post(
"/items/",
response_model=Item,
summary="Create an item",
description="Create an item with all the information, name, description, price, tax and a set of unique tags",
)
async def create_item(item: Item):
return item
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
tags: set[str] = set()
@app.post(
"/items/",
response_model=Item,
summary="Create an item",
description="Create an item with all the information, name, description, price, tax and a set of unique tags",
)
async def create_item(item: Item):
return item
๐ โช๏ธโก๏ธ #๏ธโฃ¶
๐ ๐ ๐ & ๐ ๐ โธ, ๐ ๐ช ๐ฃ โก ๐ ๏ธ ๐ ๐ข #๏ธโฃ & FastAPI ๐ โ โซ๏ธ โช๏ธโก๏ธ ๐ค.
๐ ๐ช โ โ #๏ธโฃ , โซ๏ธ ๐ ๐ฌ & ๐ฅ โ (โ ๐ ๐ง #๏ธโฃ ๐).
from typing import Set, Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: Set[str] = set()
@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
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: set[str] = set()
@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
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
tags: set[str] = set()
@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
โซ๏ธ ๐ โ๏ธ ๐ ๐ฉบ:
๐จ ๐¶
๐ ๐ช โ ๐จ ๐ โฎ๏ธ ๐ข response_description
:
from typing import Set, Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: Set[str] = set()
@app.post(
"/items/",
response_model=Item,
summary="Create an item",
response_description="The created 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
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: set[str] = set()
@app.post(
"/items/",
response_model=Item,
summary="Create an item",
response_description="The created 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
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
tags: set[str] = set()
@app.post(
"/items/",
response_model=Item,
summary="Create an item",
response_description="The created 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
Info
๐ ๐ response_description
๐ ๐ฏ ๐จ, description
๐ โก ๐ ๏ธ ๐ข.
Check
๐ โ ๐ ๐ โก ๐ ๏ธ ๐ ๐จ ๐.
, ๐ฅ ๐ ๐ซ ๐ 1๏ธโฃ, FastAPI ๐ ๐ ๐ 1๏ธโฃ "๐ ๐จ".
๐ข โก ๐ ๏ธ¶
๐ฅ ๐ ๐ช โข โก ๐ ๏ธ ๐ข, โ๏ธ ๐ต โ โซ๏ธ, ๐ถโโ๏ธ ๐ข deprecated
:
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/", tags=["items"])
async def read_items():
return [{"name": "Foo", "price": 42}]
@app.get("/users/", tags=["users"])
async def read_users():
return [{"username": "johndoe"}]
@app.get("/elements/", tags=["items"], deprecated=True)
async def read_elements():
return [{"item_id": "Foo"}]
โซ๏ธ ๐ ๐ฏ โข ๐ข ๐ ๐ฉบ:
โ โ ๐ข & ๐ซ-๐ข โก ๐ ๏ธ ๐ ๐:
๐¶
๐ ๐ช ๐ & ๐ฎ ๐ ๐ โก ๐ ๏ธ ๐ช ๐ถโโ๏ธ ๐ข โก ๐ ๏ธ ๐จโ๐จ.