首页 > Windows开发 > 详细

FastAPI系列 FastAPI应用常见配置项

时间:2021-06-06 22:36:31      阅读:40      评论:0      收藏:0      [点我收藏+]

一、概述

在路径操作配置中显然都是针对API的配置,比如API分类的tags参数等,那么针对FastAPI的应用的配置是如何的呢?常用的包含:

  • title & description & version
  • docs_url

...

当然还有很多参数,具体查看源码:

class FastAPI(Starlette):
    def __init__(
        self,
        *,
        debug: bool = False,
        routes: Optional[List[BaseRoute]] = None,
        title: str = "FastAPI",
        description: str = "",
        version: str = "0.1.0",
        openapi_url: Optional[str] = "/openapi.json",
        openapi_tags: Optional[List[Dict[str, Any]]] = None,
        servers: Optional[List[Dict[str, Union[str, Any]]]] = None,
        dependencies: Optional[Sequence[Depends]] = None,
        default_response_class: Type[Response] = Default(JSONResponse),
        docs_url: Optional[str] = "/docs",
        redoc_url: Optional[str] = "/redoc",
        swagger_ui_oauth2_redirect_url: Optional[str] = "/docs/oauth2-redirect",
        swagger_ui_init_oauth: Optional[Dict[str, Any]] = None,
        middleware: Optional[Sequence[Middleware]] = None,
        exception_handlers: Optional[
            Dict[
                Union[int, Type[Exception]],
                Callable[[Request, Any], Coroutine[Any, Any, Response]],
            ]
        ] = None,
        on_startup: Optional[Sequence[Callable[[], Any]]] = None,
        on_shutdown: Optional[Sequence[Callable[[], Any]]] = None,
        openapi_prefix: str = "",
        root_path: str = "",
        root_path_in_servers: bool = True,
        responses: Optional[Dict[Union[int, str], Dict[str, Any]]] = None,
        callbacks: Optional[List[BaseRoute]] = None,
        deprecated: Optional[bool] = None,
        include_in_schema: bool = True,
        **extra: Any,
    ) 
...

二、常见参数说明

在进行参数说明之前,先看一下未加任何参数的代码:

from fastapi import FastAPI

app = FastAPI()


@app.get("items")
async def read_items():
    return {"info": "read_items"}

表现形式:

技术分享图片

1、title & description & version

from fastapi import FastAPI

app = FastAPI(
    title="FastAPI Docs Test",
    description="FastAPI Application Params Test",
    version="1.1.1"
)
...

表现形式:

技术分享图片

 2、docs_url

from fastapi import FastAPI

app = FastAPI(
    title="FastAPI Docs Test",
    description="FastAPI Application Params Test",
    version="1.1.1",
    docs_url="/docs"
)

docs是交互文档的地址,可以自定义。还有很多其它的参数可使用,比如openapi_url、redoc_url等等。

 

FastAPI系列 FastAPI应用常见配置项

原文:https://www.cnblogs.com/shenjianping/p/14854215.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!