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
🤓 Other versions and variants
from typing import Annotated

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: Annotated[dict, Depends(common_parameters)]):
    return commons


@app.get("/users/")
async def read_users(commons: Annotated[dict, Depends(common_parameters)]):
    return commons
from typing import Annotated, 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: Annotated[dict, Depends(common_parameters)]):
    return commons


@app.get("/users/")
async def read_users(commons: Annotated[dict, Depends(common_parameters)]):
    return commons
from typing import Union

from fastapi import Depends, FastAPI
from typing_extensions import Annotated

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: Annotated[dict, Depends(common_parameters)]):
    return commons


@app.get("/users/")
async def read_users(commons: Annotated[dict, Depends(common_parameters)]):
    return commons

Tip

Prefer to use the Annotated version if possible.

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
🤓 Other versions and variants
from typing import Annotated

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: Annotated[dict, Depends(common_parameters)]):
    return commons


@app.get("/users/")
async def read_users(commons: Annotated[dict, Depends(common_parameters)]):
    return commons
from typing import Annotated, 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: Annotated[dict, Depends(common_parameters)]):
    return commons


@app.get("/users/")
async def read_users(commons: Annotated[dict, Depends(common_parameters)]):
    return commons
from typing import Union

from fastapi import Depends, FastAPI
from typing_extensions import Annotated

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: Annotated[dict, Depends(common_parameters)]):
    return commons


@app.get("/users/")
async def read_users(commons: Annotated[dict, Depends(common_parameters)]):
    return commons

Tip

Prefer to use the Annotated version if possible.

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
🤓 Other versions and variants
from typing import Annotated

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: Annotated[dict, Depends(common_parameters)]):
    return commons


@app.get("/users/")
async def read_users(commons: Annotated[dict, Depends(common_parameters)]):
    return commons
from typing import Annotated, 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: Annotated[dict, Depends(common_parameters)]):
    return commons


@app.get("/users/")
async def read_users(commons: Annotated[dict, Depends(common_parameters)]):
    return commons
from typing import Union

from fastapi import Depends, FastAPI
from typing_extensions import Annotated

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: Annotated[dict, Depends(common_parameters)]):
    return commons


@app.get("/users/")
async def read_users(commons: Annotated[dict, Depends(common_parameters)]):
    return commons

Tip

Prefer to use the Annotated version if possible.

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 🔜 ✊ 💅 🚮 ⚫️ 🌐 🗄 🔗, 👈 ⚫️ 🎦 🎓 🧾 ⚙️.