使用Sanic快速、简单、文档化的API开发框架
项目描述
Sanic-RESTPlus是Sanic的一个扩展,它添加了对快速构建REST API的支持。Sanic-RESTPlus通过最小化设置鼓励最佳实践。如果您熟悉Sanic,Sanic-RESTPlus应该很容易上手。它提供了一套连贯的装饰器和工具,用于描述您的API并正确地使用Swagger暴露其文档。
兼容性
Sanic-RestPlus需要Python 3.7及以上。
Sanic-RestPlus与Sanic v21.3+兼容
由于sanic-plugin-toolkit的限制,Sanic-RestPlus目前与Sanic v21.12或更高版本不兼容
向后兼容性通知
Sanic-RestPlus版本0.6.0已重新设计,现在需要Sanic v21.3或更高版本。
Sanic-RestPlus版本0.4.1(以及之前的版本)在Sanic 19.12+上**不工作**,请参阅此处的错误:[https://github.com/ashleysommer/sanicpluginsframework/issues/15](https://github.com/ashleysommer/sanicpluginsframework/issues/15)
如果您需要部署在Sanic v19.12或v20.12上,请使用Sanic-Restplus v0.5.x
如果您正在使用 Sanic v20.12LTS,请使用 Sanic-RestPlus v0.5.6。
安装
在不久的将来,您将能够使用 pip 安装 Sanic-Restplus
$ pip install sanic-restplus
或使用 easy_install
$ easy_install sanic-restplus
快速入门
使用 Sanic-Restplus,您只需导入 api 实例来路由和文档化您的端点。
from sanic import Sanic
from sanic_restplus import Api, Resource, fields
from sanic_restplus.restplus import restplus
from sanic_plugin_toolkit import SanicPluginRealm
app = Sanic(__name__)
realm = SanicPluginRealm(app)
rest_assoc = realm.register_plugin(restplus)
api = Api(version='1.0', title='TodoMVC API',
description='A simple TodoMVC API')
ns = api.namespace('todos', description='TODO operations')
todo = api.model('Todo', {
'id': fields.Integer(readOnly=True, description='The task unique identifier'),
'task': fields.String(required=True, description='The task details')
})
class TodoDAO(object):
def __init__(self):
self.counter = 0
self.todos = []
def get(self, id):
for todo in self.todos:
if todo['id'] == id:
return todo
api.abort(404, "Todo {} doesn't exist".format(id))
def create(self, data):
todo = data
todo['id'] = self.counter = self.counter + 1
self.todos.append(todo)
return todo
def update(self, id, data):
todo = self.get(id)
todo.update(data)
return todo
def delete(self, id):
todo = self.get(id)
self.todos.remove(todo)
DAO = TodoDAO()
DAO.create({'task': 'Build an API'})
DAO.create({'task': '?????'})
DAO.create({'task': 'profit!'})
@ns.route('/')
class TodoList(Resource):
'''Shows a list of all todos, and lets you POST to add new tasks'''
@ns.doc('list_todos')
@ns.marshal_list_with(todo)
async def get(self, request):
'''List all tasks'''
return DAO.todos
@ns.doc('create_todo')
@ns.expect(todo)
@ns.marshal_with(todo, code=201)
async def post(self, request):
'''Create a new task'''
return DAO.create(request.json), 201
@ns.route('/<id:int>')
@ns.response(404, 'Todo not found')
@ns.param('id', 'The task identifier')
class Todo(Resource):
'''Show a single todo item and lets you delete them'''
@ns.doc('get_todo')
@ns.marshal_with(todo)
async def get(self, request, id):
'''Fetch a given resource'''
return DAO.get(id)
@ns.doc('delete_todo')
@ns.response(204, 'Todo deleted')
async def delete(self, request, id):
'''Delete a task given its identifier'''
DAO.delete(id)
return '', 204
@ns.expect(todo)
@ns.marshal_with(todo)
async def put(self, request, id):
'''Update a task given its identifier'''
return DAO.update(id, request.json)
rest_assoc.api(api)
if __name__ == '__main__':
app.run(debug=True, auto_reload=False)
文档
文档托管在 Read the Docs 上。这是 Flask RestPlus 的文档,Sanic-Restplus 的文档尚未转换。
已删除 Flask-Restplus 更新日志。请参阅相关父存储库中的 flask-restplus 更新日志。