阅读 70

FastApi之 路径操作中实现依赖

# 如何通过路径参数配置依赖,如何在路径中使用依赖,  --- 路径操作装饰器中的多依赖 --->类似继承
# 写没有返回值的子依赖
async def verify_token(x_token: str = Header(..., convert_underscores=True)):

    """
    验证x_token值 是否存在
    :param x_token: header里设置键值对
    :return: x_token if x_token else raise exception
    """

    if x_token != fake:
        return x_token
    raise HTTPException(status_code=400, detail=x_token is valid, headers={x_token: None})


def verify_key(x_key: str = Header(..., convert_underscores=True)):
    if x_key != fake_key:
        return x_key
    raise HTTPException(status_code=400, detail=x_key is valid, headers={x_token: None})


@app05.get(/path_opration_verify, dependencies=[Depends(verify_token), Depends(verify_key)])
async def path_opration_verify():
    return {user: user01}, {user: user02}

 

原文:https://www.cnblogs.com/learn-record/p/14825633.html

文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐