烧瓶API工具集
flask-api-spec的Python项目详细描述
烧瓶API规范
烧瓶API工具集
pip install flask-api-spec
从一个简单的想法开始
让我们
- 为请求编写验证代码
- 为响应编写验证和序列化代码
- 编写API正文
- 集成授权然后保护api
- swaggerize它们都是自动的
- 带烧瓶
用法
安装
importflask_api_specapp=Flask(__name__)flask_api_spec.init_app(app)
示例
fromflask_api_specimportValue@app.route('/hello',apispec=dict(query=dict(# constraints for request.argsid=Value(int,error='There is not id or id is not int.'))))defhello_id():returndict(result='The id is %s'%request.args['id'])
关注以apispec=
开头的内容。我们使用带有apispec关键字参数的扩展app.route为api提供了一个健壮的特性。
fromflask_api_specimportValue@app.route('/hello/<string:name>',apispec=dict(param=dict(# constraints for parametersname=Value(str,lambdax:len(x)>3,error='name is no string or the length is not greater than 3'))))defhello_name(name):returndict(result='Hello %s!'%name)
apispec=
中的param关键字表示路径参数。我们可以明确地说明一些更详细的规则。在上述情况下,<string:name>
的大小应超过3。
待办事项
- []支持更多的mashmallow功能
- []支持更多模式功能
- []集成授权
- []生成完全夸张的yaml
- []支持烧瓶类视图