Sanic OpenAPI V3e

对sanic的openapi v3支持。记录并描述所有参数,

包括Sanic路径参数。Python3.5+

安装pip install sanic-openapi3e

用法

导入蓝图并使用简单的装饰器来记录路由:importsanicimportsanic.responsefromsanic_openapi3eimportopenapi_blueprint,swagger_blueprint,docapp=sanic.Sanic(strict_slashes=True)app.blueprint(openapi_blueprint)app.blueprint(swagger_blueprint)@app.get("/user/")@doc.summary("Fetches a user by ID")@doc.response(200,"The user")asyncdefget_user(request,user_id):returnsanic.response.json(locals())app.go_fast()

现在在url/openapi/spec.json处有一个规范。

你的路线会根据蓝图自动分类

名字。

描述路径参数importsanicimportsanic.responsefromsanic_openapi3eimportopenapi_blueprint,swagger_blueprint,docapp=sanic.Sanic(strict_slashes=True)app.blueprint(openapi_blueprint)app.blueprint(swagger_blueprint)@app.get("/examples/test_id/")@doc.parameter(name="an_id",description="An ID",required=True,_in="path")deftest_id(request,an_id):returnsanic.response.json(locals())app.go_fast()

sanic-openapiv3将识别路径参数an_id

用@doc.parameter描述,并将细节合并在一起。

您可能希望指定一个参数仅限于一组选项,

例如星期几或它有最小值。这些都可以做到

对于path、query、header和cookie中的参数:importsanicimportsanic.requestimportsanic.responsefromsanic_openapi3eimportopenapi_blueprint,swagger_blueprint,docapp=sanic.Sanic(strict_slashes=True)app.blueprint(openapi_blueprint)app.blueprint(swagger_blueprint)int_min_4=doc.Schema(_type="integer",_format="int32",minimum=4,description="Minimum: 4")@app.get("/test/some_ids")@doc.parameter(name="ids",description="Some IDs",required=True,choices=[1,3,5,7,11,13],_in="query",schema=doc.Schema.Integers,)deftest_some_ids(request:sanic.request.Request):query=request.query_stringreturnsanic.response.json(locals())@app.get("/examples/test_id_min/")@doc.parameter(name="an_id",description="An ID",required=True,_in="path",schema=int_min_4)deftest_id_min(request,an_id:int):returnsanic.response.json(locals())app.go_fast()

描述您的标签

openapi使用“标记”(每个路由可以有多个标记)将

终点。很高兴能够将您的端点分组到给定的标记中

根据蓝图的名字,但有时你会想给他们更好的

姓名:@doc.tag("tag name")。最好还是描述一下

对于这些标签(在swagger ui中显示得很好),所以

@doc.tag("tag name", description="tag description")。

你不必多次添加描述,

sanic-openapiv3e将使其可用,因此

用@doc.tag(...)装饰每个端点,只有其中一个将

需要描述。如果您尝试为

同一个标记,sanic-openapiv3e将引发显示该标记的异常

名称和冲突的描述。

在应用程序中共享和重用常用参数

可能有一些常见的参数出现在

你的API。一周中的几天?最小值必须为的分页

大于零?openapi v3有“组件”的概念,它可以

分享。设置它们很简单:importsanic.requestimportsanic.responsefromsanicimportSanicfromsanic_openapi3eimportopenapi_blueprint,swagger_blueprint,docdays_of_week=doc.Schema(_type="string",description="Days of the week, short, English",enum=["Mon","Tue","Wed","Thu","Fri","Sat","Sun"],)app=Sanic(strict_slashes=True)app.blueprint(openapi_blueprint)app.blueprint(swagger_blueprint)schemas={"int.min4":doc.Schema(title="int.min4",_type="integer",_format="int32",minimum=4,description="Minimum: 4",),"days":days_of_week,}components=doc.Components(schemas=schemas)app.config.OPENAPI_COMPONENTS=components# ^^ the line above adds these to OAS v3's "components"# the next two, which would ordinarily live in your blueprints's module,# reuse these shared components.int_min_4_ref=doc.Reference("#/components/schemas/int.min4")dow_ref=doc.Reference("#/components/schemas/days")@app.get("/simple/01/from//to//in/")@doc.parameter(name="start",description="Start day",required=True,_in="path",schema=dow_ref)@doc.parameter(name="end",description="End day",required=True,_in="path",schema=dow_ref)@doc.parameter(name="hops",description="hops to use",required=True,_in="path",schema=int_min_4_ref,)defget_start_end_hops(request,start:str,end:str,hops:int):returnsanic.response.json(locals())app.go_fast()

不推荐路由路径或参数

参数可以标记为deprecated=True:importsanicimportsanic.requestimportsanic.responsefromsanic_openapi3eimportopenapi_blueprint,swagger_blueprint,docapp=sanic.Sanic(strict_slashes=True)app.blueprint(openapi_blueprint)app.blueprint(swagger_blueprint)@app.get("/examples/test_parameter__deprecated/")@doc.parameter(name="an_id",description="An ID",required=True,_in="path",deprecated=True)@doc.summary("A path deprecated parameter")@doc.description("The parameter should be marked as deprecated")defparam__deprecated(request,an_id:int):returnsanic.response.json(locals())app.go_fast()

整个路线都可以通过@doc.deprecated:importsanicimportsanic.requestimportsanic.responsefromsanic_openapi3eimportopenapi_blueprint,swagger_blueprint,docapp=sanic.Sanic(strict_slashes=True)app.blueprint(openapi_blueprint)app.blueprint(swagger_blueprint)@app.get("/examples/test_path__deprecated/")@doc.parameter(name="an_id",description="An ID",required=True,_in="path",)@doc.summary("A path with parameter examples")@doc.description("This is marked as being deprecated")@doc.deprecateddefpath__deprecated(request,an_id:int):returnsanic.response.json(locals())app.go_fast()

排除OpenAPI规范中出现的路由(和虚张声势)

需要软启动一个端点,还是保持你的招摇简单?添加

@doc.exclude而且它根本不在openapi规范中(除非您

second将在/openapi/spec.all.json创建规范,它将

所有路线,包括不包括在内。importsanicimportsanic.requestimportsanic.responsefromsanic_openapi3eimportopenapi_blueprint,swagger_blueprint,docapp=sanic.Sanic(strict_slashes=True)app.blueprint(openapi_blueprint)app.blueprint(swagger_blueprint)@app.get("/test/alpha_release")@doc.exclude@doc.parameter(name="ids",description="Some IDs",required=True,choices=[1,3,5,7,11,13],_in="query",schema=doc.Schema.Integers,)deftest_some_ids(request:sanic.request.Request):query=request.query_stringreturnsanic.response.json(locals())app.go_fast()

配置一些app.config.API_VERSION='1.0.0'app.config.API_TITLE='An API'app.config.API_DESCRIPTION='An API description'

要有一个contact,请至少设置其中一个(最好是全部)

app.config.API_CONTACT_NAME,

app.config.API_CONTACT_URL或

app.config.API_CONTACT_EMAIL。

有一个license,set app.config.API_LICENSE_NAME和

可选的app.config.API_LICENSE_URL(所有str,但swagger ui除外。

设置termsOfServiceapp.config.API_TERMS_OF_SERVICE_URL(一个str,但是这个招摇的ui

希望将此用作URL)。

设置components、security和externalDocs需要您首先在代码中的某个地方创建相关对象(靠近

在这里创建app),

设置适当的app.config.OPENAPI_COMPONENTS,

app.config.OPENAPI_SECURITY,

app.config.OPENAPI_EXTERNAL_DOCS。

控制规范生成hide_openapi_self = app.config.get("HIDE_OPENAPI_SELF", True)

show_excluded = app.config.get("SHOW_OPENAPI_EXCLUDED", False)

show_unused_tags = app.config.get("SHOW_OPENAPI_UNUSED_TAGS", False)

实际上,您通常不想记录

/openapi路由,但通过设置app.config.HIDE_OPENAPI_SELF = False

您可以让它们出现在生成的等级库中(因此可以大摇大摆地

也一样)。

您的@doc.exclude注释总是受到尊重的,但是如果您的

配置具有app.config.SHOW_OPENAPI_EXCLUDED = True然后是秒

在/openapi/spec.all.json创建规范。你一般不会想要

这些将在您的生产部署中,但您可能希望它用于dev

以及测试目的。

欢迎加入QQ群-->: 979659372

推荐PyPI第三方库

python sanic openapi_Python sanic-openapi3e包_程序模块 - PyPI - Python中文网相关推荐

  1. python renamer模块_Python smart-image-renamer包_程序模块 - PyPI - Python中文网

    使用包含在中的exif数据智能地批量重命名图像的脚本 安装 要安装智能图像重命名程序: 推荐的方法是通过pip.pip install smart-image-renamer 否则像其他python包 ...

  2. python三方库ping_Python pingping包_程序模块 - PyPI - Python中文网

    多语言ping(ping pingping) 简介 ping ping是一个能够理解ping输出的多种语言并将结果翻译成机器可理解格式的特殊库.即json ping ping是一个独立于供应商的库,您 ...

  3. python cv模块_Python cv包_程序模块 - PyPI - Python中文网

    Cv 检查python模块的版本. 查询pypi并在所有可用版本中查找.__version__. 如果版本已经存在,则会引发错误. 在ci中很有用,可以记住更改库版本. 有关python模块版本控制的 ...

  4. python repusts模块_Python tslearn包_程序模块 - PyPI - Python中文网

    tslearn是一个python包,它为分析时间序列提供机器学习工具. 这个包基于scikit-learn.numpy和scipy库. 依赖关系Cython numpy numba scipy sci ...

  5. python如何使用geotools_Python pygeotools包_程序模块 - PyPI - Python中文网

    #Pygeotools公司 用于地理空间数据处理和分析的库和命令行工具 ##特点 -将光栅重新采样/扭曲为公共分辨率/范围/投影 -使用[numpy屏蔽数组](https://docs.scipy.o ...

  6. python rest api client_Python py-jama-rest-client包_程序模块 - PyPI - Python中文网

    JAMA软件 JAMA软件是产品开发的最终记录和行动系统.公司的现代要求 测试管理解决方案有助于企业加快开发时间.降低风险.降低复杂性和验证. 法规遵从性.600多个以产品为中心的组织,包括nasa. ...

  7. python 化学模块_Python chemif包_程序模块 - PyPI - Python中文网

    实施 解决这个问题的方法是使用一个while循环的递归下降算法.这个循环的基本结构是:todo = [[item1, processing_data],[item2, processing_data] ...

  8. python模块大全doc_Python doc8包_程序模块 - PyPI - Python中文网

    命令行用法$ doc8 -h usage: doc8 [-h] [--config path] [--allow-long-titles] [--ignore code] [--no-sphinx] ...

  9. python eel_Python django-eel包_程序模块 - PyPI - Python中文网

    黄鳝 django eel是一个用于html gui应用程序的django应用程序,具有简单的python/js互操作.它是Eel的移植版本. 回购分行master:django鳗鱼的master分支 ...

  10. python cmap_Python cmap包_程序模块 - PyPI - Python中文网

    cmapy 在python中使用matplotlib colormaps和opencv. matplotlib提供了很多nice colormaps.cmapy将这些颜色映射公开为颜色列表,这些颜色列 ...

最新文章

  1. Ace-editor 输入内容时光标闪动,定位错乱的解决方案
  2. 手机上用的是的WAP协议 电脑上的是HTTP协议 这两个有什么不同
  3. Django10:Ajax介绍/发送数据/SweetAlert
  4. 二、规则组织数学模型的建立
  5. MySql 常见错误代码大全
  6. raspberry pi_庆祝Raspberry Pi的14天
  7. c需要实现安装卸载Linux模块,Linux内核模块编译与加载
  8. code vs 集成tfs_10大Python集成开发环境和代码编辑器
  9. 利用jquery实现数字千分位排版显示,使用0动态补全8位数
  10. java的colt矩阵操作_colt-1.2.0
  11. fescar(Seata)详解
  12. SpringBoot+zk+dubbo架构实践(一):本地部署zookeeper
  13. iPhone Web App及优缺点【书摘】
  14. 物联网通信-期末复习
  15. 员工符合签订无固定期限劳动合同的条件,这种情况单位有权不予续签吗?
  16. useradd 命令的常见用法
  17. vue+tsx+slot
  18. 在Word、WPS中插入AxMath公式导致行间距异常的解决办法
  19. java记事本统计功能_JAVA 运用流编程实现简单的记事本功能
  20. Animator is not playing an AnimatorController

热门文章

  1. 计算机类岗位知识,2020江苏事业单位计算机类岗位考情
  2. 软考(24)-路由器配置
  3. HTML5,图片边框,.9效果处理
  4. WordPress-Light Year简洁小清新风格网站主题
  5. C语言数据结构-第三章栈和队列-电大同步进度
  6. “特步男相亲被拒”之后:国货岂能被diss!
  7. BC260Y-CN连接阿里云飞燕物联网平台
  8. 2018年最新Android的保活方案及效果统计
  9. 中M22春C、Java入门练习-7.14
  10. 圆周运动、一般曲线运动、阿基米德螺旋线