sanicOpenApi 学习

sanic_open_api 学习

Sanic-OpenAPI 装饰器
Exclude:
当您不想在Swagger中记录某个路由时,可以使用exclude(True)装饰器 从Swagger中排除路由
from sanic import Sanic
from sanic.response import json

from sanic_openapi import doc, openapi2_blueprint

app = Sanic()
app.blueprint(openapi2_blueprint)


@app.get("/test")
async def test(request):
    return json({"Hello": "World"})


@app.get("/config")
@doc.exclude(True)
async def get_config(request):
    return json(request.app.config)
Summary:
您可以使用summary() 装饰器,向路由中添加一个简短的摘要。指出API路由的用途是很有帮助的。
from sanic import Sanic
from sanic.response import json

from sanic_openapi import doc, openapi2_blueprint

app = Sanic()
app.blueprint(openapi2_blueprint)


@app.get("/test")
@doc.summary("Test route")
async def test(request):
    return json({"Hello": "World"})
Description
使用description() 装饰器,不仅可以处理简短的摘要,还可以处理API路由的长描述。
from sanic import Sanic
from sanic.response import json

from sanic_openapi import doc, openapi2_blueprint

app = Sanic()
app.blueprint(openapi2_blueprint)


@app.get("/test")
@doc.description('This is a test route with detail description.')
async def test(request):
    return json({"Hello": "World"})

Tag
如果您想对API路由进行分组,可以使用tag() 装饰器 来完成您的需要。
默认情况下,Sanic下注册的所有路由都将被默认标记。所有在Blueprint下的路由都将被标记为Blueprint名称。

from sanic import Sanic
from sanic.response import json

from sanic_openapi import doc, openapi2_blueprint

app = Sanic()
app.blueprint(openapi2_blueprint)


@app.get("/test")
@doc.tag("test")
async def test(request):
    return json({"Hello": "World"})
Operation
Sanic OpenAPI 将使用route(function) name作为默认的operationId。您可以使用operation() 修饰符重写operationId。当路由在某些情况下具有重复名称时,operation() 修饰符将非常有用。
Consumes
consumes() 修饰符是 sanic-open-api中最常用的修饰符。它用于记录swagger中的参数用法。可以使用str、int、dict等内置类,也可以使用sanicopenapi提供的不同字段来记录参数。
Produces
products() 装饰器 用于记录默认响应(状态为200)。
Response
可以使用response() 装饰器, 来记录非状态为200的响应,。例如下面的例子:
  • 请注意,当您同时使用response() 和products() 时,状态为200的response() 将不起作用。
from sanic import Sanic
from sanic.response import json

from sanic_openapi import doc, openapi2_blueprint

app = Sanic()
app.blueprint(openapi2_blueprint)


@app.get("/test")
@doc.response(401, {"message": str}, description="Unauthorized")
async def test(request):
    return json({"Hello": "World"})
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值