๐ฆ ๐ธ - ๐ ๐¶
๐ฅ ๐ ๐ ๐ธ โ๏ธ ๐ธ ๐ ๏ธ, โซ๏ธ ๐ ๐ผ ๐ ๐ ๐ช ๐ฎ ๐ ๐ ๐ ๐.
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๐ฌ/๐ ๏ธ โญ ๐ซ.- ๐ฅ ๐ ๐ฃ ๐ ๐ฏ โก ๐ ๏ธ, ๐ซ ๐ ๐ ๏ธ ๐โโ๏ธ.
- ๐ป ๐ ๐ ๏ธ ๐ฅ, โคด๏ธ
dependencies๐จโ๐จ, & โคด๏ธ ๐ ๐ข ๐. - ๐ ๐ช ๐ฎ
Security๐ โฎ๏ธscopes.
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 ๐.