首页 > 数据库 > 数据库开发技术 > fastapi中的依赖注入场景记录篇~水文一篇
今日水文一篇哈~~~~哈:纯属一些知识点记录
依赖注入的实现,其实是fastapi作者的集大成的闪亮点之一,之前我的对于依赖注入的使用场景,主要是局限于对表单的校验上,不管的get和post的我为了和之前使用Flask的wtfrom保持使用一致性,所以统一都是使用的依赖注入的方式的。
那现在其实细想,如果你的业务有需求的话,使用依赖注入可以实现的东西比较多:
def register_global_include_router(self):
'''
导入路由模块
:param app:
:return:
'''
pass
from fastapi import APIRouter
from pydantic import BaseModel, Field
from fastapi import Depends, Query
bp = APIRouter(tags=['XXX系统V1'], prefix='/api/v1')
class XgetoForm(BaseModel):
# 定义的是查询参数的值
# 模糊查询会传递的此参数
dno: str = Query(..., min_length=1, description="ID")
@bp.get("/get", summary="提交订单前的检测当前预约时段号源-【小钟】")
def doctor_scheduling_info_tiempm_check(forms: XgetoForm = Depends()):
# is_reserve -属性 1:表示可以点击预约 2:有排班记录,但是已预约满
return forms.dno
@bp.post("/post", summary="提交订单前的检测当前预约时段号源-【小钟】")
def doctor_scheduling_info_tiempm_check(forms: XgetoForm = Depends()):
# is_reserve -属性 1:表示可以点击预约 2:有排班记录,但是已预约满
return forms.dno
self.startge.include_router(bp)
在上面的示例中,按以前使用wtform的习惯我的,我也是直接的使用一个类的方式来处理接受相关的参数,不管是body的还是Query的,统一再一个类里面去处理。
所以有了上面示例,但是值得注意的一点是提交的方式不同的,实现注入依赖的方式也有所不同:
我们的get方式提交的方式的话,参数都是Query的方式居多,所以我们的再执行依赖注入的时候,其实有两种表达方式来实现的对参数的校验处理:
class XgetoForm(BaseModel):
# 定义的是查询参数的值
# 模糊查询会传递的此参数
dno: str = Query(..., min_length=1, description="ID")
@bp.get("/get", summary="提交订单前的检测当前预约时段号源-【小钟】")
def doctor_scheduling_info_tiempm_check(forms: XgetoForm = Depends()):
# is_reserve -属性 1:表示可以点击预约 2:有排班记录,但是已预约满
return forms.dno
@bp.get("/get2", summary="提交订单前的检测当前预约时段号源-【小钟】")
def doctor_scheduling_info_tiempm_check(forms: XgetoForm = Depends(XgetoForm)):
# is_reserve -属性 1:表示可以点击预约 2:有排班记录,但是已预约满
return forms.dno
对应的接口请求:


如上示例的中post的话,那如果我只是进行:
那它执行的还是使用Query的规则来校验参数:
但是如果我改为:
那么则:
所以这个地方,主要看你个人咯!POST的话,参数一般都是body,当然不排除同事存在的!!!这个同时的存在的话,就多组合吧!
其实我们的知道对于API的分组直接的使用
bp = APIRouter(tags=['XXX系统V1'], prefix='/api/v1')
其实分类到里面内部的,我们的会发现有一个:dependencies
其实它呐,就是我们的实现类似go语言的gin针对某个组做相关的中间件,或校验机制的实现。
比如,某个分组API下,我们的需要对它进行的相关的校验。其他其他分组下的,我们的不需要校验。那我们直接的在这里进行依赖注入的实现就可以了!非常简单的!
from fastapi import APIRouter
from pydantic import BaseModel, Field
from fastapi import Depends, Query
bp = APIRouter(tags=['XXX系统V1'], prefix='/api/v1')
class GroupAPIRouterDepends(BaseModel):
# 定义的是查询参数的值
# 模糊查询会传递的此参数
xiao: str
zhong: str
tong:str =None
xue: str = None
# 对当前的分组加入对应的前置校验
bp.dependencies = [Depends(GroupAPIRouterDepends)]
class XgetoForm(BaseModel):
# 定义的是查询参数的值
# 模糊查询会传递的此参数
dno: str = Query(..., min_length=1, description="ID")
@bp.get("/get", summary="提交订单前的检测当前预约时段号源-【小钟】")
def doctor_scheduling_info_tiempm_check(forms: XgetoForm = Depends()):
# is_reserve -属性 1:表示可以点击预约 2:有排班记录,但是已预约满
return forms.dno
@bp.get("/get2", summary="提交订单前的检测当前预约时段号源-【小钟】")
def doctor_scheduling_info_tiempm_check(forms: XgetoForm = Depends(XgetoForm)):
# is_reserve -属性 1:表示可以点击预约 2:有排班记录,但是已预约满
return forms.dno
@bp.post("/post", summary="提交订单前的检测当前预约时段号源-【小钟】")
def doctor_scheduling_info_tiempm_check(forms: XgetoForm= Depends(XgetoForm)):
# is_reserve -属性 1:表示可以点击预约 2:有排班记录,但是已预约满
return forms.dno
self.startge.include_router(bp)
关键的部分就再:
其实也是一样的:
from fastapi import APIRouter
from pydantic import BaseModel, Field
from fastapi import Depends, Query
bp = APIRouter(tags=['XXX系统V1'], prefix='/api/v1')
class GroupAPIRouterDepends(BaseModel):
# 定义的是查询参数的值
# 模糊查询会传递的此参数
xiao: str
zhong: str
tong:str =None
xue: str = None
# 对当前的分组加入对应的前置校验
bp.dependencies = [Depends(GroupAPIRouterDepends)]
class XgetoForm(BaseModel):
# 定义的是查询参数的值
# 模糊查询会传递的此参数
dno: str = Query(..., min_length=1, description="ID")
class LocalAPIRouterDepends(BaseModel):
# 定义的是查询参数的值
# 模糊查询会传递的此参数
locs: str
@bp.get("/get", summary="提交订单前的检测当前预约时段号源-【小钟】",dependencies=[Depends(LocalAPIRouterDepends)])
def doctor_scheduling_info_tiempm_check(forms: XgetoForm = Depends()):
# is_reserve -属性 1:表示可以点击预约 2:有排班记录,但是已预约满
return forms.dno
访问结果:
END
简书:https://www.jianshu.com/u/d6960089b087
掘金:https://juejin.cn/user/2963939079225608
公众号:微信搜【小儿来一壶枸杞酒泡茶】
小钟同学 | 文 【欢迎一起学习交流】| QQ:308711822
1:本文相关描述主要是个人的认知和见解,如有不当之处,还望各位大佬指正。 2:关于文章内容,部分内容参考自互联网整理,如有链接会声明标注;如没有及时标注备注的链接的,如有侵权请联系,我会立即删除处理哟。
原文作者:程序员小钟同学;如有侵权,请联系管理员删除。