sanicOpenApi 学习
生活随笔
收集整理的這篇文章主要介紹了
sanicOpenApi 学习
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
sanic_open_api 學(xué)習(xí)
Sanic-OpenAPI 裝飾器
Exclude:
當(dāng)您不想在Swagger中記錄某個(gè)路由時(shí),可以使用exclude(True)裝飾器 從Swagger中排除路由 from sanic import Sanic from sanic.response import jsonfrom sanic_openapi import doc, openapi2_blueprintapp = 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() 裝飾器,向路由中添加一個(gè)簡(jiǎn)短的摘要。指出API路由的用途是很有幫助的。 from sanic import Sanic from sanic.response import jsonfrom sanic_openapi import doc, openapi2_blueprintapp = Sanic() app.blueprint(openapi2_blueprint)@app.get("/test") @doc.summary("Test route") async def test(request):return json({"Hello": "World"})Description
使用description() 裝飾器,不僅可以處理簡(jiǎn)短的摘要,還可以處理API路由的長(zhǎng)描述。 from sanic import Sanic from sanic.response import jsonfrom sanic_openapi import doc, openapi2_blueprintapp = 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
如果您想對(duì)API路由進(jìn)行分組,可以使用tag() 裝飾器 來完成您的需要。 默認(rèn)情況下,Sanic下注冊(cè)的所有路由都將被默認(rèn)標(biāo)記。所有在Blueprint下的路由都將被標(biāo)記為Blueprint名稱。 from sanic import Sanic from sanic.response import jsonfrom sanic_openapi import doc, openapi2_blueprintapp = 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作為默認(rèn)的operationId。您可以使用operation() 修飾符重寫operationId。當(dāng)路由在某些情況下具有重復(fù)名稱時(shí),operation() 修飾符將非常有用。Consumes
consumes() 修飾符是 sanic-open-api中最常用的修飾符。它用于記錄swagger中的參數(shù)用法??梢允褂胹tr、int、dict等內(nèi)置類,也可以使用sanicopenapi提供的不同字段來記錄參數(shù)。Produces
products() 裝飾器 用于記錄默認(rèn)響應(yīng)(狀態(tài)為200)。Response
可以使用response() 裝飾器, 來記錄非狀態(tài)為200的響應(yīng),。例如下面的例子:- 請(qǐng)注意,當(dāng)您同時(shí)使用response() 和products() 時(shí),狀態(tài)為200的response() 將不起作用。
總結(jié)
以上是生活随笔為你收集整理的sanicOpenApi 学习的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怪物Appr值与Data里Mon-x.w
- 下一篇: BIGO | imo实时语音传输优化揭秘