一个功能齐全的Flask框架,用于快速、简单且文档化的API开发
项目描述
Flask-RESTPlus是Flask的一个扩展,它增加了快速构建REST API的支持。Flask-RESTPlus通过最少的设置鼓励最佳实践。如果您熟悉Flask,Flask-RESTPlus应该很容易上手。它提供了一系列协调的装饰器和工具,用于描述您的API,并使用Swagger正确地暴露其文档。
兼容性
Flask-RestPlus需要Python 2.7或3.4+。
安装
您可以使用pip安装Flask-Restplus
$ pip install flask-restplus
或使用easy_install
$ easy_install flask-restplus
快速入门
使用Flask-Restplus,您只需导入api实例来路由和记录您的端点。
from flask import Flask
from flask_restplus import Api, Resource, fields
app = Flask(__name__)
api = Api(app, 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)
def get(self):
'''List all tasks'''
return DAO.todos
@ns.doc('create_todo')
@ns.expect(todo)
@ns.marshal_with(todo, code=201)
def post(self):
'''Create a new task'''
return DAO.create(api.payload), 201
@ns.route('/<int:id>')
@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)
def get(self, id):
'''Fetch a given resource'''
return DAO.get(id)
@ns.doc('delete_todo')
@ns.response(204, 'Todo deleted')
def delete(self, id):
'''Delete a task given its identifier'''
DAO.delete(id)
return '', 204
@ns.expect(todo)
@ns.marshal_with(todo)
def put(self, id):
'''Update a task given its identifier'''
return DAO.update(id, api.payload)
if __name__ == '__main__':
app.run(debug=True)
贡献者
Flask-RESTPlus由@noirbizarre提供。自2019年初以来,@SteadBytes、@a-luna、@j5awry、@ziirish志愿帮助@noirbizarre维护项目。当然,每个人都欢迎贡献力量,我们将乐意审查您的PR或回答您的问题。
文档
文档托管在Read the Docs
贡献
想要贡献力量!太棒了!查看CONTRIBUTING.rst!变更日志 =========
0.13.0 (2019-08-12)
0.12.1 (2018-09-28)
修复先前版本中缺失的变更日志
确保同时具有 $ref 和描述(或其他属性)的规范定义输出是有效的(使用 allOf)
添加初始规范模式和验证支持
确保空枚举不被序列化(以具有有效的规范)
0.12.0 (2018-09-27)
0.11.0 (2018-05-16)
将授权解析添加到命名空间中(#403)
添加供应商扩展支持(#97)
RequestParser 参数现在支持 split 操作
确保默认布尔值 False 与 RequestParser 一起工作(#199)
Schema 错误不会被 AttributeError: Api does not have __schema__ attribute 长时间隐藏(#194)
添加一个新的 URL 验证器,更灵活和精确。
现在将帮助信息添加到源错误消息中,而不是字符串插值(#147)
使用 pytest 代替 nosetests
升级到 Swagger-UI 3.4.0
修复注释中的错别字
在 marshal_with 和 marshal 中添加一个可选的键参数 skip_none
修复 Python 2.7 上掩码不正确的问题(#217)
修复 doc/scaling 中的错别字
为 allow_null 和 Nested 添加文档
添加 Namespace.payload
- 重大变更:默认情况下,所有内容都是无序的,因为排序对性能有严重影响
Api 和 Namespace 现在接受一个可选的 ordered 参数
marshal_with 和 marshal 现在接受一个可选的 ordered 参数
重大变更
停止支持 Python 2.6
- 改进头处理(#119)
@api.header 只在所有响应中记录响应头
@api.response 可以接受一个可选的 headers 参数来记录响应的特定头信息。
请求头由 @api.expect 装饰器处理。
0.10.1 (2017-03-04)
0.10.0 (2017-02-12)
0.9.2 (2016-04-22)
同一版本,但 PyPI 的一个错误迫使重新上传。
0.9.1 (2016-04-22)
0.9.0 (2016-02-22)
使 Namespace 在 Flask 中表现得像 Blueprint。
在 doc 装饰器中弃用 parser 和 body 参数。
弃用 Model.extend,改用 Model.clone。
添加了 param 装饰器。
在 Swagger 文档中尊重方法限制(问题链接:#93)。
改进了文档。
0.8.6 (2015-12-26)
处理 API 信息中的可调用对象。
处理错误处理程序中的文档。
删除/合并 flask_restful flask_restful.RequestParser。
将 RequestParser 处理到 expect 装饰器。
处理输入解析器的模式。
- 添加了一些输入。
email
ip
ipv4
ipv6
0.8.5 (2015-12-12)
处理 Polymorph 字段上的掩码。
处理继承模型的掩码。
将 flask_restful.abort 替换为 flask_restplus.errors.abort。
将 flask_restful.unpack 替换为 flask_restplus.utils.unpack。
- 重大变更:
将 ApiModel 重命名为 Model。
将 ApiNamespace 重命名为 Namespace。
0.8.4 (2015-12-07)
删除/合并 flask_restful.Resource,以解决递归问题。
允许任何 callable 作为字段 default、min、max 等。
添加了 Date 字段。
改进了对不一致掩码的错误处理。
处理模型级别的默认掩码。
支持掩码字段名中的冒号和破折号。
- 重大变更:
将 exceptions 模块重命名为 errors。
将 RestException 重命名为 RestError。
将 MarshallingException 重命名为 MarshallingError。
DateTime 字段始终输出日期和时间。
0.8.3 (2015-12-05)
删除/合并 flask-restful 字段。
删除/合并 flask-restplus 输入。
将Swagger-UI更新到版本2.1.3
如果DEBUG=False,则使用Swagger-UI的压缩版本
支持蓝图子域名(仅静态)
添加了对默认字段掩码的支持
0.8.2 (2015-12-01)
在模型上应用掩码时跳过未知字段
将*令牌添加到字段掩码(所有剩余字段)
确保生成的端点不会冲突
删除/合并flask-restful的Api.handler_error()
0.8.1 (2015-11-27)
- 重构Swagger UI处理
允许使用@api.documentation注册自定义视图
允许使用doc参数注册自定义URL
允许禁用文档doc=False
通过头信息添加字段掩码支持(请参阅:字段掩码文档)
在flask_restplus.inputs上公开flask_restful.inputs模块
- 添加了对一些缺失字段和属性的支持
host根字段(仅在设置SERVER_NAME配置时字段存在)
自定义tags根字段
exclusiveMinimum和exclusiveMaximum数字字段属性
multipleOf数字字段属性
minLength和maxLength字符串字段属性
pattern字符串字段属性
minItems和maxItems列表字段属性
uniqueItems列表字段属性
允许覆盖默认的错误处理器
修复
0.8.0
添加了负载验证(基于jsonschema的初始实现)
添加了@api.deprecated以标记资源或方法已过时
添加了@api.header装饰器快捷方式以记录头信息
添加了Postman导出
修复与flask-restful 0.3.4的兼容性
允许使用__schema_example__指定自定义字段示例
在Swagger UI中添加了对PATCH方法的支持
升级到Swagger UI 2.1.2
将枚举处理为可调用对象
允许使用SWAGGER_UI_DOC_EXPANSION参数配置docExpansion
0.7.2
与flask-restful 0.3.3兼容
修复RequestParser中action=append的处理
升级到SwaggerUI 2.1.8-M1
其他修复
0.7.1
修复@api.marshal_with_list()关键字参数的处理。
0.7.0
通过__schema__属性公开模型和字段模式
删除对模型作为类的支持
添加@api.errorhandler()以注册自定义错误处理器
添加@api.response()快捷装饰器
修复在定义中缺失的嵌套模型列表
0.6.0
支持Python 2.6
- 实验性多态支持(仅单一继承)
添加Polymorph字段
在String字段上添加对discriminator属性的支持
添加api.inherit()方法
添加ClassName字段
0.5.1
对具有模式的参数进行修复(不要设置type=string)
0.5.0
允许使用更短的语法设置操作id:@api.doc('my-operation')
添加了一个指定预期输入模型的快捷方式:@api.expect(my_fields)
添加了字段的title属性
添加@api.extend()以扩展模型
确保required和allow_null对于NestedField之间的连贯性
支持将原始类型列表和模型列表作为正文
升级到最新版本的Swagger UI
修复
0.4.2
将apidoc蓝图重命名为restplus_doc以避免冲突
0.4.1
添加了 SWAGGER_VALIDATOR_URL 配置参数
添加了 readonly 字段参数
升级到最新版本的Swagger UI
0.4.0
迁移到 Flask-Restful 0.3+
使用默认的 Blueprint/App 机制
允许使用 @api.doc(False) 或 @api.hide 隐藏一些资源或方法
允许使用 default_id 可调用参数全局自定义默认 operationId
0.3.0
- 切换到 Swagger 2.0(重大变更)
notes 文档现在是 description
nickname 文档现在是 id
新的响应声明格式
为文档 body 输入添加了缺失的 body 参数
Flask-Restful 0.3+ 兼容性切换前的最后一个版本
0.2.4
处理 fields.List 上的 description 和 required 属性
0.2.3
修复自定义字段注册问题
0.2.2
修复声明中的模型列表
0.2.1
允许使用 Api.model 自定义字段类型
处理 fields.List 中的自定义字段
0.2
升级到 SwaggerUI 0.2.22
支持额外的字段文档属性:required、description、enum、min、max 和 default
初始支持 RequestParser 中的模型
0.1.3
修复 Api.marshal() 快捷方式
0.1.2
添加了 Api.marshal_with() 和 Api.marshal_list_with() 装饰器
添加了 Api.marshal() 快捷方式
0.1.1
使用 zip_safe=False 正确打包。
0.1
初始版本
项目详情
flask-restplus-0.13.0.tar.gz 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | a66e442d0bca08f389fc3d07b4d808fc89961285d12fb8013f7cf15516fa9f5c |
|
MD5 | 63bf1f895d7a3398c590ff223c8d59b8 |
|
BLAKE2b-256 | acd6270f22ed9974e3ce0a3cc8c0be08211e63f82b46550dda85d0ae26b37371 |
flask_restplus-0.13.0-py2.py3-none-any.whl的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | a15d251923a8feb09a5d805c2f4d188555910a42c64d58f7dd281b8cac095f1b |
|
MD5 | 7e5be9b9267f8c578709f41cc21b790f |
|
BLAKE2b-256 | c2a6b17c848771f96ad039ad9e3ea275e842a16c39c4f3eb9f60ee330b20b6c2 |