FastAPI查看及API文档

一:查看

先写一个简单的小例子

 

from enum import Enum

import uvicorn
from fastapi import FastAPI


class ModelName(str, Enum):
    tell_a = "1"
    tell_b = "2"
    tell_c = "3"


app = FastAPI()


@app.get("/models/{model_name}")
async def get_model(model_name: ModelName):
    if model_name == ModelName.tell_a:
        return {"model_name": model_name, "msg": "hello one"}
    elif model_name.value == "3":
        return {"model_name": model_name, "msg": "hello three"}
    return {"model_name": model_name, "msg": "hello two"}


if __name__ == '__main__':
    uvicorn.run("enum_test:app")

 

本例子中查看如访问 http://127.0.0.1:8000/models/2

二:交互式API文档   http://127.0.0.1:8000/docs

 

 

三:可选的API文档    http://127.0.0.1:8000/redoc

 

 

四:查看 openapi.json

 

posted @ 2022-03-18 09:38  手可摘星辰/*  阅读(485)  评论(0编辑  收藏  举报