对sanic的openapi v3支持
sanic-openapi3的Python项目详细描述
Sanic OpenAPI v3
为sanic api提供openapi v3规范。 基于原始Sanic OpenAPI扩展名。
安装
pip install sanic-openapi3
用法
导入蓝图并使用简单的装饰器来记录路由:
fromsanic_openapi3importopenapi,openapi_blueprint@app.get("/user/<user_id:int>")@openapi.summary("Fetches a user by ID")@openapi.response(200,{"user":{"name":str,"id":int}})asyncdefget_user(request,user_id):...@app.post("/user")@openapi.summary("Creates a user")@openapi.body({"user":{"name":str}})asyncdefcreate_user(request):...app.blueprint(openapi_blueprint)
现在在url/openapi.json
处有一个规范。
你的路线将根据蓝图自动分类。
为输入/输出建模
classCar:make=strmodel=stryear=intclassGarage:spaces=intcars=[Car]@app.get("/garage")@openapi.summary("Gets the whole garage")@openapi.response(200,Garage)asyncdefget_garage(request):returnjson({"spaces":2,"cars":[{"make":"Nissan","model":"370Z"}]})
更具描述性
classCar:make=doc.String("Who made the car")model=doc.String("Type of car. This will vary by make")year=doc.Integer("4-digit year of the car",required=False)classGarage:spaces=doc.Integer("How many cars can fit in the garage")cars=doc.List(Car,description="All cars in the garage")
配置所有内容
app.config.OPENAPI_VERSION='1.0.0'app.config.OPENAPI_TITLE='Car API'app.config.OPENAPI_DESCRIPTION='Car API'app.config.OPENAPI_TERMS_OF_SERVICE='https://example.com/terms'app.config.OPENAPI_CONTACT_EMAIL='mail@example.com'app.config.OPENAPI_CONTACT_NAME='mail@example.com'