Skip to content

๐Ÿฆ ๐Ÿˆธ - ๐Ÿ’— ๐Ÿ“

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ— ๐Ÿˆธ โš–๏ธ ๐Ÿ•ธ ๐Ÿ› ๏ธ, โšซ๏ธ ๐Ÿ›Ž ๐Ÿ’ผ ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿšฎ ๐ŸŒ ๐Ÿ”› ๐Ÿ‘ ๐Ÿ“.

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:

from fastapi import APIRouter

router = APIRouter()


@router.get("/users/", tags=["users"])
async def read_users():
    return [{"username": "Rick"}, {"username": "Morty"}]


@router.get("/users/me", tags=["users"])
async def read_user_me():
    return {"username": "fakecurrentuser"}


@router.get("/users/{username}", tags=["users"])
async def read_user(username: str):
    return {"username": username}

โžก ๐Ÿ› ๏ธ โฎ๏ธ APIRouter

& โคด๏ธ ๐Ÿ‘† โš™๏ธ โšซ๏ธ ๐Ÿ“ฃ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ.

โš™๏ธ โšซ๏ธ ๐ŸŽ ๐ŸŒŒ ๐Ÿ‘† ๐Ÿ”œ โš™๏ธ FastAPI ๐ŸŽ“:

from fastapi import APIRouter

router = APIRouter()


@router.get("/users/", tags=["users"])
async def read_users():
    return [{"username": "Rick"}, {"username": "Morty"}]


@router.get("/users/me", tags=["users"])
async def read_user_me():
    return {"username": "fakecurrentuser"}


@router.get("/users/{username}", tags=["users"])
async def read_user(username: str):
    return {"username": username}

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ’ญ APIRouter "๐Ÿฉ FastAPI" ๐ŸŽ“.

๐ŸŒ ๐ŸŽ ๐ŸŽ› ๐Ÿ•โ€๐Ÿฆบ.

๐ŸŒ ๐ŸŽ parameters, responses, dependencies, tags, โ™’๏ธ.

Tip

๐Ÿ‘‰ ๐Ÿ–ผ, ๐Ÿ”ข ๐Ÿค™ router, โœ‹๏ธ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“› โšซ๏ธ ๐Ÿ‘ ๐Ÿ‘† ๐Ÿ’š.

๐Ÿ‘ฅ ๐Ÿ”œ ๐Ÿ”Œ ๐Ÿ‘‰ APIRouter ๐Ÿ‘‘ FastAPI ๐Ÿ“ฑ, โœ‹๏ธ ๐Ÿฅ‡, โžก๏ธ โœ… ๐Ÿ”— & โž•1๏ธโƒฃ APIRouter.

๐Ÿ”—

๐Ÿ‘ฅ ๐Ÿ‘€ ๐Ÿ‘ˆ ๐Ÿ‘ฅ ๐Ÿ”œ ๐Ÿ’ช ๐Ÿ”— โš™๏ธ ๐Ÿ“š ๐Ÿฅ‰ ๐Ÿˆธ.

๐Ÿ‘ฅ ๐Ÿšฎ ๐Ÿ‘ซ ๐Ÿ‘ซ ๐Ÿ‘ dependencies ๐Ÿ•น (app/dependencies.py).

๐Ÿ‘ฅ ๐Ÿ”œ ๐Ÿ”œ โš™๏ธ ๐Ÿ™… ๐Ÿ”— โœ ๐Ÿ›ƒ X-Token ๐ŸŽš:

from fastapi import Header, HTTPException


async def get_token_header(x_token: str = Header()):
    if x_token != "fake-super-secret-token":
        raise HTTPException(status_code=400, detail="X-Token header invalid")


async def get_query_token(token: str):
    if token != "jessica":
        raise HTTPException(status_code=400, detail="No Jessica token provided")

Tip

๐Ÿ‘ฅ โš™๏ธ ๐Ÿ’ญ ๐ŸŽš ๐Ÿ“‰ ๐Ÿ‘‰ ๐Ÿ–ผ.

โœ‹๏ธ ๐ŸŽฐ ๐Ÿ’ผ ๐Ÿ‘† ๐Ÿ”œ ๐Ÿคš ๐Ÿ‘ ๐Ÿ โš™๏ธ ๐Ÿ› ๏ธ ๐Ÿ’‚โ€โ™‚ ๐Ÿš™.

โž•1๏ธโƒฃ ๐Ÿ•น โฎ๏ธ APIRouter

โžก๏ธ ๐Ÿ’ฌ ๐Ÿ‘† โœ”๏ธ ๐Ÿ”— ๐Ÿ’ก ๐Ÿšš "๐Ÿฌ" โšช๏ธโžก๏ธ ๐Ÿ‘† ๐Ÿˆธ ๐Ÿ•น app/routers/items.py.

๐Ÿ‘† โœ”๏ธ โžก ๐Ÿ› ๏ธ :

  • /items/
  • /items/{item_id}

โšซ๏ธ ๐ŸŒ ๐ŸŽ ๐Ÿ“Š โฎ๏ธ app/routers/users.py.

โœ‹๏ธ ๐Ÿ‘ฅ ๐Ÿ’š ๐Ÿ™ƒ & ๐Ÿ“‰ ๐Ÿ“Ÿ ๐Ÿ–.

๐Ÿ‘ฅ ๐Ÿ’ญ ๐ŸŒ โžก ๐Ÿ› ๏ธ ๐Ÿ‘‰ ๐Ÿ•น โœ”๏ธ ๐ŸŽ:

  • โžก prefix: /items.
  • tags: (1๏ธโƒฃ ๐Ÿ”–: items).
  • โž• responses.
  • dependencies: ๐Ÿ‘ซ ๐ŸŒ ๐Ÿ’ช ๐Ÿ‘ˆ X-Token ๐Ÿ”— ๐Ÿ‘ฅ โœ.

, โ†ฉ๏ธ โŽ ๐ŸŒ ๐Ÿ‘ˆ ๐Ÿ”  โžก ๐Ÿ› ๏ธ, ๐Ÿ‘ฅ ๐Ÿ’ช ๐Ÿšฎ โšซ๏ธ APIRouter.

from fastapi import APIRouter, Depends, HTTPException

from ..dependencies import get_token_header

router = APIRouter(
    prefix="/items",
    tags=["items"],
    dependencies=[Depends(get_token_header)],
    responses={404: {"description": "Not found"}},
)


fake_items_db = {"plumbus": {"name": "Plumbus"}, "gun": {"name": "Portal Gun"}}


@router.get("/")
async def read_items():
    return fake_items_db


@router.get("/{item_id}")
async def read_item(item_id: str):
    if item_id not in fake_items_db:
        raise HTTPException(status_code=404, detail="Item not found")
    return {"name": fake_items_db[item_id]["name"], "item_id": item_id}


@router.put(
    "/{item_id}",
    tags=["custom"],
    responses={403: {"description": "Operation forbidden"}},
)
async def update_item(item_id: str):
    if item_id != "plumbus":
        raise HTTPException(
            status_code=403, detail="You can only update the item: plumbus"
        )
    return {"item_id": item_id, "name": "The great Plumbus"}

โžก ๐Ÿ”  โžก ๐Ÿ› ๏ธ โœ”๏ธ โ–ถ๏ธ โฎ๏ธ /, ๐Ÿ’–:

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

...๐Ÿ”ก ๐Ÿ”œ ๐Ÿšซ ๐Ÿ”Œ ๐Ÿ /.

, ๐Ÿ”ก ๐Ÿ‘‰ ๐Ÿ’ผ /items.

๐Ÿ‘ฅ ๐Ÿ’ช ๐Ÿšฎ ๐Ÿ“‡ tags & โž• responses ๐Ÿ‘ˆ ๐Ÿ”œ โœ” ๐ŸŒ โžก ๐Ÿ› ๏ธ ๐Ÿ”Œ ๐Ÿ‘‰ ๐Ÿ“ป.

& ๐Ÿ‘ฅ ๐Ÿ’ช ๐Ÿšฎ ๐Ÿ“‡ dependencies ๐Ÿ‘ˆ ๐Ÿ”œ ๐Ÿšฎ ๐ŸŒ โžก ๐Ÿ› ๏ธ ๐Ÿ“ป & ๐Ÿ”œ ๐Ÿ› ๏ธ/โŽ ๐Ÿ”  ๐Ÿ“จ โš’ ๐Ÿ‘ซ.

Tip

๐Ÿ—’ ๐Ÿ‘ˆ, ๐ŸŒ… ๐Ÿ’– ๐Ÿ”— โžก ๐Ÿ› ๏ธ ๐Ÿ‘จโ€๐ŸŽจ, ๐Ÿ™…โ€โ™‚ ๐Ÿ’ฒ ๐Ÿ”œ ๐Ÿšถโ€โ™€๏ธ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ”ข.

๐Ÿ”š ๐Ÿ ๐Ÿ‘ˆ ๐Ÿฌ โžก ๐Ÿ”œ:

  • /items/
  • /items/{item_id}

...๐Ÿ‘ฅ ๐ŸŽฏ.

  • ๐Ÿ‘ซ ๐Ÿ”œ โ„ข โฎ๏ธ ๐Ÿ“‡ ๐Ÿ”– ๐Ÿ‘ˆ ๐Ÿ”Œ ๐Ÿ‘ ๐ŸŽป "items".
    • ๐Ÿ‘ซ "๐Ÿ”–" โœด๏ธ โš  ๐Ÿง ๐ŸŽ“ ๐Ÿงพ โš™๏ธ (โš™๏ธ ๐Ÿ—„).
  • ๐ŸŒ ๐Ÿ‘ซ ๐Ÿ”œ ๐Ÿ”Œ ๐Ÿ” responses.
  • ๐ŸŒ ๐Ÿ‘ซ โžก ๐Ÿ› ๏ธ ๐Ÿ”œ โœ”๏ธ ๐Ÿ“‡ dependencies ๐Ÿ”ฌ/๐Ÿ› ๏ธ โญ ๐Ÿ‘ซ.

Tip

โœ”๏ธ dependencies APIRouter ๐Ÿ’ช โš™๏ธ, ๐Ÿ–ผ, ๐Ÿšš ๐Ÿค ๐ŸŽ‚ ๐Ÿ‘ช โžก ๐Ÿ› ๏ธ. ๐Ÿšฅ ๐Ÿ”— ๐Ÿšซ ๐Ÿšฎ ๐Ÿ“ฆ ๐Ÿ”  1๏ธโƒฃ ๐Ÿ‘ซ.

Check

prefix, tags, responses, & dependencies ๐Ÿ”ข (๐Ÿ“š ๐ŸŽ ๐Ÿ’ผ) โš’ โšช๏ธโžก๏ธ FastAPI โ„น ๐Ÿ‘† โŽ ๐Ÿ“Ÿ โŽ.

๐Ÿ—„ ๐Ÿ”—

๐Ÿ‘‰ ๐Ÿ“Ÿ ๐Ÿ‘จโ€โคโ€๐Ÿ‘จ ๐Ÿ•น app.routers.items, ๐Ÿ“ app/routers/items.py.

& ๐Ÿ‘ฅ ๐Ÿ’ช ๐Ÿคš ๐Ÿ”— ๐Ÿ”ข โšช๏ธโžก๏ธ ๐Ÿ•น app.dependencies, ๐Ÿ“ app/dependencies.py.

๐Ÿ‘ฅ โš™๏ธ โš– ๐Ÿ—„ โฎ๏ธ .. ๐Ÿ”—:

from fastapi import APIRouter, Depends, HTTPException

from ..dependencies import get_token_header

router = APIRouter(
    prefix="/items",
    tags=["items"],
    dependencies=[Depends(get_token_header)],
    responses={404: {"description": "Not found"}},
)


fake_items_db = {"plumbus": {"name": "Plumbus"}, "gun": {"name": "Portal Gun"}}


@router.get("/")
async def read_items():
    return fake_items_db


@router.get("/{item_id}")
async def read_item(item_id: str):
    if item_id not in fake_items_db:
        raise HTTPException(status_code=404, detail="Item not found")
    return {"name": fake_items_db[item_id]["name"], "item_id": item_id}


@router.put(
    "/{item_id}",
    tags=["custom"],
    responses={403: {"description": "Operation forbidden"}},
)
async def update_item(item_id: str):
    if item_id != "plumbus":
        raise HTTPException(
            status_code=403, detail="You can only update the item: plumbus"
        )
    return {"item_id": item_id, "name": "The great Plumbus"}

โ” โš– ๐Ÿ—„ ๐Ÿ‘ท

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 ๐ŸŽฏ ๐Ÿ‘ˆ โžก ๐Ÿ› ๏ธ:

from fastapi import APIRouter, Depends, HTTPException

from ..dependencies import get_token_header

router = APIRouter(
    prefix="/items",
    tags=["items"],
    dependencies=[Depends(get_token_header)],
    responses={404: {"description": "Not found"}},
)


fake_items_db = {"plumbus": {"name": "Plumbus"}, "gun": {"name": "Portal Gun"}}


@router.get("/")
async def read_items():
    return fake_items_db


@router.get("/{item_id}")
async def read_item(item_id: str):
    if item_id not in fake_items_db:
        raise HTTPException(status_code=404, detail="Item not found")
    return {"name": fake_items_db[item_id]["name"], "item_id": item_id}


@router.put(
    "/{item_id}",
    tags=["custom"],
    responses={403: {"description": "Operation forbidden"}},
)
async def update_item(item_id: str):
    if item_id != "plumbus":
        raise HTTPException(
            status_code=403, detail="You can only update the item: plumbus"
        )
    return {"item_id": item_id, "name": "The great Plumbus"}

Tip

๐Ÿ‘‰ ๐Ÿ โžก ๐Ÿ› ๏ธ ๐Ÿ”œ โœ”๏ธ ๐ŸŒ€ ๐Ÿ”–: ["items", "custom"].

& โšซ๏ธ ๐Ÿ”œ โœ”๏ธ ๐Ÿ‘ฏโ€โ™‚๏ธ ๐Ÿ“จ ๐Ÿงพ, 1๏ธโƒฃ 404 & 1๏ธโƒฃ 403.

๐Ÿ‘‘ FastAPI

๐Ÿ”œ, โžก๏ธ ๐Ÿ‘€ ๐Ÿ•น app/main.py.

๐Ÿ“ฅ ๐ŸŒโ” ๐Ÿ‘† ๐Ÿ—„ & โš™๏ธ ๐ŸŽ“ FastAPI.

๐Ÿ‘‰ ๐Ÿ”œ ๐Ÿ‘‘ ๐Ÿ“ ๐Ÿ‘† ๐Ÿˆธ ๐Ÿ‘ˆ ๐Ÿ‘” ๐ŸŒ ๐Ÿ‘ฏโ€โ™‚๏ธ.

& ๐Ÿ† ๐Ÿ‘† โš› ๐Ÿ”œ ๐Ÿ”œ ๐Ÿ–– ๐Ÿšฎ ๐Ÿ‘ ๐ŸŽฏ ๐Ÿ•น, ๐Ÿ‘‘ ๐Ÿ“ ๐Ÿ”œ ๐Ÿ™….

๐Ÿ—„ FastAPI

๐Ÿ‘† ๐Ÿ—„ & โœ FastAPI ๐ŸŽ“ ๐Ÿ›Ž.

& ๐Ÿ‘ฅ ๐Ÿ’ช ๐Ÿ“ฃ ๐ŸŒ ๐Ÿ”— ๐Ÿ‘ˆ ๐Ÿ”œ ๐ŸŒ€ โฎ๏ธ ๐Ÿ”— ๐Ÿ”  APIRouter:

from fastapi import Depends, FastAPI

from .dependencies import get_query_token, get_token_header
from .internal import admin
from .routers import items, users

app = FastAPI(dependencies=[Depends(get_query_token)])


app.include_router(users.router)
app.include_router(items.router)
app.include_router(
    admin.router,
    prefix="/admin",
    tags=["admin"],
    dependencies=[Depends(get_token_header)],
    responses={418: {"description": "I'm a teapot"}},
)


@app.get("/")
async def root():
    return {"message": "Hello Bigger Applications!"}

๐Ÿ—„ APIRouter

๐Ÿ”œ ๐Ÿ‘ฅ ๐Ÿ—„ ๐ŸŽ ๐Ÿ” ๐Ÿ‘ˆ โœ”๏ธ APIRouterโ“‚:

from fastapi import Depends, FastAPI

from .dependencies import get_query_token, get_token_header
from .internal import admin
from .routers import items, users

app = FastAPI(dependencies=[Depends(get_query_token)])


app.include_router(users.router)
app.include_router(items.router)
app.include_router(
    admin.router,
    prefix="/admin",
    tags=["admin"],
    dependencies=[Depends(get_token_header)],
    responses={418: {"description": "I'm a teapot"}},
)


@app.get("/")
async def root():
    return {"message": "Hello Bigger Applications!"}

๐Ÿ“ 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

๐Ÿฅ‡ โฌ "โš– ๐Ÿ—„":

from .routers import items, users

๐Ÿฅˆ โฌ "๐ŸŽ† ๐Ÿ—„":

from app.routers import items, users

๐Ÿ’ก ๐ŸŒ… ๐Ÿ”ƒ ๐Ÿ ๐Ÿ“ฆ & ๐Ÿ•น, โœ ๐Ÿ›‚ ๐Ÿ ๐Ÿงพ ๐Ÿ”ƒ ๐Ÿ•น.

โŽ ๐Ÿ“› ๐Ÿ’ฅ

๐Ÿ‘ฅ ๐Ÿญ ๐Ÿ” items ๐Ÿ”—, โ†ฉ๏ธ ๐Ÿญ ๐Ÿšฎ ๐Ÿ”ข router.

๐Ÿ‘‰ โ†ฉ๏ธ ๐Ÿ‘ฅ โœ”๏ธ โž•1๏ธโƒฃ ๐Ÿ”ข ๐Ÿ“› router ๐Ÿ” users.

๐Ÿšฅ ๐Ÿ‘ฅ โœ”๏ธ ๐Ÿ—„ 1๏ธโƒฃ โฎ๏ธ ๐ŸŽ, ๐Ÿ’–:

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

router โšช๏ธโžก๏ธ users ๐Ÿ”œ ๐Ÿ“ 1๏ธโƒฃ โšช๏ธโžก๏ธ items & ๐Ÿ‘ฅ ๐Ÿšซ๐Ÿ”œ ๐Ÿ’ช โš™๏ธ ๐Ÿ‘ซ ๐ŸŽ ๐Ÿ•ฐ.

, ๐Ÿ’ช โš™๏ธ ๐Ÿ‘ฏโ€โ™‚๏ธ ๐Ÿ‘ซ ๐ŸŽ ๐Ÿ“, ๐Ÿ‘ฅ ๐Ÿ—„ ๐Ÿ” ๐Ÿ”—:

from fastapi import Depends, FastAPI

from .dependencies import get_query_token, get_token_header
from .internal import admin
from .routers import items, users

app = FastAPI(dependencies=[Depends(get_query_token)])


app.include_router(users.router)
app.include_router(items.router)
app.include_router(
    admin.router,
    prefix="/admin",
    tags=["admin"],
    dependencies=[Depends(get_token_header)],
    responses={418: {"description": "I'm a teapot"}},
)


@app.get("/")
async def root():
    return {"message": "Hello Bigger Applications!"}

๐Ÿ”Œ APIRouterโ“‚ users & items

๐Ÿ”œ, โžก๏ธ ๐Ÿ”Œ routerโ“‚ โšช๏ธโžก๏ธ ๐Ÿ” users & items:

from fastapi import Depends, FastAPI

from .dependencies import get_query_token, get_token_header
from .internal import admin
from .routers import items, users

app = FastAPI(dependencies=[Depends(get_query_token)])


app.include_router(users.router)
app.include_router(items.router)
app.include_router(
    admin.router,
    prefix="/admin",
    tags=["admin"],
    dependencies=[Depends(get_token_header)],
    responses={418: {"description": "I'm a teapot"}},
)


@app.get("/")
async def root():
    return {"message": "Hello Bigger Applications!"}

Info

users.router ๐Ÿ”Œ APIRouter ๐Ÿ”˜ ๐Ÿ“ app/routers/users.py.

& items.router ๐Ÿ”Œ APIRouter ๐Ÿ”˜ ๐Ÿ“ app/routers/items.py.

โฎ๏ธ app.include_router() ๐Ÿ‘ฅ ๐Ÿ’ช ๐Ÿšฎ ๐Ÿ”  APIRouter ๐Ÿ‘‘ FastAPI ๐Ÿˆธ.

โšซ๏ธ ๐Ÿ”œ ๐Ÿ”Œ ๐ŸŒ ๐Ÿ›ฃ โšช๏ธโžก๏ธ ๐Ÿ‘ˆ ๐Ÿ“ป ๐Ÿ• โšซ๏ธ.

๐Ÿ“ก โ„น

โšซ๏ธ ๐Ÿ”œ ๐Ÿค™ ๐Ÿ”˜ โœ โžก ๐Ÿ› ๏ธ ๐Ÿ”  โžก ๐Ÿ› ๏ธ ๐Ÿ‘ˆ ๐Ÿ“ฃ APIRouter.

, โ›… ๐ŸŽ‘, โšซ๏ธ ๐Ÿ”œ ๐Ÿค™ ๐Ÿ‘ท ๐Ÿšฅ ๐ŸŒ ๐ŸŽ ๐Ÿ‘ ๐Ÿ“ฑ.

Check

๐Ÿ‘† ๐Ÿšซ โœ”๏ธ ๐Ÿ˜Ÿ ๐Ÿ”ƒ ๐ŸŽญ ๐Ÿ•โ” โœ… ๐Ÿ“ป.

๐Ÿ‘‰ ๐Ÿ”œ โœŠ โฒ & ๐Ÿ”œ ๐Ÿ•ด ๐Ÿ”จ ๐Ÿ•ด.

โšซ๏ธ ๐Ÿ† ๐Ÿšซ ๐Ÿ“‰ ๐ŸŽญ. ๐Ÿ‘ถ

๐Ÿ”Œ APIRouter โฎ๏ธ ๐Ÿ›ƒ prefix, tags, responses, & dependencies

๐Ÿ”œ, โžก๏ธ ๐ŸŒˆ ๐Ÿ‘† ๐Ÿข ๐Ÿค ๐Ÿ‘† app/internal/admin.py ๐Ÿ“.

โšซ๏ธ ๐Ÿ”Œ APIRouter โฎ๏ธ ๐Ÿ“ก โžก ๐Ÿ› ๏ธ ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿข ๐Ÿ’ฐ ๐Ÿ–– ๐Ÿ“š ๐Ÿ—.

๐Ÿ‘‰ ๐Ÿ–ผ โšซ๏ธ ๐Ÿ”œ ๐Ÿ’Ž ๐Ÿ™…. โœ‹๏ธ โžก๏ธ ๐Ÿ’ฌ ๐Ÿ‘ˆ โ†ฉ๏ธ โšซ๏ธ ๐Ÿ’ฐ โฎ๏ธ ๐ŸŽ ๐Ÿ— ๐Ÿข, ๐Ÿ‘ฅ ๐Ÿšซ๐Ÿ”œ ๐Ÿ”€ โšซ๏ธ & ๐Ÿšฎ prefix, dependencies, tags, โ™’๏ธ. ๐Ÿ”— APIRouter:

from fastapi import APIRouter

router = APIRouter()


@router.post("/")
async def update_admin():
    return {"message": "Admin getting schwifty"}

โœ‹๏ธ ๐Ÿ‘ฅ ๐Ÿ’š โš’ ๐Ÿ›ƒ prefix ๐Ÿ•โ” โœ… APIRouter ๐Ÿ‘ˆ ๐ŸŒ ๐Ÿšฎ โžก ๐Ÿ› ๏ธ โ–ถ๏ธ โฎ๏ธ /admin, ๐Ÿ‘ฅ ๐Ÿ’š ๐Ÿ” โšซ๏ธ โฎ๏ธ dependencies ๐Ÿ‘ฅ โช โœ”๏ธ ๐Ÿ‘‰ ๐Ÿ—, & ๐Ÿ‘ฅ ๐Ÿ’š ๐Ÿ”Œ tags & responses.

๐Ÿ‘ฅ ๐Ÿ’ช ๐Ÿ“ฃ ๐ŸŒ ๐Ÿ‘ˆ ๐Ÿต โœ”๏ธ ๐Ÿ”€ โฎ๏ธ APIRouter ๐Ÿšถโ€โ™€๏ธ ๐Ÿ‘ˆ ๐Ÿ”ข app.include_router():

from fastapi import Depends, FastAPI

from .dependencies import get_query_token, get_token_header
from .internal import admin
from .routers import items, users

app = FastAPI(dependencies=[Depends(get_query_token)])


app.include_router(users.router)
app.include_router(items.router)
app.include_router(
    admin.router,
    prefix="/admin",
    tags=["admin"],
    dependencies=[Depends(get_token_header)],
    responses={418: {"description": "I'm a teapot"}},
)


@app.get("/")
async def root():
    return {"message": "Hello Bigger Applications!"}

๐Ÿ‘ˆ ๐ŸŒŒ, โฎ๏ธ APIRouter ๐Ÿ”œ ๐Ÿšง โš—, ๐Ÿ‘ฅ ๐Ÿ’ช ๐Ÿ’ฐ ๐Ÿ‘ˆ ๐ŸŽ app/internal/admin.py ๐Ÿ“ โฎ๏ธ ๐ŸŽ ๐Ÿ— ๐Ÿข.

๐Ÿ ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ“ฑ, ๐Ÿ”  โžก ๐Ÿ› ๏ธ โšช๏ธโžก๏ธ admin ๐Ÿ•น ๐Ÿ”œ โœ”๏ธ:

  • ๐Ÿ”ก /admin.
  • ๐Ÿ”– admin.
  • ๐Ÿ”— get_token_header.
  • ๐Ÿ“จ 418. ๐Ÿ‘ถ

โœ‹๏ธ ๐Ÿ‘ˆ ๐Ÿ”œ ๐Ÿ•ด ๐Ÿ“‰ ๐Ÿ‘ˆ APIRouter ๐Ÿ‘† ๐Ÿ“ฑ, ๐Ÿšซ ๐Ÿ™† ๐ŸŽ ๐Ÿ“Ÿ ๐Ÿ‘ˆ โš™๏ธ โšซ๏ธ.

, ๐Ÿ–ผ, ๐ŸŽ ๐Ÿ— ๐Ÿ’ช โš™๏ธ ๐ŸŽ APIRouter โฎ๏ธ ๐ŸŽ ๐Ÿค ๐Ÿ‘ฉโ€๐Ÿ”ฌ.

๐Ÿ”Œ โžก ๐Ÿ› ๏ธ

๐Ÿ‘ฅ ๐Ÿ’ช ๐Ÿšฎ โžก ๐Ÿ› ๏ธ ๐Ÿ”— FastAPI ๐Ÿ“ฑ.

๐Ÿ“ฅ ๐Ÿ‘ฅ โšซ๏ธ... ๐ŸŽฆ ๐Ÿ‘ˆ ๐Ÿ‘ฅ ๐Ÿ’ช ๐Ÿคท:

from fastapi import Depends, FastAPI

from .dependencies import get_query_token, get_token_header
from .internal import admin
from .routers import items, users

app = FastAPI(dependencies=[Depends(get_query_token)])


app.include_router(users.router)
app.include_router(items.router)
app.include_router(
    admin.router,
    prefix="/admin",
    tags=["admin"],
    dependencies=[Depends(get_token_header)],
    responses={418: {"description": "I'm a teapot"}},
)


@app.get("/")
async def root():
    return {"message": "Hello Bigger Applications!"}

& โšซ๏ธ ๐Ÿ”œ ๐Ÿ‘ท โ˜‘, ๐Ÿ‘ฏโ€โ™‚๏ธ โฎ๏ธ ๐ŸŒ ๐ŸŽ โžก ๐Ÿ› ๏ธ ๐Ÿšฎ โฎ๏ธ app.include_router().

๐Ÿ“ถ ๐Ÿ“ก โ„น

๐Ÿ—’: ๐Ÿ‘‰ ๐Ÿ“ถ ๐Ÿ“ก โ„น ๐Ÿ‘ˆ ๐Ÿ‘† ๐ŸŽฒ ๐Ÿ’ช ๐Ÿšถ.


APIRouterโ“‚ ๐Ÿšซ "๐Ÿ—ป", ๐Ÿ‘ซ ๐Ÿšซ ๐Ÿ‘ฝ โšช๏ธโžก๏ธ ๐ŸŽ‚ ๐Ÿˆธ.

๐Ÿ‘‰ โ†ฉ๏ธ ๐Ÿ‘ฅ ๐Ÿ’š ๐Ÿ”Œ ๐Ÿ‘ซ โžก ๐Ÿ› ๏ธ ๐Ÿ—„ ๐Ÿ”— & ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ”ข.

๐Ÿ‘ฅ ๐Ÿšซ๐Ÿ”œ โŽ ๐Ÿ‘ซ & "๐Ÿ—ป" ๐Ÿ‘ซ โžก ๐ŸŽ‚, โžก ๐Ÿ› ๏ธ "๐Ÿ––" (๐Ÿค-โœ), ๐Ÿšซ ๐Ÿ”Œ ๐Ÿ”—.

โœ… ๐Ÿง ๐Ÿ› ๏ธ ๐Ÿฉบ

๐Ÿ”œ, ๐Ÿƒ 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 ๐Ÿ”Œ.