Skip to content

๐Ÿ”—

FastAPI โœ”๏ธ ๐Ÿ“ถ ๐Ÿ‹๏ธ โœ‹๏ธ ๐Ÿ‹๏ธ ๐Ÿ”— ๐Ÿ’‰ โš™๏ธ.

โšซ๏ธ ๐Ÿ— ๐Ÿ“ถ ๐Ÿ™… โš™๏ธ, & โš’ โšซ๏ธ ๐Ÿ“ถ โฉ ๐Ÿ™† ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ› ๏ธ ๐ŸŽ ๐Ÿฆฒ โฎ๏ธ FastAPI.

โšซ๏ธโ” "๐Ÿ”— ๐Ÿ’‰"

"๐Ÿ”— ๐Ÿ’‰" โ›“, ๐Ÿ“‹, ๐Ÿ‘ˆ ๐Ÿ“ค ๐ŸŒŒ ๐Ÿ‘† ๐Ÿ“Ÿ (๐Ÿ‘‰ ๐Ÿ’ผ, ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ”ข) ๐Ÿ“ฃ ๐Ÿ‘œ ๐Ÿ‘ˆ โšซ๏ธ ๐Ÿšš ๐Ÿ‘ท & โš™๏ธ: "๐Ÿ”—".

& โคด๏ธ, ๐Ÿ‘ˆ โš™๏ธ (๐Ÿ‘‰ ๐Ÿ’ผ FastAPI) ๐Ÿ”œ โœŠ ๐Ÿ’… ๐Ÿ”จ โšซ๏ธโ” ๐Ÿ’ช ๐Ÿšš ๐Ÿ‘† ๐Ÿ“Ÿ โฎ๏ธ ๐Ÿ“š ๐Ÿ’ช ๐Ÿ”— ("๐Ÿ’‰" ๐Ÿ”—).

๐Ÿ‘‰ ๐Ÿ“ถ โš  ๐Ÿ•โ” ๐Ÿ‘† ๐Ÿ’ช:

  • โœ”๏ธ ๐Ÿ’ฐ โš› (๐ŸŽ ๐Ÿ“Ÿ โš› ๐Ÿ”„ & ๐Ÿ”„).
  • ๐Ÿ’ฐ ๐Ÿ’ฝ ๐Ÿ”—.
  • ๐Ÿ› ๏ธ ๐Ÿ’‚โ€โ™‚, ๐Ÿค, ๐Ÿ”‘ ๐Ÿ“„, โ™’๏ธ.
  • & ๐Ÿ“š ๐ŸŽ ๐Ÿ‘œ...

๐ŸŒ ๐Ÿ‘ซ, โช ๐Ÿ“‰ ๐Ÿ“Ÿ ๐Ÿ”.

๐Ÿฅ‡ ๐Ÿ”

โžก๏ธ ๐Ÿ‘€ ๐Ÿ“ถ ๐Ÿ™… ๐Ÿ–ผ. โšซ๏ธ ๐Ÿ”œ ๐Ÿ™… ๐Ÿ‘ˆ โšซ๏ธ ๐Ÿšซ ๐Ÿ“ถ โš , ๐Ÿ”œ.

โœ‹๏ธ ๐Ÿ‘‰ ๐ŸŒŒ ๐Ÿ‘ฅ ๐Ÿ’ช ๐ŸŽฏ ๐Ÿ”› โ” ๐Ÿ”— ๐Ÿ’‰ โš™๏ธ ๐Ÿ‘ท.

โœ ๐Ÿ”—, โš–๏ธ "โ˜‘"

โžก๏ธ ๐Ÿฅ‡ ๐ŸŽฏ ๐Ÿ”› ๐Ÿ”—.

โšซ๏ธ ๐Ÿ”ข ๐Ÿ‘ˆ ๐Ÿ’ช โœŠ ๐ŸŒ ๐ŸŽ ๐Ÿ”ข ๐Ÿ‘ˆ โžก ๐Ÿ› ๏ธ ๐Ÿ”ข ๐Ÿ’ช โœŠ:

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

๐Ÿ‘ˆ โšซ๏ธ.

2๏ธโƒฃ โธ.

& โšซ๏ธ โœ”๏ธ ๐ŸŽ ๐Ÿ’  & ๐Ÿ“Š ๐Ÿ‘ˆ ๐ŸŒ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ”ข โœ”๏ธ.

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ’ญ โšซ๏ธ โžก ๐Ÿ› ๏ธ ๐Ÿ”ข ๐Ÿต "๐Ÿ‘จโ€๐ŸŽจ" (๐Ÿต @app.get("/some-path")).

& โšซ๏ธ ๐Ÿ’ช ๐Ÿ“จ ๐Ÿ•ณ ๐Ÿ‘† ๐Ÿ’š.

๐Ÿ‘‰ ๐Ÿ’ผ, ๐Ÿ‘‰ ๐Ÿ”— โŒ›:

  • ๐Ÿ“ฆ ๐Ÿ”ข ๐Ÿ”ข q ๐Ÿ‘ˆ str.
  • ๐Ÿ“ฆ ๐Ÿ”ข ๐Ÿ”ข skip ๐Ÿ‘ˆ int, & ๐Ÿ”ข 0.
  • ๐Ÿ“ฆ ๐Ÿ”ข ๐Ÿ”ข limit ๐Ÿ‘ˆ int, & ๐Ÿ”ข 100.

& โคด๏ธ โšซ๏ธ ๐Ÿ“จ dict โš— ๐Ÿ“š ๐Ÿ’ฒ.

๐Ÿ—„ Depends

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

๐Ÿ“ฃ ๐Ÿ”—, "โš“๏ธ"

๐ŸŽ ๐ŸŒŒ ๐Ÿ‘† โš™๏ธ Body, Query, โ™’๏ธ. โฎ๏ธ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ”ข ๐Ÿ”ข, โš™๏ธ Depends โฎ๏ธ ๐Ÿ†• ๐Ÿ”ข:

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

๐Ÿ‘ ๐Ÿ‘† โš™๏ธ Depends ๐Ÿ”ข ๐Ÿ‘† ๐Ÿ”ข ๐ŸŽ ๐ŸŒŒ ๐Ÿ‘† โš™๏ธ Body, Query, โ™’๏ธ, Depends ๐Ÿ‘ท ๐Ÿ‘„ ๐ŸŽ.

๐Ÿ‘† ๐Ÿ•ด ๐Ÿค Depends ๐Ÿ‘ ๐Ÿ”ข.

๐Ÿ‘‰ ๐Ÿ”ข ๐Ÿ”œ ๐Ÿ•ณ ๐Ÿ’– ๐Ÿ”ข.

& ๐Ÿ‘ˆ ๐Ÿ”ข โœŠ ๐Ÿ”ข ๐ŸŽ ๐ŸŒŒ ๐Ÿ‘ˆ โžก ๐Ÿ› ๏ธ ๐Ÿ”ข .

Tip

๐Ÿ‘† ๐Ÿ”œ ๐Ÿ‘€ โšซ๏ธโ” ๐ŸŽ "๐Ÿ‘œ", โ†–๏ธ โšช๏ธโžก๏ธ ๐Ÿ”ข, ๐Ÿ’ช โš™๏ธ ๐Ÿ”— โญ ๐Ÿ“ƒ.

๐Ÿ•โ” ๐Ÿ†• ๐Ÿ“จ ๐Ÿ›ฌ, FastAPI ๐Ÿ”œ โœŠ ๐Ÿ’…:

  • ๐Ÿค™ ๐Ÿ‘† ๐Ÿ”— ("โ˜‘") ๐Ÿ”ข โฎ๏ธ โ˜‘ ๐Ÿ”ข.
  • ๐Ÿคš ๐Ÿ โšช๏ธโžก๏ธ ๐Ÿ‘† ๐Ÿ”ข.
  • ๐Ÿ› ๏ธ ๐Ÿ‘ˆ ๐Ÿ ๐Ÿ”ข ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ”ข.
graph TB

common_parameters(["common_parameters"])
read_items["/items/"]
read_users["/users/"]

common_parameters --> read_items
common_parameters --> read_users

๐Ÿ‘‰ ๐ŸŒŒ ๐Ÿ‘† โœ ๐Ÿ”— ๐Ÿ“Ÿ ๐Ÿ• & FastAPI โœŠ ๐Ÿ’… ๐Ÿค™ โšซ๏ธ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ.

Check

๐Ÿ‘€ ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿšซ โœ”๏ธ โœ ๐ŸŽ ๐ŸŽ“ & ๐Ÿšถโ€โ™€๏ธ โšซ๏ธ ๐Ÿ‘ฑ FastAPI "ยฎ" โšซ๏ธ โš–๏ธ ๐Ÿ•ณ ๐ŸŽ.

๐Ÿ‘† ๐Ÿšถโ€โ™€๏ธ โšซ๏ธ Depends & FastAPI ๐Ÿ’ญ โ” ๐ŸŽ‚.

async โš–๏ธ ๐Ÿšซ async

๐Ÿ”— ๐Ÿ”œ ๐Ÿค™ FastAPI (๐ŸŽ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ”ข), ๐ŸŽ ๐Ÿšซ โœ” โช ๐Ÿ”ฌ ๐Ÿ‘† ๐Ÿ”ข.

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ async def โš–๏ธ ๐Ÿ˜ def.

& ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ”— โฎ๏ธ async def ๐Ÿ”˜ ๐Ÿ˜ def โžก ๐Ÿ› ๏ธ ๐Ÿ”ข, โš–๏ธ def ๐Ÿ”— ๐Ÿ”˜ async def โžก ๐Ÿ› ๏ธ ๐Ÿ”ข, โ™’๏ธ.

โšซ๏ธ ๐Ÿšซ ๐Ÿค”. FastAPI ๐Ÿ”œ ๐Ÿ’ญ โšซ๏ธโ”.

Note

๐Ÿšฅ ๐Ÿ‘† ๐Ÿšซ ๐Ÿ’ญ, โœ… ๐Ÿ”: *"๐Ÿƒ โ“" * ๐Ÿ“„ ๐Ÿ”ƒ async & await ๐Ÿฉบ.

๐Ÿ› ๏ธ โฎ๏ธ ๐Ÿ—„

๐ŸŒ ๐Ÿ“จ ๐Ÿ“„, ๐Ÿ”ฌ & ๐Ÿ“„ ๐Ÿ‘† ๐Ÿ”— (& ๐ŸŽง-๐Ÿ”—) ๐Ÿ”œ ๐Ÿ› ๏ธ ๐ŸŽ ๐Ÿ—„ ๐Ÿ”—.

, ๐ŸŽ“ ๐Ÿฉบ ๐Ÿ”œ โœ”๏ธ ๐ŸŒ โ„น โšช๏ธโžก๏ธ ๐Ÿ‘ซ ๐Ÿ”— ๐Ÿ’โ€โ™‚๏ธ:

๐Ÿ™… โš™๏ธ

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ‘€ โšซ๏ธ, โžก ๐Ÿ› ๏ธ ๐Ÿ”ข ๐Ÿ“ฃ โš™๏ธ ๐Ÿ•โ” โžก & ๐Ÿ› ๏ธ ๐Ÿ, & โคด๏ธ FastAPI โœŠ ๐Ÿ’… ๐Ÿค™ ๐Ÿ”ข โฎ๏ธ โ˜‘ ๐Ÿ”ข, โŽ ๐Ÿ“Š โšช๏ธโžก๏ธ ๐Ÿ“จ.

๐Ÿค™, ๐ŸŒ (โš–๏ธ ๐Ÿ†) ๐Ÿ•ธ ๐Ÿ› ๏ธ ๐Ÿ‘ท ๐Ÿ‘‰ ๐ŸŽ ๐ŸŒŒ.

๐Ÿ‘† ๐Ÿ™… ๐Ÿค™ ๐Ÿ‘ˆ ๐Ÿ”ข ๐Ÿ”—. ๐Ÿ‘ซ ๐Ÿค™ ๐Ÿ‘† ๐Ÿ› ๏ธ (๐Ÿ‘‰ ๐Ÿ’ผ, FastAPI).

โฎ๏ธ ๐Ÿ”— ๐Ÿ’‰ โš™๏ธ, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ’ฌ FastAPI ๐Ÿ‘ˆ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ”ข "๐Ÿช€" ๐Ÿ”› ๐Ÿ•ณ ๐Ÿ™† ๐Ÿ‘ˆ ๐Ÿ”œ ๐Ÿ› ๏ธ โญ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ”ข, & FastAPI ๐Ÿ”œ โœŠ ๐Ÿ’… ๐Ÿ› ๏ธ โšซ๏ธ & "๐Ÿ’‰" ๐Ÿ.

๐ŸŽ โš  โš– ๐Ÿ‘‰ ๐ŸŽ ๐Ÿ’ญ "๐Ÿ”— ๐Ÿ’‰":

  • โ„น
  • ๐Ÿ•โ€๐Ÿฆบ
  • ๐Ÿ•โ€๐Ÿฆบ
  • ๐Ÿ’‰
  • ๐Ÿฆฒ

FastAPI ๐Ÿ”Œ-๐Ÿ”Œ

๐Ÿ› ๏ธ & "๐Ÿ”Œ-"โ“‚ ๐Ÿ’ช ๐Ÿ— โš™๏ธ ๐Ÿ”— ๐Ÿ’‰ โš™๏ธ. โœ‹๏ธ ๐Ÿ‘, ๐Ÿ“ค ๐Ÿค™ ๐Ÿ™…โ€โ™‚ ๐Ÿ’ช โœ "๐Ÿ”Œ-๐Ÿ”Œ", โš™๏ธ ๐Ÿ”— โšซ๏ธ ๐Ÿ’ช ๐Ÿ“ฃ โ™พ ๐Ÿ”ข ๐Ÿ› ๏ธ & ๐Ÿ”— ๐Ÿ‘ˆ โ–ถ๏ธ๏ธ ๐Ÿ’ช ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ”ข.

& ๐Ÿ”— ๐Ÿ’ช โœ ๐Ÿ“ถ ๐Ÿ™… & ๐Ÿ‹๏ธ ๐ŸŒŒ ๐Ÿ‘ˆ โœ” ๐Ÿ‘† ๐Ÿ—„ ๐Ÿ ๐Ÿ“ฆ ๐Ÿ‘† ๐Ÿ’ช, & ๐Ÿ› ๏ธ ๐Ÿ‘ซ โฎ๏ธ ๐Ÿ‘† ๐Ÿ› ๏ธ ๐Ÿ”ข ๐Ÿ‘ฉโ€โคโ€๐Ÿ‘จ โธ ๐Ÿ“Ÿ, ๐ŸŒ–.

๐Ÿ‘† ๐Ÿ”œ ๐Ÿ‘€ ๐Ÿ–ผ ๐Ÿ‘‰ โญ ๐Ÿ“ƒ, ๐Ÿ”ƒ ๐Ÿ”— & โ˜ ๐Ÿ’ฝ, ๐Ÿ’‚โ€โ™‚, โ™’๏ธ.

FastAPI ๐Ÿ”—

๐Ÿฆ ๐Ÿ”— ๐Ÿ’‰ โš™๏ธ โš’ FastAPI ๐Ÿ”— โฎ๏ธ:

  • ๐ŸŒ ๐Ÿ”— ๐Ÿ’ฝ
  • โ˜ ๐Ÿ’ฝ
  • ๐Ÿ”ข ๐Ÿ“ฆ
  • ๐Ÿ”ข ๐Ÿ”—
  • ๐Ÿค & โœ” โš™๏ธ
  • ๐Ÿ› ๏ธ โš™๏ธ โš– โš™๏ธ
  • ๐Ÿ“จ ๐Ÿ’ฝ ๐Ÿ’‰ โš™๏ธ
  • โ™’๏ธ.

๐Ÿ™… & ๐Ÿ‹๏ธ

๐Ÿ‘ ๐Ÿ”— ๐Ÿ”— ๐Ÿ’‰ โš™๏ธ ๐Ÿ“ถ ๐Ÿ™… ๐Ÿ”ฌ & โš™๏ธ, โšซ๏ธ ๐Ÿ“ถ ๐Ÿ‹๏ธ.

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”ฌ ๐Ÿ”— ๐Ÿ‘ˆ ๐Ÿ”„ ๐Ÿ’ช ๐Ÿ”ฌ ๐Ÿ”— ๐Ÿ‘ซ.

๐Ÿ”š, ๐Ÿ”— ๐ŸŒฒ ๐Ÿ”— ๐Ÿ—, & ๐Ÿ”— ๐Ÿ’‰ โš™๏ธ โœŠ ๐Ÿ’… ๐Ÿ”ฌ ๐ŸŒ ๐Ÿ‘‰ ๐Ÿ”— ๐Ÿ‘† (& ๐Ÿ‘ซ ๐ŸŽง-๐Ÿ”—) & ๐Ÿšš (๐Ÿ’‰) ๐Ÿ ๐Ÿ”  ๐Ÿ”.

๐Ÿ–ผ, โžก๏ธ ๐Ÿ’ฌ ๐Ÿ‘† โœ”๏ธ 4๏ธโƒฃ ๐Ÿ› ๏ธ ๐Ÿ”— (โžก ๐Ÿ› ๏ธ):

  • /items/public/
  • /items/private/
  • /users/{user_id}/activate
  • /items/pro/

โคด๏ธ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿšฎ ๐ŸŽ โœ” ๐Ÿ“„ ๐Ÿ”  ๐Ÿ‘ซ โฎ๏ธ ๐Ÿ”— & ๐ŸŽง-๐Ÿ”—:

graph TB

current_user(["current_user"])
active_user(["active_user"])
admin_user(["admin_user"])
paying_user(["paying_user"])

public["/items/public/"]
private["/items/private/"]
activate_user["/users/{user_id}/activate"]
pro_items["/items/pro/"]

current_user --> active_user
active_user --> admin_user
active_user --> paying_user

current_user --> public
active_user --> private
admin_user --> activate_user
paying_user --> pro_items

๐Ÿ› ๏ธ โฎ๏ธ ๐Ÿ—„

๐ŸŒ ๐Ÿ‘ซ ๐Ÿ”—, โช ๐Ÿ“ฃ ๐Ÿ‘ซ ๐Ÿ“„, ๐Ÿšฎ ๐Ÿ”ข, ๐Ÿ”ฌ, โ™’๏ธ. ๐Ÿ‘† โžก ๐Ÿ› ๏ธ.

FastAPI ๐Ÿ”œ โœŠ ๐Ÿ’… ๐Ÿšฎ โšซ๏ธ ๐ŸŒ ๐Ÿ—„ ๐Ÿ”—, ๐Ÿ‘ˆ โšซ๏ธ ๐ŸŽฆ ๐ŸŽ“ ๐Ÿงพ โš™๏ธ.