๐ข ๐ข¶
๐โ ๐ ๐ฃ ๐ ๐ข ๐ข ๐ ๐ซ ๐ โก ๐ข, ๐ซ ๐ ๐ฌ "๐ข" ๐ข.
from fastapi import FastAPI
app = FastAPI()
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]
๐ข โ ๐-๐ฒ ๐ซ ๐ ๐ถ โฎ๏ธ ?
๐, ๐ &
๐ฆน.
๐ผ, ๐:
http://127.0.0.1:8000/items/?skip=0&limit=10
...๐ข ๐ข:
skip
: โฎ๏ธ ๐ฒ0
limit
: โฎ๏ธ ๐ฒ10
๐ซ ๐ ๐, ๐ซ "๐" ๐ป.
โ๏ธ ๐โ ๐ ๐ฃ ๐ซ โฎ๏ธ ๐ ๐ (๐ผ ๐, int
), ๐ซ ๐ ๐ ๐ & โ ๐ก โซ๏ธ.
๐ ๐ ๐ ๏ธ ๐ โ โก ๐ข โ ๐ข ๐ข:
- ๐จโ๐จ ๐โ๐ฆบ (๐ฒ)
- ๐ฝ "โ"
- ๐ฝ ๐ฌ
- ๐ง ๐งพ
๐ข¶
๐ข ๐ข ๐ซ ๐ง ๐ โก, ๐ซ ๐ช ๐ฆ & ๐ช โ๏ธ ๐ข ๐ฒ.
๐ผ ๐ ๐ซ โ๏ธ ๐ข ๐ฒ skip=0
& limit=10
.
, ๐ ๐:
http://127.0.0.1:8000/items/
๐ ๐ ๐:
http://127.0.0.1:8000/items/?skip=0&limit=10
โ๏ธ ๐ฅ ๐ ๐ถ, ๐ผ:
http://127.0.0.1:8000/items/?skip=20
๐ข ๐ฒ ๐ ๐ข ๐:
skip=20
: โฉ๏ธ ๐ โ โซ๏ธ ๐limit=10
: โฉ๏ธ ๐ ๐ข ๐ฒ
๐ฆ ๐ข¶
๐ ๐, ๐ ๐ช ๐ฃ ๐ฆ ๐ข ๐ข, โ ๐ซ ๐ข None
:
from typing import Union
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
async def read_item(item_id: str, q: Union[str, None] = None):
if q:
return {"item_id": item_id, "q": q}
return {"item_id": item_id}
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
async def read_item(item_id: str, q: str | None = None):
if q:
return {"item_id": item_id, "q": q}
return {"item_id": item_id}
๐ ๐ผ, ๐ข ๐ข q
๐ ๐ฆ, & ๐ None
๐ข.
Check
๐ ๐ FastAPI ๐ ๐ฅ ๐ ๐ โก ๐ข item_id
โก ๐ข & q
๐ซ,, โซ๏ธ ๐ข ๐ข.
๐ข ๐ข ๐ ๐ ๏ธ¶
๐ ๐ช ๐ฃ bool
๐, & ๐ซ ๐ ๐:
from typing import Union
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
async def read_item(item_id: str, q: Union[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
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
async def read_item(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
๐ ๐ผ, ๐ฅ ๐ ๐ถ:
http://127.0.0.1:8000/items/foo?short=1
โ๏ธ
http://127.0.0.1:8000/items/foo?short=True
โ๏ธ
http://127.0.0.1:8000/items/foo?short=true
โ๏ธ
http://127.0.0.1:8000/items/foo?short=on
โ๏ธ
http://127.0.0.1:8000/items/foo?short=yes
โ๏ธ ๐ ๐ ๐ผ ๐ (๐ , ๐ฅ ๐ค ๐ , โ๏ธ), ๐ ๐ข ๐ ๐ ๐ข short
โฎ๏ธ bool
๐ฒ True
. โช False
.
๐ โก & ๐ข ๐ข¶
๐ ๐ช ๐ฃ ๐ โก ๐ข & ๐ข ๐ข ๐ ๐ฐ, FastAPI ๐ญ โ โ.
& ๐ ๐ซ โ๏ธ ๐ฃ ๐ซ ๐ ๐ฏ โ.
๐ซ ๐ ๐ฌ ๐:
from typing import Union
from fastapi import FastAPI
app = FastAPI()
@app.get("/users/{user_id}/items/{item_id}")
async def read_user_item(
user_id: int, item_id: str, q: Union[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
from fastapi import FastAPI
app = FastAPI()
@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
โ ๐ข ๐ข¶
๐โ ๐ ๐ฃ ๐ข ๐ฒ ๐ซ-โก ๐ข (๐, ๐ฅ โ๏ธ ๐ด ๐ ๐ข ๐ข), โคด๏ธ โซ๏ธ ๐ซ โ.
๐ฅ ๐ ๐ซ ๐ ๐ฎ ๐ฏ ๐ฒ โ๏ธ โ โซ๏ธ ๐ฆ, โ ๐ข None
.
โ๏ธ ๐โ ๐ ๐ โ ๐ข ๐ข โ, ๐ ๐ช ๐ซ ๐ฃ ๐ ๐ข ๐ฒ:
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
async def read_user_item(item_id: str, needy: str):
item = {"item_id": item_id, "needy": needy}
return item
๐ฅ ๐ข ๐ข needy
โ ๐ข ๐ข ๐ str
.
๐ฅ ๐ ๐ ๐ ๐ฅ ๐ ๐:
http://127.0.0.1:8000/items/foo-item
...๐ต โ โ ๐ข needy
, ๐ ๐ ๐ โ ๐:
{
"detail": [
{
"loc": [
"query",
"needy"
],
"msg": "field required",
"type": "value_error.missing"
}
]
}
needy
๐ ๐ข, ๐ ๐ ๐ช โ โซ๏ธ ๐:
http://127.0.0.1:8000/items/foo-item?needy=sooooneedy
...๐ ๐ ๐ท:
{
"item_id": "foo-item",
"needy": "sooooneedy"
}
& โ๏ธ, ๐ ๐ช ๐ฌ ๐ข โ, โ๏ธ ๐ข ๐ฒ, & ๐ ๐ฆ:
from typing import Union
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
async def read_user_item(
item_id: str, needy: str, skip: int = 0, limit: Union[int, None] = None
):
item = {"item_id": item_id, "needy": needy, "skip": skip, "limit": limit}
return item
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
async def read_user_item(
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
๐ ๐ผ, ๐ค 3๏ธโฃ ๐ข ๐ข:
needy
, โstr
.skip
,int
โฎ๏ธ ๐ข ๐ฒ0
.limit
, ๐ฆint
.
Tip
๐ ๐ช โ๏ธ Enum
โ ๐ ๐ โฎ๏ธ โก ๐ข.