日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

sanicOpenApi 学习

發(fā)布時間:2023/12/8 编程问答 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sanicOpenApi 学习 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

sanic_open_api 學(xué)習(xí)

Sanic-OpenAPI 裝飾器

Exclude:
當(dāng)您不想在Swagger中記錄某個路由時,可以使用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() 裝飾器,向路由中添加一個簡短的摘要。指出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() 裝飾器,不僅可以處理簡短的摘要,還可以處理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.description('This is a test route with detail description.') async def test(request):return json({"Hello": "World"})
Tag
如果您想對API路由進(jìn)行分組,可以使用tag() 裝飾器 來完成您的需要。 默認(rèn)情況下,Sanic下注冊的所有路由都將被默認(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ù)名稱時,operation() 修飾符將非常有用。
Consumes
consumes() 修飾符是 sanic-open-api中最常用的修飾符。它用于記錄swagger中的參數(shù)用法。可以使用str、int、dict等內(nèi)置類,也可以使用sanicopenapi提供的不同字段來記錄參數(shù)。
Produces
products() 裝飾器 用于記錄默認(rèn)響應(yīng)(狀態(tài)為200)。
Response
可以使用response() 裝飾器, 來記錄非狀態(tài)為200的響應(yīng),。例如下面的例子:
  • 請注意,當(dāng)您同時使用response() 和products() 時,狀態(tài)為200的response() 將不起作用。
from sanic import Sanic from sanic.response import jsonfrom sanic_openapi import doc, openapi2_blueprintapp = Sanic() app.blueprint(openapi2_blueprint)@app.get("/test") @doc.response(401, {"message": str}, description="Unauthorized") async def test(request):return json({"Hello": "World"})

總結(jié)

以上是生活随笔為你收集整理的sanicOpenApi 学习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。