FastAPI 学习之路(四十四)路径操作的高级配置

简介: FastAPI 学习之路(四十四)路径操作的高级配置

   在实际的开发中呢,我们可能有些接口呢,不能对比进行开放,比如说我们内部的一些监控的接口,那么我们肯定想着如何在接口文档中进行屏蔽,那么我们看下应该如何实现呢。


@app.get("/legacy/", include_in_schema=False)
def get_legacy_data(response: Response):
    headers = {"X-Cat": "leizi", "Content-Language": "en-US"}
    data = """<?xml version="1.0"?>
    <shampoo>
    <Header>
        Apply shampoo here.
    </Header>
    <Body>
        You'll have to use soap here.
    </Body>r
    </shampoo>
    """
    response.set_cookie(key="message", value="hello")
    return Response(content=data, media_type="application/xml",
                    headers=headers)


 其实很简单,只需要配置


include_in_schema=False


即可。那么我们看下接口文档是否有这个接口呢


image.png


是没有在接口的文档中展示的,只能供我们自己内部直接调用。我们直接去访问。


image.png


       接口可以正常返回的。


       docstring 的高级描述

       

       路径操作函数 的 docstring 中用于 OpenAPI 的行数。


添加一个 \f (一个「换页」的转义字符)可以使 FastAPI 在那一位置截断用于 OpenAPI 的输出。


       我们看下在接口中的具体实现


# 新建用户
@usersRouter.post("/users/", tags=["users"], response_model=Users)
def create_user(user: UserCreate, db: Session = Depends(get_db)):
    """
        - **email**: 用户的邮箱
        - **password**: 用户密码
    """
    db_crest = get_user_emai(db, user.email)
    if not db_crest:
        return db_create_user(db=db, user=user)
    raise HTTPException(status_code=200, detail="账号不能重复")


 我们看下最后会返回什么。


image.png


我们可以看到,在接口文档中,我们去描述了我们的参数。文档内正常展示了,那么我们可以用这个,对接口的参数进行一些描述后,就可以展示在我们对外的接口文档中,方便去理解每个字段。

相关文章
|
SQL Oracle 关系型数据库
FastAPI数据库系列(一) MySQL数据库操作 一、简介
FastAPI中你可以使用任何关系型数据库,可以通过SQLAlchemy将其轻松的适应于任何的数据库,比如: PostgreSQL MySQL SQLite Oracle Microsoft SQL Server ...
|
12天前
|
Python
Fastapi进阶用法,路径参数,路由分发,查询参数等详解
Fastapi进阶用法,路径参数,路由分发,查询参数等详解
|
存储 IDE 中间件
FastAPI(44)- 操作关系型数据库(下)
FastAPI(44)- 操作关系型数据库(下)
385 0
|
SQL 关系型数据库 MySQL
FastAPI(44)- 操作关系型数据库(上)
FastAPI(44)- 操作关系型数据库(上)
276 0
FastAPI(36)- FastAPI 的元数据配置和文档 URL
FastAPI(36)- FastAPI 的元数据配置和文档 URL
359 0
FastAPI(36)- FastAPI 的元数据配置和文档 URL
FastAPI(32)- Dependencies in path operation 通过路径操作装饰器的 dependencies 参数声明依赖
FastAPI(32)- Dependencies in path operation 通过路径操作装饰器的 dependencies 参数声明依赖
141 0
FastAPI(32)- Dependencies in path operation 通过路径操作装饰器的 dependencies 参数声明依赖
FastAPI(26)- Path Operation Configuration 路径操作的配置
FastAPI(26)- Path Operation Configuration 路径操作的配置
107 0
FastAPI(26)- Path Operation Configuration 路径操作的配置
|
IDE API 开发工具
FastAPI(14)- 路径操作函数参数的类型是一个嵌套 Pydantic Model 的使用场景
FastAPI(14)- 路径操作函数参数的类型是一个嵌套 Pydantic Model 的使用场景
166 0
FastAPI(14)- 路径操作函数参数的类型是一个嵌套 Pydantic Model 的使用场景