从你的烧瓶项目中提取夸张的规格

flasgger-TheodoFork的Python项目详细描述


#flasgger
为从docstring或引用的yaml文件中提取规范的所有flask视图创建swagger 2.0api文档。

swagger ui是嵌入的,默认情况下文档位于**/api docs/index.html中**

[![代码运行状况](https://landscape.io/github/rochacbruno/flasgger/master/landscape.svg?style=flat)(https://landscape.io/github/rochacbruno/flasgger/master)

[![沃克尔状态](https://app.wercker.com/status/d86586341ba8b313162b36f84b192a9c/m"沃克尔状态")(https://app.wercker.com/project/bykey/d86586341ba8b313162b36f84b192a9c)

<;a target="blank"ref="https://www.paypal.com/cgi-bin/webscr?CMD=\u捐款&;业务=Rochacbruno%40gmail%2ecom&;LC=br&;item\u name=flasgger&;no\u note=0&;currency\u code=USD&;BN=pp%2ddonationsbf%3abtn\u捐赠sm%2egf%3anonhostedguest">;IMG ALT ="与PayPal"SRC ="HTTP://www. PayPalObjuts.COM/EnUsU/I/BTN/BTNY-DONATEATSIM.GIF"/GT;

flasger提供一个扩展(swagger),用于检查flask应用程序中是否有包含带有swagger 2.0[操作](https://github.com/swagger api/swagger spec/blob/master/versions/2.0.md操作对象)对象的yaml docstring的终结点。







**由openshift驱动的openshift驱动的openshift



《入门入门


创建一个虚拟化虚拟化虚拟化虚拟化虚拟化虚拟化虚拟化
``` ` ` `
` ` ` `
` ` ` ` ` ` `
` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` `>PIP安装从烧瓶进口烧瓶中创建一个名为simple_test.py的文件,jsonify,从flasgger进口烧瓶中请求
flasgger进口烧瓶



app=烧瓶(\\\\\\\\\\\\



>
@app.route(/recs,methods=['get']]]>
>def recs()

>
"
a简单测试api
此端点不执行任何操作
只返回"test"
--
标记:
-testapi
参数:
-name:size
in:query
type:string
描述:元素大小
响应:
200:
描述:单个用户项
架构:
id:return_test
属性:
结果:
类型:字符串
描述:测试
默认值:"test"
"
size=int(request.args.get('size',"
return jsonify({"result":"test"*size})




app.run(debug=true)
````
` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` `
` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` `
```
pip安装flasgger
``

#运行demo app

````
python-m-flasgger.exampl_-app
`````


access:http://localhost:5000查看demo app运行


`````
````您可以在gunicorn




gunicorn flasgger.exampl_-app:app-b 0.0.0.0.0.0.0:5000


`````````
````````

>
a简单的例子

`` python
来自烧瓶导入烧瓶,jsonify
来自flasger导入招摇过市

招摇过市(app)

@app.route('/api/<;string:username>;')
def user_api(用户名):
"
user api
此资源返回用户信息
--
标记:
-用户
参数:
-名称:用户名
输入:字符串
必需:true
响应:
200:
描述:单个用户项
架构:
id:user_response
属性:
用户名:
类型:字符串
描述:用户名
默认值:部分用户名

"
"返回jsonify({'username':username})



app.run()

````

>;注意:在路径中捕获参数时,始终使用显式类型,错误:```/api/<;username>;``good:``/api/<;string:username>;``

可以使用外部文件
使用yaml或yml扩展名并遵循相同的模式。


符合
,但3个连字符以下的所有内容都应符合。
--
标记:
-用户
参数:
-输入:路径
名称:用户名
类型:字符串
必需:真
响应:
200:
描述:单个用户项
架构:
id:记录用户名
属性:
用户名:
类型:字符串
描述:用户的名称
默认值:"steve harris"

````

,然后将此文件用作flasger的视图

``python
的规范。utils import从

@app.route('/api/<;string:username>;')
@swag_from('path/to/external_file.yml')
def fromfile_decorated(用户名):
返回jsonify({'username':username})
`````


>或者如果不想使用装饰符,只需使用快捷方式

``python
@app.route('/api/<;string:username>;')
def from file_decorated(用户名):
"
file:path/to/external_file.yml
""
返回jsonify({'username':username})

````

>;注意:以上示例仅适用于单个定义

字符串:user name>;',endpoint='with_用户名',methods=['put','get'])
@app.route('/api/',endpoint='without_user_name')
@swag_from('path/to/external_file.yml',endpoint='with_user_name')
@swag_from('path/to/external_file_no_user_get.yml',endpoint without_user_name',方法=['get'])
@swag\u from('path/to/external_file_no_user_put.yml',endpoint='without_user_name',methods=['put'])
def fromfile_decorated(username=none):
如果不是username:
返回"no user!"
返回jsonify({'username':username})
```

validationerror

@swag_from('defs.yml')
def post():
data=request.json
尝试:
validate(data,'schema_id','defs.yml',,_文件
除validationerror之外:
返回abort(500)
````


所有验证选项都可以在http://json schema.org/latest/json schema validation.html中找到定义
用``<;br>;```替换每个```\n````但可以更改此行为
设置另一种消毒液。

````
来自flasger import swagger,no_sanitizer

app=flask()
swagger(app,sanitizer=no_sanitizer)
````

消毒剂

```
swagger(app,sanitizer=lambda text:do_anything_with(text))
````

在methodview类(ala[flask restful](https://github.com/flask restful/flask restful))和常规flask视图函数的方法中,

遵循yaml约定,flasger搜索`--`,前面的所有内容都作为端点的"summary"(第一行)和"description"(第二行)提供而之后的所有内容都被解析为一个swagger[操作](https://github.com/swagger api/swagger spec/blob/master/versions/2.0.md操作对象)对象。

][参数](https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md schemaobject)中的对象(https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md parameterobject)和[响应](https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md responseObject)对象,flasgger偏离了标准。我们需要内联架构的"id"字段,然后使用该字段将[架构](https://github.com/swagger api/swagger spec/blob/master/versions/2.0.md\schema object)对象正确放置在[定义](https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md definitions object)对象。

[架构](https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md schemaobject)对象也可以在其他[架构]的属性中定义](https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md schemaobject)对象。上面显示了一个带有用户地址属性的示例。



**flasgger/example_app.py**

`` python
来自烧瓶进口烧瓶,jsonify,请求flask.views导入方法view
from flasgger import swagger


app=flask(u name_uu)



url)
rule\u filter是一个可调用的函数,它接收"rule"对象并返回一个布尔值,以便仅在所需视图中进行筛选

app.config['swagger']={
"swagger"version:"2.0",
以下是默认的
"headers":[
('access-control-allow-origin','*'),
('access-control-allow-headers','authorization,content type'),
('access-control-expose-headers','authorization'),
('access-control-allow-methods','get,post,put,delete,options"),
('access-control-allow-credentials',"true"),
('access-control-max-age',60*60*24*20),
],
;另一个可选设置
"url前缀":"swaggerdocs",
"子域":"docs.mysite,com",
如果未设置规范/注册规范,则规范也是可选的,公开所有视图
"规范":[
{
"版本":"0.0.1",
"标题":"API v1",
"终结点":"v1规范",
"路由":"/v1/spec",

"规则过滤器"是可选的
可以调用它来筛选要提取的视图

"rule_filter":lambda rule:rule.endpoint.startsWith(
应仅为_v1)
}
这里的config-swagger(app,config={})



class-userapi(methodview):

"
获取用户列表
第一行是摘要
所有后续行,直到将连字符添加到描述中为止标记:
-用户
参数:
-名称:团队id
输入:路径
描述:团队id(键入任意数字)
必需:true
类型:整数
响应:
200:
描述:返回用户列表
架构:
类型:数组
项:
$ref:'35;/definitions/user'
"
数据={
"users":[
{"name":"steven wilson","team":团队id},
{"name":"mikael akerfeldt","team":团队id},
{"name":"daniel gildenlow","team":团队id}
]
}
返回jsonify(data)


def post(self,团队id):
"
创建新用户
第一行是摘要
所有后续行,直到连字符添加到描述中为止标记:
-用户
参数:
-名称:团队id
输入:路径
描述:团队id(输入任意数字)
必需:true
输入:整数
-输入:body
名称:body
架构:
id:user
必需:
-team
-name
properties:
team:
type:integer
description:team for user
name:
类型:字符串
描述:用户的名称
响应:
201:
描述:用户创建的架构:
类型:数组
项:
$ref:'35;/定义/user'
"
返回jsonify({"newuser":request.json,"team_id":team_id})



view=userapi.as_view('users')
app.add_url_ru rule(
/v1/users/<;int:team_id>;',
view_func=view,
methods=["get","post"],
endpoint='应该是一个只有用户才能使用的网络,如果你想











你仍然可以使用@app.route@app.route,如果你想

app.run(debug=true)

```


(debug=true)


<
``>
``````

然后访问[http://localhost:5000/apiddocs/index.html(http://localhost:5000/apiddocs/index.html/apiddocs/index是的API文档的实际应用

如果您只想公开规范json,请考虑它。



欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java从提供的列表中获取非重复值   java为什么Thymeleaf从HashMap返回字符串?   java将矩阵推到窗口的中心   java如何包含webapp_配置。txt到播放的dist-zip?   java实现产品密钥   WebSphereJava。lang.UnsupportedClassVersionError:JVMCFRE003错误的主要版本;class=org/slf4j/impl/StaticLoggerBinder,偏移量=6   使用java创建动态树结构   java为什么JVM在一次繁忙的旋转暂停后,会对同一代码块显示更多的延迟?   java无法使用distrolessdebug访问jarfile docker   java JMeter如何将多行响应数据传递给ForEach控制器请求   java空指针异常碎片活动   java改变JFileChooser的外观   性能Java位集:高效地查找所有真实位?   java可扩展的recyclerview实现