22.FastAPI开发大型应用

您所在的位置:网站首页 nodejs适合做大项目吗 22.FastAPI开发大型应用

22.FastAPI开发大型应用

2024-05-27 14:01| 来源: 网络整理| 查看: 265

22.FastAPI开发大型应用

在前面的代码示例中,我们都是在一个文件创建路由;在实际开发中,一般会根据需求进行模块划分,代码项目中也会根据模块进行开发,不同的模块采用不同的文件来编写程序。在FastAPI中提供了APIRouter来组织路由, 相当于Flask中的 Blueprints。

22.1 使用APIRouter创建路由

1.导入 APIRouter

from fastapi import APIRouter

2.使用 APIRouter 定义路由操作

router = APIRouter()

使用 @router.get等对路由方法进行修饰即可,使用方式与FastAPI类相同。

22.2 FastAPI 主体程序

在FastAPI主体程序中,将具有 APIRouter 的子模块导入,然后通过FastAPI类的 include_router 将路由添加到应用程序。代码示例如下:

目录结构:

main.py test __init__.py test.py demo __init__.py demo.py

上面的目录结构中,test与demo是Python包。

test.py

# coding: utf-8 from fastapi import APIRouter ​ router = APIRouter() ​ @router.get(path='/hello1') async def hello1():    return {'hello1': 'Hello APIRouter1'}

demo.py

# coding: utf-8 from fastapi import APIRouter ​ router = APIRouter() ​ @router.get(path='/hello2') async def hello1():    return {'hello2': 'Hello APIRouter2'}

main.py

# coding: utf-8 from fastapi import FastAPI from test import test from demo import demo ​ app = FastAPI() app.include_router(test.router) app.include_router(demo.router) ​ @app.get(path='/') async def root():    return "Hello world"

启动应用,然后进行请求测试:

curl http://127.0.0.1:8000 "Hello world" curl http://127.0.0.1:8000/hello1 {"hello1":"Hello APIRouter1"} curl http://127.0.0.1:8000/hello2 {"hello2":"Hello APIRouter2"} 22.3 APIRouter 的参数

在实际开发中,一个文件中的路由是有相同的部分的,比如:“/test/hello1”、“/test/hello2”中,“/test”是相同的路由路径,在FastAPI中,可以通过APIRouter的参数来添加路由的前缀,而不需要在每个路由定义的时候分别去添加。APIRouter的参数:

prefix 路由前缀

tags 标签

dependencies 依赖项

responses 额外的响应

除此之外,我们有时候在不同的文件中还需要相同的路由前缀,所以,APIRouter 最好在包的初始化文件__init__.py中声明和定义。下面将上面的代码修改如下:

demo\__init__.py

# coding: utf-8 from fastapi import APIRouter ​ router = APIRouter(prefix='/demo') import demo.demo

demo.py

# coding: utf-8 from demo import router ​ @router.get(path='/hello2') async def hello1():    return {'hello2': 'Hello APIRouter2'}

test\__init__.py

# coding: utf-8 from fastapi import APIRouter ​ router = APIRouter(prefix='/test') import test.test

test.py

# coding: utf-8 from test import router ​ @router.get(path='/hello1') async def hello1():    return {'hello1': 'Hello APIRouter1'}

main.py

# coding: utf-8 from fastapi import FastAPI import test import demo ​ app = FastAPI() app.include_router(test.router) app.include_router(demo.router) ​ @app.get(path='/') async def root():    return "Hello world"

从以上代码中可以看出,对应定义的路由:

curl http://127.0.0.1:8000/demo/hello2 curl http://127.0.0.1:8000/test/hello1

运行测试:

curl http://127.0.0.1:8000/test/hello1 {"hello1":"Hello APIRouter1"} curl http://127.0.0.1:8000/demo/hello2 {"hello2":"Hello APIRouter2"}

需要注意:在包的初始化文件__init__.py文件中,一定要对使用了router的python文件进行import,并且import指令要放到router声明的后面,以便将使用了router的python文件编译进来。

22.4包含路由

在FastAPI中,也可以使用FastAPI类的include_router函数的相应参数进行路由前缀的设置,当然也可以设置其他参数,这样就不必在APIRouter声明中设置参数。如将上面的代码中修改如下:

__init__.py中的代码修改为:

from fastapi import APIRouter router = APIRouter()

main.py

# coding: utf-8 from fastapi import FastAPI import test import demo ​ app = FastAPI() app.include_router(test.router, prefix='/test') app.include_router(demo.router, prefix='/demo') ​ @app.get(path='/') async def root():    return "Hello world"

运行结果与上面的代码是相同的。

22.5多次使用不同的 prefix 包含同一个路由器

在FastAPI中,也可以在同一路由上使用不同的前缀来多次使用 .include_router();这样做在有些场景下可能有用,例如以不同的前缀公开同一个的 API,比方说 /api/v1 和 /api/latest;这是一个可能并不真正需要的高级用法,但万一你有需要了就能够用上。

如:在上面的main.py中增加一行:

app.include_router(demo.router, prefix='/demo2')

这样,我们在请求curl http://127.0.0.1:8000/demo2/hello2也会返回与curl http://127.0.0.1:8000/demo/hello2相同的结果。

22.6在另一个 APIRouter 中包含一个 APIRouter

与在 FastAPI 应用程序中包含 APIRouter 的方式相同,也可以在另一个 APIRouter 中包含 APIRouter。

如:上面的代码中,我们在test\__init__.py中增加一行:

router.include_router(demo.router)

这样,就可以请求:curl http://127.0.0.1:8000/test/hello2,返回结果:

{"hello2":"Hello APIRouter2"}

本篇主要讲述了FastAPI的APIRouter的使用方法,主要以其prefix参数以及include_router函数的prefix参数的使用来说明如何组织路由,通过组织路由来完成大型项目的开发,以便我们可以在多个文件中组织和管理软件的代码。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3