apispec库的chalice插件

apispec-chalice的Python项目详细描述


Latest versionTravis-CI

Chalice用于apispec(fka swagger)生成库的插件。

安装

来自PYPI:

$ pip install apispec-chalice

restful应用程序示例

fromapispecimportAPISpecfromchaliceimportChalicefrommarshmallowimportSchema,fieldsimportapispec_chalice# Create an APISpecspec=APISpec(title='Swagger Petstore',version='1.0.0',plugins=['apispec_chalice','apispec.ext.marshmallow',],)# Optional marshmallow supportclassCategorySchema(Schema):id=fields.Int()name=fields.Str(required=True)classPetSchema(Schema):category=fields.Nested(CategorySchema,many=True)name=fields.Str()classErrorSchema(Schema):status_code=fields.Int(required=True)message=fields.Str()app=Chalice(__name__)@app.route('/pets',methods=['GET','POST'])defpets(gist_id):'''
    ---
    get:
        responses:
            200:
                schema:
                    type: array
                    items: PetSchema
            404:
                schema: ErrorSchema
    post:
        responses:
            201:
                headers:
                    Location:
                        description: 'URI of new pet'
                        type: string
            400:
                schema: ErrorSchema
    '''pass@app.route('/pets/{pet_name}',methods=['GET','PUT','DELETE'])defpet(gist_id):'''
    ---
    get:
        responses:
            200:
                schema: PetSchema
            404:
                schema: ErrorSchema
    delete:
        responses:
            204:
                description: 'deleted pet'
            404:
                schema: ErrorSchema
    put:
        responses:
            204:
                description: 'deleted pet'
            400:
                schema: ErrorSchema
    '''pass# Register entities and pathsspec.definition('Category',schema=CategorySchema)spec.definition('Pet',schema=PetSchema)spec.definition('Error',schema=ErrorSchema)spec.add_path(app=app,view=pets)spec.add_path(app=app,view=pet)

生成的openapi规范

spec.to_dict()#{#   'info':{#      'title':'Swagger Petstore',#      'version':'1.0.0'#   },#   'paths':{#      '/pets':{#         'get':{#            'responses':{#               '200':{#                  'schema':{#                     'type':'array',#                     'items':{#                        '$ref':'#/definitions/Pet'#                     }#                  }#               },#               '404':{#                  'schema':{#                     '$ref':'#/definitions/Error'#                  }#               }#            }#         },#         'post':{#            'responses':{#               '201':{#                  'headers':{#                     'Location':{#                        'description':'URI of new pet',#                        'type':'string'#                     }#                  }#               },#               '400':{#                  'schema':{#                     '$ref':'#/definitions/Error'#                  }#               }#            }#         }#      },#      '/pets/{pet_name}':{#         'get':{#            'responses':{#               '200':{#                  'schema':{#                     '$ref':'#/definitions/Pet'#                  }#               },#               '404':{#                  'schema':{#                     '$ref':'#/definitions/Error'#                  }#               }#            }#         },#         'delete':{#            'responses':{#               '204':{#                  'description':'deleted pet'#               },#               '404':{#                  'schema':{#                     '$ref':'#/definitions/Error'#                  }#               }#            }#         },#         'put':{#            'responses':{#               '204':{#                  'description':'deleted pet'#               },#               '400':{#                  'schema':{#                     '$ref':'#/definitions/Error'#                  }#               }#            }#         }#      }#   },#   'tags':[##   ],#   'swagger':'2.0',#   'definitions':{#      'Category':{#         'type':'object',#         'properties':{#            'name':{#               'type':'string'#            },#            'id':{#               'type':'integer',#               'format':'int32'#            }#         },#         'required':[#            'name'#         ]#      },#      'Pet':{#         'type':'object',#         'properties':{#            'name':{#               'type':'string'#            },#            'category':{#               'type':'array',#               'items':{#                  '$ref':'#/definitions/Category'#               }#            }#         }#      },#      'Error':{#         'type':'object',#         'properties':{#            'message':{#               'type':'string'#            },#            'status_code':{#               'type':'integer',#               'format':'int32'#            }#         },#         'required':[#            'status_code'#         ]#      }#   },#   'parameters':{##   }#}

许可证

麻省理工学院许可。有关详细信息,请参阅捆绑的LICENSE文件。

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

推荐PyPI第三方库


热门话题
java使Eclipse在其控制台中显示最顶层的异常,而不是完整的堆栈跟踪   java如何为一个组件提供多个DropTargetListener?   在Eclipse包资源管理器中,有些文件不可见?   java在Spring Boot中使用@Bean配置设置类属性的默认值   在JTextPane中使用#链接的Java HTML?   java当应用程序打开时,如何将通知内容发送给活动?   java Android ROOM如何编写包含多个实体的查询,这是在哪里完成的?   Play Framework的java登录/注销问题?   java如何从安卓 Cordova/Phonegap调用javascript函数   JavaFX8如何在Java8中显示上次修改的LocalDateTime?   javabean验证中的多个约束注释   java使用JSTL设置请求属性   java在Android启动的服务中调用函数   用于检查xml是否包含键和值的java XPath表达式   在java游戏中使用斜坡因子挥杆   Java文件:尝试使用FileWriter将结果附加到已经存在的文件中   bootclasspath Java Xbootclasspath,相对路径   java我如何让这个“怪物战斗模拟器”工作?   swing使用动作侦听器隐藏和显示java桌面应用程序