๐ ๐¶
โญ ๐คฟ โฌ ๐ ๐ ๐ โ๏ธ, โก๏ธ โป โฎ๏ธ ๐ผ.
dict
โช๏ธโก๏ธ โฎ๏ธ ๐ผ¶
โฎ๏ธ ๐ผ, ๐ฅ ๐ฌ dict
โช๏ธโก๏ธ ๐ ๐ ("โ"):
from typing import Union
from fastapi import Depends, FastAPI
app = FastAPI()
async def common_parameters(
q: Union[str, None] = None, skip: int = 0, limit: int = 100
):
return {"q": q, "skip": skip, "limit": limit}
@app.get("/items/")
async def read_items(commons: dict = Depends(common_parameters)):
return commons
@app.get("/users/")
async def read_users(commons: dict = Depends(common_parameters)):
return commons
from fastapi import Depends, FastAPI
app = FastAPI()
async def common_parameters(q: str | None = None, skip: int = 0, limit: int = 100):
return {"q": q, "skip": skip, "limit": limit}
@app.get("/items/")
async def read_items(commons: dict = Depends(common_parameters)):
return commons
@app.get("/users/")
async def read_users(commons: dict = Depends(common_parameters)):
return commons
โ๏ธ โคด๏ธ ๐ฅ ๐ค dict
๐ข commons
โก ๐ ๏ธ ๐ข.
& ๐ฅ ๐ญ ๐ ๐จโ๐จ ๐ช ๐ซ ๐ ๐ ๐โ๐ฆบ (๐ ๐ ๏ธ) dict
โ, โฉ๏ธ ๐ซ ๐ช ๐ซ ๐ญ ๐ซ ๐ & ๐ฒ ๐.
๐ฅ ๐ช ๐...
โซ๏ธโ โ ๐¶
๐ ๐ ๐ โ๏ธ ๐ ๐ ๐ฃ ๐ข.
โ๏ธ ๐ ๐ซ ๐ด ๐ ๐ฃ ๐ (๐ โซ๏ธ ๐ ๐ฒ ๐ โ ).
๐ โ ๐ ๐ ๐ "๐ง๐ฒ".
"๐ง๐ฒ" ๐ ๐ณ ๐ ๐ ๐ช "๐ค" ๐ ๐ข.
, ๐ฅ ๐ โ๏ธ ๐ something
(๐ ๐ช ๐ซ ๐ข) & ๐ ๐ช "๐ค" โซ๏ธ (๐ ๏ธ โซ๏ธ) ๐:
something()
โ๏ธ
something(some_argument, some_keyword_argument="foo")
โคด๏ธ โซ๏ธ "๐ง๐ฒ".
๐ ๐¶
๐ 5๏ธโฃ๐ ๐ ๐ โ ๐ ๐ ๐, ๐ โ๏ธ ๐ ๐ โ.
๐ผ:
class Cat:
def __init__(self, name: str):
self.name = name
fluffy = Cat(name="Mr Fluffy")
๐ ๐ผ, fluffy
๐ ๐ Cat
.
& โ fluffy
, ๐ "๐ค" Cat
.
, ๐ ๐ ๐ง๐ฒ.
โคด๏ธ, FastAPI, ๐ ๐ช โ๏ธ ๐ ๐ ๐.
โซ๏ธโ FastAPI ๐ค โ ๐ โซ๏ธ "๐ง๐ฒ" (๐ข, ๐ โ๏ธ ๐ณ ๐) & ๐ข ๐ฌ.
๐ฅ ๐ ๐ถโโ๏ธ "๐ง๐ฒ" ๐ FastAPI, โซ๏ธ ๐ ๐ฌ ๐ข ๐ "๐ง๐ฒ", & ๐ ๏ธ ๐ซ ๐ ๐ ๐ข โก ๐ ๏ธ ๐ข. โ ๐ง-๐.
๐ โ ๐ง๐ฒ โฎ๏ธ ๐ โโ ๐ข ๐. ๐ โซ๏ธ ๐ โก ๐ ๏ธ ๐ข โฎ๏ธ ๐ โโ ๐ข.
โคด๏ธ, ๐ฅ ๐ช ๐ ๐ "โ" common_parameters
โช๏ธโก๏ธ ๐ ๐ CommonQueryParams
:
from typing import Union
from fastapi import Depends, FastAPI
app = FastAPI()
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
class CommonQueryParams:
def __init__(self, q: Union[str, None] = None, skip: int = 0, limit: int = 100):
self.q = q
self.skip = skip
self.limit = limit
@app.get("/items/")
async def read_items(commons: CommonQueryParams = Depends(CommonQueryParams)):
response = {}
if commons.q:
response.update({"q": commons.q})
items = fake_items_db[commons.skip : commons.skip + commons.limit]
response.update({"items": items})
return response
from fastapi import Depends, FastAPI
app = FastAPI()
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
class CommonQueryParams:
def __init__(self, q: str | None = None, skip: int = 0, limit: int = 100):
self.q = q
self.skip = skip
self.limit = limit
@app.get("/items/")
async def read_items(commons: CommonQueryParams = Depends(CommonQueryParams)):
response = {}
if commons.q:
response.update({"q": commons.q})
items = fake_items_db[commons.skip : commons.skip + commons.limit]
response.update({"items": items})
return response
๐ธ ๐ __init__
๐ฉโ๐ฌ โ๏ธ โ ๐ ๐:
from typing import Union
from fastapi import Depends, FastAPI
app = FastAPI()
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
class CommonQueryParams:
def __init__(self, q: Union[str, None] = None, skip: int = 0, limit: int = 100):
self.q = q
self.skip = skip
self.limit = limit
@app.get("/items/")
async def read_items(commons: CommonQueryParams = Depends(CommonQueryParams)):
response = {}
if commons.q:
response.update({"q": commons.q})
items = fake_items_db[commons.skip : commons.skip + commons.limit]
response.update({"items": items})
return response
from fastapi import Depends, FastAPI
app = FastAPI()
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
class CommonQueryParams:
def __init__(self, q: str | None = None, skip: int = 0, limit: int = 100):
self.q = q
self.skip = skip
self.limit = limit
@app.get("/items/")
async def read_items(commons: CommonQueryParams = Depends(CommonQueryParams)):
response = {}
if commons.q:
response.update({"q": commons.q})
items = fake_items_db[commons.skip : commons.skip + commons.limit]
response.update({"items": items})
return response
...โซ๏ธ โ๏ธ ๐ ๐ข ๐ โฎ๏ธ common_parameters
:
from typing import Union
from fastapi import Depends, FastAPI
app = FastAPI()
async def common_parameters(
q: Union[str, None] = None, skip: int = 0, limit: int = 100
):
return {"q": q, "skip": skip, "limit": limit}
@app.get("/items/")
async def read_items(commons: dict = Depends(common_parameters)):
return commons
@app.get("/users/")
async def read_users(commons: dict = Depends(common_parameters)):
return commons
from fastapi import Depends, FastAPI
app = FastAPI()
async def common_parameters(q: str | None = None, skip: int = 0, limit: int = 100):
return {"q": q, "skip": skip, "limit": limit}
@app.get("/items/")
async def read_items(commons: dict = Depends(common_parameters)):
return commons
@app.get("/users/")
async def read_users(commons: dict = Depends(common_parameters)):
return commons
๐ ๐ข โซ๏ธโ FastAPI ๐ โ๏ธ "โ" ๐.
๐ฏโโ๏ธ ๐ผ, โซ๏ธ ๐ โ๏ธ:
- ๐ฆ
q
๐ข ๐ข ๐str
. skip
๐ข ๐ข ๐int
, โฎ๏ธ ๐ข0
.limit
๐ข ๐ข ๐int
, โฎ๏ธ ๐ข100
.
๐ฏโโ๏ธ ๐ผ ๐ฝ ๐ ๐, โ, ๐ ๐ ๐ ๐, โ๏ธ.
โ๏ธ โซ๏ธ¶
๐ ๐ ๐ช ๐ฃ ๐ ๐ โ๏ธ ๐ ๐.
from typing import Union
from fastapi import Depends, FastAPI
app = FastAPI()
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
class CommonQueryParams:
def __init__(self, q: Union[str, None] = None, skip: int = 0, limit: int = 100):
self.q = q
self.skip = skip
self.limit = limit
@app.get("/items/")
async def read_items(commons: CommonQueryParams = Depends(CommonQueryParams)):
response = {}
if commons.q:
response.update({"q": commons.q})
items = fake_items_db[commons.skip : commons.skip + commons.limit]
response.update({"items": items})
return response
from fastapi import Depends, FastAPI
app = FastAPI()
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
class CommonQueryParams:
def __init__(self, q: str | None = None, skip: int = 0, limit: int = 100):
self.q = q
self.skip = skip
self.limit = limit
@app.get("/items/")
async def read_items(commons: CommonQueryParams = Depends(CommonQueryParams)):
response = {}
if commons.q:
response.update({"q": commons.q})
items = fake_items_db[commons.skip : commons.skip + commons.limit]
response.update({"items": items})
return response
FastAPI ๐ค CommonQueryParams
๐. ๐ โ "๐" ๐ ๐ & ๐ ๐ ๐ถโโ๏ธ ๐ข commons
๐ ๐ข.
๐ โ ๐ Depends
¶
๐ โ ๐ฅ โ CommonQueryParams
๐ ๐ ๐:
commons: CommonQueryParams = Depends(CommonQueryParams)
๐ CommonQueryParams
,:
... = Depends(CommonQueryParams)
...โซ๏ธโ FastAPI ๐ ๐ค โ๏ธ ๐ญ โซ๏ธโ ๐.
โช๏ธโก๏ธ โซ๏ธ ๐ FastAPI ๐ โ ๐ฃ ๐ข & ๐ โซ๏ธโ FastAPI ๐ ๐ค ๐ค.
๐ ๐ผ, ๐ฅ CommonQueryParams
,:
commons: CommonQueryParams ...
...๐ซ โ๏ธ ๐ ๐ ๐ FastAPI. FastAPI ๐ ๐ซ โ๏ธ โซ๏ธ ๐ฝ ๐ ๏ธ, ๐ฌ, โ๏ธ. (โซ๏ธ โ๏ธ = Depends(CommonQueryParams)
๐).
๐ ๐ช ๐ค โ:
commons = Depends(CommonQueryParams)
...:
from typing import Union
from fastapi import Depends, FastAPI
app = FastAPI()
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
class CommonQueryParams:
def __init__(self, q: Union[str, None] = None, skip: int = 0, limit: int = 100):
self.q = q
self.skip = skip
self.limit = limit
@app.get("/items/")
async def read_items(commons=Depends(CommonQueryParams)):
response = {}
if commons.q:
response.update({"q": commons.q})
items = fake_items_db[commons.skip : commons.skip + commons.limit]
response.update({"items": items})
return response
from fastapi import Depends, FastAPI
app = FastAPI()
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
class CommonQueryParams:
def __init__(self, q: str | None = None, skip: int = 0, limit: int = 100):
self.q = q
self.skip = skip
self.limit = limit
@app.get("/items/")
async def read_items(commons=Depends(CommonQueryParams)):
response = {}
if commons.q:
response.update({"q": commons.q})
items = fake_items_db[commons.skip : commons.skip + commons.limit]
response.update({"items": items})
return response
โ๏ธ ๐ฃ ๐ ๐ก ๐ ๐ ๐ ๐จโ๐จ ๐ ๐ญ โซ๏ธโ ๐ ๐ถโโ๏ธ ๐ข commons
, & โคด๏ธ โซ๏ธ ๐ช โน ๐ โฎ๏ธ ๐ ๐ ๏ธ, ๐ โ
, โ๏ธ:
โจ¶
โ๏ธ ๐ ๐ ๐ ๐ฅ โ๏ธ ๐ ๐ ๐ฅ, โ CommonQueryParams
๐:
commons: CommonQueryParams = Depends(CommonQueryParams)
FastAPI ๐ โจ ๐ซ ๐ผ, ๐โ ๐ ๐ฏ ๐ ๐ FastAPI ๐ "๐ค" โ ๐ ๐ โซ๏ธ.
๐ ๐ฏ ๐ผ, ๐ ๐ช ๐:
โฉ๏ธ โ:
commons: CommonQueryParams = Depends(CommonQueryParams)
...๐ โ:
commons: CommonQueryParams = Depends()
๐ ๐ฃ ๐ ๐ ๐ข, & ๐ โ๏ธ Depends()
๐ฎ "๐ข" ๐ฒ (๐ โฎ๏ธ =
) ๐ ๐ข ๐ข, ๐ต ๐ ๐ข Depends()
, โฉ๏ธ โ๏ธ โ ๐ ๐ ๐ ๐ Depends(CommonQueryParams)
.
๐ ๐ผ ๐ โคด๏ธ ๐ ๐:
from typing import Union
from fastapi import Depends, FastAPI
app = FastAPI()
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
class CommonQueryParams:
def __init__(self, q: Union[str, None] = None, skip: int = 0, limit: int = 100):
self.q = q
self.skip = skip
self.limit = limit
@app.get("/items/")
async def read_items(commons: CommonQueryParams = Depends()):
response = {}
if commons.q:
response.update({"q": commons.q})
items = fake_items_db[commons.skip : commons.skip + commons.limit]
response.update({"items": items})
return response
from fastapi import Depends, FastAPI
app = FastAPI()
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
class CommonQueryParams:
def __init__(self, q: str | None = None, skip: int = 0, limit: int = 100):
self.q = q
self.skip = skip
self.limit = limit
@app.get("/items/")
async def read_items(commons: CommonQueryParams = Depends()):
response = {}
if commons.q:
response.update({"q": commons.q})
items = fake_items_db[commons.skip : commons.skip + commons.limit]
response.update({"items": items})
return response
...& FastAPI ๐ ๐ญ โซ๏ธโ.
Tip
๐ฅ ๐ ๐ ๐ ๐จ ๐ ๐, ๐คทโโ โซ๏ธ, ๐ ๐ซ ๐ช โซ๏ธ.
โซ๏ธ โจ. โฉ๏ธ FastAPI ๐ ๐ ๐ค ๐ ๐ ๐ ๐.