假设结构如下
. ├── app │ ├── __init__.py │ ├── main.py │ ├── dependencies.py │ └── routers │ │ ├── __init__.py │ │ ├── items.py │ │ └── users.py │ └── internal │ ├── __init__.py │ └── admin.py
有点像 Flask 里面的蓝图,为某个模块创建路径操作
#!usr/bin/env python # -*- coding:utf-8 _*- """ # author: 小菠萝测试笔记 # blog: https://www.cnblogs.com/poloyy/ # time: 2021/9/28 7:26 下午 # file: users.py """ from fastapi import APIRouter, Depends, HTTPException from dependencies import get_token_header # 属于该模块的路由 user_router = APIRouter( # 这里配置的 tags、dependencies、responses 对这个模块的内的所有路径操作都生效 # 路径前缀,该模块下所有路径操作的前缀 prefix="/users", # 标签 tags=["users"], # 依赖项 dependencies=[Depends(get_token_header)], # 响应 responses={404: {"description": "users Not found"}} ) @user_router.get('/account/login') async def login(): return {} @user_router.get('/account/logout') async def logout(): return {} # 单独给某个路径操作声明 tags、responses @user_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"}
#!usr/bin/env python # -*- coding:utf-8 _*- """ # author: 小菠萝测试笔记 # blog: https://www.cnblogs.com/poloyy/ # time: 2021/9/28 7:26 下午 # file: items.py """ from fastapi import APIRouter, Depends from dependencies import get_token_header item_router = APIRouter( # 这里配置的 tags、dependencies、responses 对这个模块的内的所有路径操作都生效 # 路径前缀,该模块下所有路径操作的前缀 prefix="/items", # 标签 tags=["items"], # 依赖项 dependencies=[Depends(get_token_header)], # 响应 responses={404: {"description": "items Not found"}} ) @item_router.get('/item') async def index(): return {} @item_router.get('/item/list') async def list(): return {}
from .items import item_router from .users import user_router
#!usr/bin/env python # -*- coding:utf-8 _*- """ # author: 小菠萝测试笔记 # blog: https://www.cnblogs.com/poloyy/ # time: 2021/9/28 7:26 下午 # file: admin.py """ from fastapi import APIRouter admin_router = APIRouter() @admin_router.post("/") async def update_admin(): return {"message": "Admin getting schwifty"}
from .admin import admin_router
#!usr/bin/env python # -*- coding:utf-8 _*- """ # author: 小菠萝测试笔记 # blog: https://www.cnblogs.com/poloyy/ # time: 2021/9/28 7:25 下午 # file: main.py """ import uvicorn from fastapi import FastAPI, Depends # 导入子路由 from dependencies import get_query_token, get_token_header from routers import user_router, item_router from common import admin_router # 主路由 app = FastAPI( # 声明全局依赖项 # 如果每个 APIRouter 都会用到这个依赖项,那么应该声明为全局依赖项 dependencies=[Depends(get_query_token)] ) # 添加子路由 app.include_router(user_router) app.include_router(item_router) # 也可以在这里给子路由声明 prefix、tags、dependencies、responses,而无需修改原始的 APIRouter app.include_router( 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!"} if __name__ == "__main__": uvicorn.run(app="main:app", host="127.0.0.1", port=8080, debug=True, reload=True)
user_router = APIRouter( prefix="/users", ) user_router.include_router( router=item_router prefix="/items" )