Skip to content

๐ŸŽ“ ๐Ÿ”—

โญ ๐Ÿคฟ โฌ ๐Ÿ”˜ ๐Ÿ”— ๐Ÿ’‰ โš™๏ธ, โžก๏ธ โ™ป โฎ๏ธ ๐Ÿ–ผ.

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 ๐Ÿ’… ๐Ÿ”ƒ ๐Ÿค ๐Ÿ‘† ๐Ÿ“‰ ๐Ÿ“Ÿ ๐Ÿ”.