Welcome to Flask-Rebar¶
Welcome to Flask-Rebar’s documentation!
Flask-Rebar combines flask, marshmallow, and swagger for robust REST services.
Features¶
- Request and Response Validation - Flask-Rebar relies on schemas from the popular Marshmallow package to validate incoming requests and marshal outgoing responses.
- Automatic Swagger Generation - The same schemas used for validation and marshaling are used to automatically generate OpenAPI specifications (a.k.a. Swagger). This also means automatic documentation via Swagger UI.
- Error Handling - Uncaught exceptions from Flask-Rebar are converted to appropriate HTTP errors.
Example¶
Here’s what a basic Flask-Rebar application looks like:
from flask import Flask
from flask_rebar import errors, Rebar
from marshmallow import fields, Schema
from my_app import database
rebar = Rebar()
# All handler URL rules will be prefixed by '/v1'
registry = rebar.create_handler_registry(prefix='/v1')
class TodoSchema(Schema):
id = fields.Integer()
complete = fields.Boolean()
description = fields.String()
# This schema will validate the incoming request's query string
class GetTodosQueryStringSchema(Schema):
complete = fields.Boolean()
# This schema will marshal the outgoing response
class GetTodosResponseSchema(Schema):
data = fields.Nested(TodoSchema, many=True)
@registry.handles(
rule='/todos',
method='GET',
query_string_schema=GetTodosQueryStringSchema(),
response_body_schema=GetTodosResponseSchema(), # For version <= 1.7.0 use marshal_schema
)
def get_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 errors
raise errors.Forbidden()
...
# The response will be marshaled by `marshal_schema`
return {'data': []}
def create_app(name):
app = Flask(name)
rebar.init_app(app)
return app
if __name__ == '__main__':
create_app(__name__).run()
- Why Flask-Rebar?
- Installation
- Basics
- API Versioning
- Swagger Generation
- Authentication
- API Reference
- Tutorials
- Recipes
- Contributing
- Version History
- Changelog
- v2.1.0 (2021-10-19)
- v2.0.2 (2021-08-09)
- v2.0.1 (2021-08-09)
- v2.0.0 (2021-07-27)
- v1.12.2 (2020-08-04)
- v1.12.1 (2020-03-26)
- v1.12.0 (2020-01-08)
- v1.11.0 (2019-10-28)
- v1.10.2 (2019-09-19)
- v1.10.1 (2019-09-19)
- v1.10.0 (2019-09-11)
- v1.9.1 (2019-08-20)
- v1.9.0 (2019-07-24)
- v1.8.1 (2019-06-14)
- v1.8.0 (2019-06-12)
- v1.7.0 (2019-06-05)
- v1.6.3 (2019-05-10)
- v1.6.2 (2019-05-08)
- v1.6.1 (2019-05-03)
- v1.6.0 (2019-05-02)
- v1.5.1 (2019-03-22)
- v1.5.0 (2019-03-22)
- v1.4.1 (2019-02-19)
- v1.4.0 (2019-01-31)
- v1.3.0 (2018-12-04)
- v1.2.0 (2018-11-29)
- v1.1.0 (2018-11-13)
- v1.0.8 (2018-10-30)
- v1.0.7 (2018-10-29)
- v1.0.6 (2018-10-11)
- v1.0.4 (2018-04-05)
- v1.0.3 (2018-03-27)
- v1.0.2 (2018-03-07)
- v1.0.1 (2018-03-07)
- v1.0.0 (2018-03-04)
- v0.1.0 (2018-03-03)