美文网首页
sanic-openApi 学习

sanic-openApi 学习

作者: woniuxia | 来源:发表于2021-06-24 16:46 被阅读0次

sanicOpenApi 学习

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
produces() 装饰器 用于记录默认响应(状态为200)。
Response
可以使用response() 装饰器, 来记录非状态为200的响应,。例如下面的例子:
  • 请注意,当您同时使用response() 和produces() 时,状态为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"})

如果对你有帮助,希望能点个赞,谢谢

相关文章

  • sanic-openApi 学习

    sanicOpenApi 学习 Sanic-OpenAPI 装饰器 Exclude: Summary: Descr...

  • 学习学习学习

    第三天了,连续三天,早上睁眼开始,看视频,做课件,连续作业,直到晚上十二点才睡觉。吃饭不规律,想起来就吃,感觉不饿...

  • 学习学习学习

    23岁的我,才真正明白,什么是学习,什么是努力,努力和不努力真的不同,就好比同样是一篇稿子,我用一周背下来,有的人...

  • 学习学习学习!

    妈妈总是让我学习,我只能用装当办法。 方法一: 方法二: 方法三: 方法四: ...

  • 学习学习学习

    001.今天看财富自由之路看了第二遍,而且看了一半,算是完成任务很开心。中间有想放弃的念头,坚持看完。眼睛痛,一直...

  • 学习学习学习

    马自达为什么坚持高压缩比自吸

  • 学习!学习!学习!

    学习的痛苦是暂时的 没有学到的痛苦是永恒的 因为学习而特别充实的一天 很踏实 ~~~~ 2015.11.28.阴天...

  • 学习!学习!学习!

    无数次想要去逃离,可这封闭的世界根本出不去。你没有什么可以抛弃、只能咬着牙带着面具微笑的活下去。 没有那个人、他也...

  • 学习学习学习!

    昨天和今天两个上午,都在学习新媒体运营,学习的过程中心里只有一个想法:这也太套路,太功利了吧。可真应了那句话...

  • 学习,学习,学习!

    近期学习重点有两个方面,一方面是把上一个阶段定下任务的几本书读完,并在读的过程中有输出和转化,让阅读和学习真正能有...

网友评论

      本文标题:sanic-openApi 学习

      本文链接:https://www.haomeiwen.com/subject/twqgyltx.html