烧瓶钢筋结合烧瓶,棉花糖,和大摇大摆的强大休息服务。
flask-rebar的Python项目详细描述
Flask Rebar结合了flask、marshmallow和swagger来提供健壮的rest服务。
功能
- 请求和响应验证-flask rebar依赖于流行的棉花糖包中的模式来验证传入请求和封送传出响应。
- automatic swagger generation-用于验证和封送处理的模式用于自动生成openapi规范(也称为swagger)。这也意味着通过Swagger UI自动生成文档。
- 错误处理-来自烧瓶钢筋的未捕获异常将转换为适当的http错误。
示例
fromflaskimportFlaskfromflask_rebarimporterrors,Rebarfrommarshmallowimportfields,Schemafrommy_appimportdatabaserebar=Rebar()# All handler URL rules will be prefixed by '/v1'registry=rebar.create_handler_registry(prefix='/v1')classTodoSchema(Schema):id=fields.Integer()complete=fields.Boolean()description=fields.String()# This schema will validate the incoming request's query stringclassGetTodosQueryStringSchema(Schema):complete=fields.Boolean()# This schema will marshal the outgoing responseclassGetTodosResponseSchema(Schema):data=fields.Nested(TodoSchema,many=True)@registry.handles(rule='/todos',method='GET',query_string_schema=GetTodosQueryStringSchema(),response_body_schema=GetTodosResponseSchema(),# for versions <= 1.7.0, use marshal_schema)defget_todos():""" This docstring will be rendered as the operation's description in the auto-generated OpenAPI specification. """# The query string has already been validated by `query_string_schema`complete=rebar.validated_args.get('complete')...# Errors are converted to appropriate HTTP errorsraiseerrors.Forbidden()...# The response will be marshaled by `marshal_schema`return{'data':[]}defcreate_app(name):app=Flask(name)rebar.init_app(app)returnappif__name__=='__main__':create_app(__name__).run()
有关更完整的示例,请查看位于examples/todo.py的示例应用程序。此示例应用程序的一些示例请求可以在examples/todo_output.md中找到。
安装
pip install flask-rebar
文档
可以找到更广泛的文档here。
贡献
还有工作要做,鼓励大家贡献!查看contribution guide了解更多信息。