FastAPI系列 FastAPI应用常见配置项

一、概述

在路径操作配置中显然都是针对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"}

表现形式:

FastAPI系列 FastAPI应用常见配置项

1、title & description & version

from fastapi import FastAPI

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

表现形式:

FastAPI系列 FastAPI应用常见配置项

 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应用常见配置项

上一篇:c# 通过反射获得内部类


下一篇:2-Redis常用API