用于apispec的插件,通过使用修饰函数堆栈帮助重用文档。
apispec-decorated-crawler的Python项目详细描述
APISPEC装饰爬虫
用于apispec的插件,通过使用修饰函数堆栈帮助重用文档。
扩展示例
使用此设置:
fromapispecimportAPISpecfromflaskimportFlaskfrommarshmallowimportSchema,fieldsfrombrunoais.apispec.ext.decorated_crawlerimportdocd_wrapsapp=Flask(__name__)# Create an APISpecspec=APISpec(title='A safe Random swagger Petstore',version='1.0.0',plugins=['apispec.ext.flask','apispec.ext.marshmallow','brunoais.apispec.ext.decorated_crawler',],)defdecorates(func):@docd_wraps(func)defcalling(*args,**kwargs):""" --- get: consumes: - application/xml+dec1 security: - AlmostBasicAuth: [] BasicAuth: [] - ApiKeyAuth: [] tags: - down1 - up1 responses: 400: description: he may fail {f[x-400-suffix]} schema: PetSchema _: responses: 400: description: Global fail schema: PetSchema """returnfunc(*args,**kwargs)returncallingdefdecorates2(func):@docd_wraps(func)defcalling(*args,**kwargs):""" --- get: tags: - get2 - up1 security: - OAuth2: [scope1, scope2] responses: 402: description: mefail schema: PetSchema """returnfunc(*args,**kwargs)returncalling
传递视图函数:
@app.route('/random/<kind>')@decorates@decorates2defrandom_pet(kind):""" A cute furry animal endpoint. --- post: description: POST a pet get: description: Get a pet security: - ApiKeyAuth: [] consumes: - application/json - application/xml produces: - application/json - application/xml parameters: - in: path name: kind required: true type: string description: Path Parameter description in Markdown. - in: query name: offset type: string description: Query Parameter description in Markdown. responses: 200: description: A pet to be returned schema: PetSchema 400: x-400-suffix: yeah yeah.... """pet={"category":[{"id":1,"name":"Named"}],"name":"woof"}returnjsonify(PetSchema().dump(pet).data)# Optional marshmallow supportclassCategorySchema(Schema):id=fields.Int()name=fields.Str(required=True)classPetSchema(Schema):category=fields.Nested(CategorySchema,many=True)name=fields.Str()spec.definition('Category',schema=CategorySchema)spec.definition('Pet',schema=PetSchema)withapp.test_request_context():spec.add_path(view=random_pet)
结果变为
print(spec.to_yaml())
definitions:# ...info:{title:A safe Random swagger Petstore, version:1.0.0}paths:/random/{kind}:post:description:POST a petresponses:400:{description:Global fail, schema:PetSchema}get:consumes:[application/json,application/xml,application/xml+dec1]description:Get a petparameters:-{description:Path Parameter description in Markdown., in:path, name:kind, required:true, type:string}-{description:Query Parameter description in Markdown., in:query, name:offset, type:string}produces:[application/json,application/xml]responses:200:description:A pet to be returnedschema:{$ref:'#/definitions/Pet'}400:{description:he may fail yeah yeah...., schema:PetSchema, x-400-suffix:yeahyeah....}402:{description:mefail, schema:PetSchema}security:-ApiKeyAuth:[]-AlmostBasicAuth:[]BasicAuth:[]-OAuth2:[scope1,scope2]tags:[down1,up1,get2]swagger:'2.0'tags:[]
详细信息
这是一个操作助手,允许您传递修饰视图并获取所有修饰函数的组合文档。
它需要传递给add_path
的视图。检查视图docstrings及其docd_包装decorator函数并合并所有
将文档转换为单个文档。
如果您使用decorators来管理身份验证,或者即使您有共享的错误页而您没有
希望在所有视图上分别记录常见错误状态(状态400、状态500等)。
所有文档从下到上合并,从view函数开始,到最上面的decorator结束。 decorators可以声明一个名为u(下划线)的“特殊”http方法。在所有方法中最后一个被应用, 也是自下而上,在随后的传球中。
安装
pip install apispec-decorated-crawler
许可证
这是自由软件:你可以自由地修改和重新发布它。 在法律允许的范围内,没有任何保证。