You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

15 KiB

🦏 🈸 - 💗 📁

🚥 👆 🏗 🈸 ⚖️ 🕸 🛠️, ️ 🛎 💼 👈 👆 💪 🚮 🌐 🔛 👁 📁.

FastAPI 🚚 🏪 🧰 📊 👆 🈸 🚧 🌐 💪.

!!! info 🚥 👆 👟 ➡️ 🏺, 👉 🔜 🌓 🏺 📗.

🖼 📁 📊

➡️ 💬 👆 ✔️ 📁 📊 💖 👉:

.
├── app
│   ├── __init__.py
│   ├── main.py
│   ├── dependencies.py
│   └── routers
│   │   ├── __init__.py
│   │   ├── items.py
│   │   └── users.py
│   └── internal
│       ├── __init__.py
│       └── admin.py

!!! tip 📤 📚 __init__.py 📁: 1️⃣ 🔠 📁 ⚖️ 📁.

👉 ⚫️❔ ✔ 🏭 📟 ⚪️➡️ 1️⃣ 📁 🔘 ➕1️⃣.

🖼, `app/main.py` 👆 💪 ✔️ ⏸ 💖:

```
from app.routers import items
```
  • app 📁 🔌 🌐. & ✔️ 🛁 📁 app/__init__.py, ️ "🐍 📦" (🗃 "🐍 🕹"): app.
  • 🔌 app/main.py 📁. 🔘 🐍 📦 (📁 ⏮️ 📁 __init__.py), ️ "🕹" 👈 📦: app.main.
  • 📤 app/dependencies.py 📁, 💖 app/main.py, ️ "🕹": app.dependencies.
  • 📤 📁 app/routers/ ⏮️ 1️⃣ 📁 __init__.py, ️ "🐍 📦": app.routers.
  • 📁 app/routers/items.py 🔘 📦, app/routers/,, 🔁: app.routers.items.
  • 🎏 ⏮️ app/routers/users.py, 1️⃣ 🔁: app.routers.users.
  • 📤 📁 app/internal/ ⏮️ 1️⃣ 📁 __init__.py, 1️⃣ "🐍 📦": app.internal.
  • & 📁 app/internal/admin.py 1️⃣ 🔁: app.internal.admin.

🎏 📁 📊 ⏮️ 🏤:

.
├── app                  # "app" is a Python package
│   ├── __init__.py      # this file makes "app" a "Python package"
│   ├── main.py          # "main" module, e.g. import app.main
│   ├── dependencies.py  # "dependencies" module, e.g. import app.dependencies
│   └── routers          # "routers" is a "Python subpackage"
│   │   ├── __init__.py  # makes "routers" a "Python subpackage"
│   │   ├── items.py     # "items" submodule, e.g. import app.routers.items
│   │   └── users.py     # "users" submodule, e.g. import app.routers.users
│   └── internal         # "internal" is a "Python subpackage"
│       ├── __init__.py  # makes "internal" a "Python subpackage"
│       └── admin.py     # "admin" submodule, e.g. import app.internal.admin

APIRouter

➡️ 💬 📁 💡 🚚 👩‍💻 🔁 /app/routers/users.py.

👆 💚 ✔️ 🛠️ 🔗 👆 👩‍💻 👽 ➡️ 🎂 📟, 🚧 ️ 🏗.

🍕 🎏 FastAPI 🈸/🕸 🛠️ (🍕 🎏 "🐍 📦").

👆 💪🛠️ 👈 🕹 ⚙️ APIRouter.

🗄 APIRouter

👆 🗄 ️ & ✍ "👐" 🎏 🌌 👆 🔜 ⏮️ 🎓 FastAPI:

{!../../../docs_src/bigger_applications/app/routers/users.py!}

🛠️ ⏮️ APIRouter

& ⤴️ 👆 ⚙️ 📣 👆 🛠️.

⚙️ 🎏 🌌 👆 🔜 ⚙️ FastAPI 🎓:

{!../../../docs_src/bigger_applications/app/routers/users.py!}

👆 💪 💭 APIRouter "🐩 FastAPI" 🎓.

🌐 🎏 🎛 🐕‍🦺.

🌐 🎏 parameters, responses, dependencies, tags, ️.

!!! tip 👉 🖼, 🔢 🤙 router, 👆 💪 📛 👐 👆 💚.

👥 🔜 🔌 👉 APIRouter 👑 FastAPI 📱, 🥇, ➡️ 🔗 & 1️⃣ APIRouter.

🔗

👥 👀 👈 👥 🔜 💪 🔗 ⚙️ 📚 🥉 🈸.

👥 🚮 👫 👫 👍 dependencies 🕹 (app/dependencies.py).

👥 🔜 🔜 ⚙️ 🙅 🔗🛃 X-Token 🎚:

{!../../../docs_src/bigger_applications/app/dependencies.py!}

!!! tip 👥 ⚙️ 💭 🎚 📉 👉 🖼.

✋️ 🎰 💼 👆 🔜 🤚 👍 🏁 ⚙️ 🛠️ [💂‍♂ 🚙](./security/index.md){.internal-link target=_blank}.

1️⃣ 🕹 ⏮️ APIRouter

➡️ 💬 👆 ✔️ 🔗 💡 🚚 "🏬" ➡️ 👆 🈸 🕹 app/routers/items.py.

👆 ✔️ 🛠️ :

  • /items/
  • /items/{item_id}

🌐 🎏 📊 ⏮️ app/routers/users.py.

👥 💚 🙃 & 📉 📟 🍖.

👥 💭 🌐 🛠️ 👉 🕹 ✔️ 🎏:

  • prefix: /items.
  • tags: (1️⃣ 🔖: items).
  • responses.
  • dependencies: 👫 🌐 💪 👈 X-Token 🔗 👥 ✍.

, ↩️ 🌐 👈 🔠 🛠️, 👥 💪 🚮 APIRouter.

{!../../../docs_src/bigger_applications/app/routers/items.py!}

🔠 🛠️ ✔️ ▶️ ⏮️ /, 💖:

@router.get("/{item_id}")
async def read_item(item_id: str):
    ...

...🔡 🔜 🚫 🔌 🏁 /.

, 🔡 👉 💼 /items.

👥 💪 🚮 📇 tags & responses 👈 🔜🌐 🛠️ 🔌 👉 📻.

& 👥 💪 🚮 📇 dependencies 👈 🔜 🚮 🌐 🛠️ 📻 & 🔜 🛠️/ 🔠 📨👫.

!!! tip 🗒 👈, 🌅 💖 🔗 ➡ 🛠️ 👨‍🎨{.internal-link target=_blank}, 🙅‍♂ 💲 🔜 🚶‍♀️ 👆 🛠️ 🔢.

🔚 🏁 👈 🏬🔜:

  • /items/
  • /items/{item_id}

...👥 🎯.

  • 👫 🔜⏮️ 📇 🔖 👈 🔌 👁 🎻 "items".
    • 👫 "🔖" ✴️🏧 🎓 🧾 ⚙️ (⚙️ 🗄).
  • 🌐 👫 🔜 🔌 🔁 responses.
  • 🌐 👫 🛠️ 🔜 ✔️ 📇 dependencies 🔬/🛠️👫.
    • 🚥 👆 📣 🔗 🎯 🛠️, 👫 🔜 🛠️ 💁‍♂️.
    • 📻 🔗 🛠️ 🥇, ⤴️ dependencies 👨‍🎨{.internal-link target=_blank}, & ⤴️ 😐 🔢 🔗.
    • 👆 💪 🚮 Security 🔗 ⏮️ scopes{.internal-link target=_blank}.

!!! tip ✔️ dependencies APIRouter 💪 ⚙️, 🖼, 🚚 🤝 🎂 👪 🛠️. 🚥 🔗 🚫 🚮 📦 🔠 1️⃣ 👫.

!!! check prefix, tags, responses, & dependencies 🔢 (📚 🎏 💼) ⚒ ➡️ FastAPI👆 📟 .

🗄 🔗

👉 📟 👨‍❤‍👨 🕹 app.routers.items, 📁 app/routers/items.py.

& 👥 💪 🤚 🔗 🔢 ➡️ 🕹 app.dependencies, 📁 app/dependencies.py.

👥 ⚙️ ⚖ 🗄 ⏮️ .. 🔗:

{!../../../docs_src/bigger_applications/app/routers/items.py!}

⚖ 🗄 👷

!!! tip 🚥 👆 💭 👌 🗄 👷, 😣📄 🔛.

👁 ❣ ., 💖:

from .dependencies import get_token_header

🔜 ⛓:

  • ▶️ 🎏 📦 👈 👉 🕹 (📁 app/routers/items.py) 🖖 (📁 app/routers/)...
  • 🔎 🕹 dependencies (👽 📁 app/routers/dependencies.py)...
  • & ➡️ ️, 🗄 🔢 get_token_header.

👈 📁 🚫 🔀, 👆 🔗 📁 app/dependencies.py.

💭 👆 📱/📁 📊 👀 💖:


2️⃣.., 💖:

from ..dependencies import get_token_header

⛓:

  • ▶️ 🎏 📦 👈 👉 🕹 (📁 app/routers/items.py) 🖖 (📁 app/routers/)...
  • 🚶 👪 📦 (📁 app/)...
  • & 📤, 🔎 🕹 dependencies (📁 app/dependencies.py)...
  • & ➡️ ️, 🗄 🔢 get_token_header.

👈 👷 👶


🎏 🌌, 🚥 👥 ✔️ ⚙️ 3️⃣..., 💖:

from ...dependencies import get_token_header

that 🔜 ⛓:

  • ▶️ 🎏 📦 👈 👉 🕹 (📁 app/routers/items.py) 🖖 (📁 app/routers/)...
  • 🚶 👪 📦 (📁 app/)...
  • ⤴️ 🚶 👪 👈 📦 (📤 🙅‍♂ 👪 📦, app 🔝 🎚 👶)...
  • & 📤, 🔎 🕹 dependencies (📁 app/dependencies.py)...
  • & ➡️ ️, 🗄 🔢 get_token_header.

👈 🔜 🔗 📦 🔛 app/, ⏮️ 🚮 👍 📁 __init__.py, ️. 👥 🚫 ✔️ 👈. , 👈 🔜 🚮 👆 🖼. 👶

🔜 👆 💭 👷, 👆 💪 ⚙️ ⚖ 🗄 👆 👍 📱 🙅‍♂ 🤔 🏗 👫. 👶

🚮 🛃 tags, responses, & dependencies

👥 🚫 🔡 /items 🚫 tags=["items"] 🔠 🛠️ ↩️ 👥 🚮 👫 APIRouter.

👥 💪 🚮 🌅 tags 👈 🔜🎯 🛠️, & responses 🎯 👈 🛠️:

{!../../../docs_src/bigger_applications/app/routers/items.py!}

!!! tip 👉 🏁🛠️ 🔜 ✔️ 🌀 🔖: ["items", "custom"].

 & ⚫️ 🔜 ✔️ 👯‍♂️ 📨 🧾, 1️⃣ `404` & 1️⃣ `403`.

👑 FastAPI

🔜, ➡️ 👀 🕹 app/main.py.

📥 🌐 👆 🗄 & ⚙️ 🎓 FastAPI.

👉 🔜 👑 📁 👆 🈸 👈 👔 🌐 👯‍♂️.

& 🏆 👆🔜 🔜 🖖 🚮 👍 🎯 🕹, 👑 📁 🔜 🙅.

🗄 FastAPI

👆 🗄 & ✍ FastAPI 🎓 🛎.

& 👥 💪 📣 🌐 🔗{.internal-link target=_blank} 👈 🔜 🌀 ⏮️ 🔗 🔠 APIRouter:

{!../../../docs_src/bigger_applications/app/main.py!}

🗄 APIRouter

🔜 👥 🗄 🎏 🔁 👈 ✔️ APIRouterⓂ:

{!../../../docs_src/bigger_applications/app/main.py!}

📁 app/routers/users.py & app/routers/items.py 🔁 👈 🍕 🎏 🐍 📦 app, 👥 💪 ⚙️ 👁 ❣ . 🗄 👫 ⚙️ "⚖ 🗄".

🏭 👷

📄:

from .routers import items, users

⛓:

  • ▶️ 🎏 📦 👈 👉 🕹 (📁 app/main.py) 🖖 (📁 app/)...
  • 👀 📦 routers (📁 app/routers/)...
  • & ➡️ ️, 🗄 🔁 items (📁 app/routers/items.py) & users (📁 app/routers/users.py)...

🕹 items 🔜 ✔️ 🔢 router (items.router). 👉 🎏 1️⃣ 👥📁 app/routers/items.py, APIRouter 🎚.

& ⤴️ 👥 🎏 🕹 users.

👥 💪 🗄 👫 💖:

from app.routers import items, users

!!! info 🥇 "⚖ 🗄":

```Python
from .routers import items, users
```

🥈 ⏬ "🎆 🗄":

```Python
from app.routers import items, users
```

💡 🌅 🔃 🐍 📦 &amp; 🕹, ✍ <a href="https://docs.python.org/3/tutorial/modules.html" class="external-link" target="_blank">🛂 🐍 🧾 🔃 🕹</a>.

📛 💥

👥 🏭 🔁 items 🔗, ↩️ 🏭 🚮 🔢 router.

👉 ↩️ 👥 ✔️ 1️⃣ 🔢 📛 router 🔁 users.

🚥 👥 ✔️ 🗄 1️⃣ ⏮️ 🎏, 💖:

from .routers.items import router
from .routers.users import router

router ➡️ users 🔜 📁 1️⃣ ➡️ items & 👥 🚫🔜 💪 ⚙️ 👫 🎏 🕰.

, 💪 ⚙️ 👯‍♂️ 👫 🎏 📁, 👥 🗄 🔁 🔗:

{!../../../docs_src/bigger_applications/app/main.py!}

🔌 APIRouterusers & items

🔜, ➡️ 🔌 router➡️ 🔁 users & items:

{!../../../docs_src/bigger_applications/app/main.py!}

!!! info users.router 🔌 APIRouter 🔘 📁 app/routers/users.py.

 &amp; `items.router` 🔌 `APIRouter` 🔘 📁 `app/routers/items.py`.

⏮️ app.include_router() 👥 💪 🚮 🔠 APIRouter 👑 FastAPI 🈸.

🔜 🔌 🌐 🛣 ➡️ 👈 📻 🍕 ️.

!!! note "📡 ℹ" 🔜 🤙 🔘🛠️ 🔠 🛠️ 👈 📣 APIRouter.

, ⛅ 🎑, ⚫️ 🔜 🤙 👷 🚥 🌐 🎏 👁 📱.

!!! check 👆 🚫 ✔️ 😟 🔃 🎭 🕐 📻.

👉 🔜 ✊ ⏲ &amp; 🔜 🕴 🔨 🕴.

⚫️ 🏆 🚫 📉 🎭. 👶

🔌 APIRouter ⏮️ 🛃 prefix, tags, responses, & dependencies

🔜, ➡️ 🌈 👆 🏢 🤝 👆 app/internal/admin.py 📁.

🔌 APIRouter ⏮️ 📡 🛠️ 👈 👆 🏢 💰 🖖 📚 🏗.

👉 🖼 🔜 💎 🙅. ➡️ 💬 👈 ↩️ 💰 ⏮️ 🎏 🏗 🏢, 👥 🚫🔜 🔀 ️ & 🚮 prefix, dependencies, tags, ️. 🔗 APIRouter:

{!../../../docs_src/bigger_applications/app/internal/admin.py!}

👥 💚🛃 prefix 🕐 APIRouter 👈 🌐 🚮 🛠️ ▶️ ⏮️ /admin, 👥 💚 🔐 ⏮️ dependencies 👥 ✔️ 👉 🏗, & 👥 💚 🔌 tags & responses.

👥 💪 📣 🌐 👈 🍵 ✔️ 🔀 ⏮️ APIRouter 🚶‍♀️ 👈 🔢 app.include_router():

{!../../../docs_src/bigger_applications/app/main.py!}

👈 🌌, ⏮️ APIRouter 🔜 🚧 ⚗, 👥 💪 💰 👈 🎏 app/internal/admin.py 📁 ⏮️ 🎏 🏗 🏢.

🏁 👈 👆 📱, 🔠 🛠️ ➡️ admin 🕹 🔜 ✔️:

  • 🔡 /admin.
  • 🔖 admin.
  • 🔗 get_token_header.
  • 📨 418. 👶

👈 🔜 🕴 📉 👈 APIRouter 👆 📱, 🚫 🙆 🎏 📟 👈 ⚙️ ️.

, 🖼, 🎏 🏗 💪 ⚙️ 🎏 APIRouter ⏮️ 🎏 🤝 👩‍🔬.

🔌 🛠️

👥 💪 🚮 🛠️ 🔗 FastAPI 📱.

📥 👥 ️... 🎦 👈 👥 💪 🤷:

{!../../../docs_src/bigger_applications/app/main.py!}

& 🔜 👷 ☑, 👯‍♂️ ⏮️ 🌐 🎏 🛠️ 🚮 ⏮️ app.include_router().

!!! info "📶 📡 ℹ" 🗒: 👉 📶 📡👈 👆 🎲 💪 🚶.

---

 `APIRouter`Ⓜ 🚫 "🗻", 👫 🚫 👽 ⚪️➡️ 🎂 🈸.

👉 ↩️ 👥 💚 🔌 👫 *➡ 🛠️* 🗄 🔗 &amp; 👩‍💻 🔢.

👥 🚫🔜 ❎ 👫 &amp; "🗻" 👫 ➡ 🎂, *➡ 🛠️* "🖖" (🏤-✍), 🚫 🔌 🔗.

🏧 🛠️ 🩺

🔜, 🏃 uvicorn, ⚙️ 🕹 app.main & 🔢 app:

$ uvicorn app.main:app --reload

<span style="color: green;">INFO</span>:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

& 📂 🩺 http://127.0.0.1:8000/docs.

👆 🔜 👀 🏧 🛠️ 🩺, ➡️ 🌐 🔁, ⚙️ ☑ ➡ (& 🔡) & ☑ 🔖:

🔌 🎏 📻 💗 🕰 ⏮️ 🎏 prefix

👆 💪 ⚙️ .include_router() 💗 🕰 ⏮️ 🎏 📻 ⚙️ 🎏 🔡.

👉 💪 ⚠, 🖼, 🎦 🎏 🛠️ 🔽 🎏 🔡, /api/v1 & /api/latest.

👉 🏧 ⚙️ 👈 👆 5️⃣📆 🚫 🤙 💪, 📤 💼 👆.

🔌 APIRouter 1️⃣

🎏 🌌 👆 💪 🔌 APIRouter FastAPI 🈸, 👆 💪 🔌 APIRouter 1️⃣ APIRouter ⚙️:

router.include_router(other_router)

💭 👆 ️ ⏭ 🔌 router FastAPI 📱, 👈 🛠️ ➡️ other_router 🔌.