Sanic框架之插件sanic-openapi

前言

学习使用sanic-openapi来构建API

安装

pip3.6 install sanic-openapi

使用

from sanic.response import json
from sanic.blueprints import Blueprint
from sanic_openapi import swagger_blueprint, openapi_blueprint,doc

params = {
    'id': {'type': 'integer', 'description': '数据ID', 'in': 'from', 'required': True},
    'fields': {'type': 'string', 'description': '返回的字段:默认是全部', 'in': 'query', 'required': False},
}
test_data = {
    'id':1,
    'fields':'Int'
}
api = Blueprint('todos', url_prefix='v1.0/todos')

@api.get('/', strict_slashes=True)
@doc.route('获取一条数据','根据用户id获取一条数据',{'id': {'type': 'integer', 'description': '数据ID', 'in': 'query', 'required': True}},params)
async def get(request):
    return json(test_data)


if __name__ == '__main__':
    app = Sanic(__name__)
    app.blueprints(api)
    app.blueprint(openapi_blueprint)
    app.blueprint(swagger_blueprint)
    app.run(debug=True)

查看

http://127.0.0.1:8000/swagger 就能看到对应的API文档

转载于:https://my.oschina.net/u/2333235/blog/1526493

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值