Skip to content

Getting started with flask rest jsonapi

Bhavesh Anand edited this page Jan 27, 2018 · 3 revisions
  • flask-rest-jsonapi is an extension for Flask for quickly building rest APIs around the JSONAPI 1.0 specification
  • Beside the basic requirements, the deciding factor for choosing this extension were support for :
    • Proper relationship definitions
    • Sorting
    • Filtering
    • Pagination
  • The documentation can be found at http://flask-rest-jsonapi.readthedocs.io/en/latest/.
  • The extension depends heavily upon marshmallow-jsonapi (docs) which in turn depends upon marshmallow (docs).
  • Do not forget to refer marshmallow documentation in case of any difficulties, it will help a lot.
  • As some modifications were required in the framework, and getting them merged in the original library may take some time, so the modified copy will be kept at https://github.com/shubham-padia/flask-rest-jsonapi/ for a very short amount of time.
  • Do not hesitate to tinker with the code of flask-rest-jsonapi, in case of any difficulties, the source code will help you understand what is actually going on.
  • For any new endpoint you define, make sure you add it here .

Some technical details not mentioned properly in the documentation:

  • For every ResourceList, ResourceDetail and ResourceRelationship object, you can specify decorators,methods etc. in the following way
    class PersonList(ResourceDetail):
        schema = PersonSchema
        data_layer = {'session': db.session,
                      'model': Person}
        methods = ['GET', 'PATCH']
        decorators = (login_required, )
        get_schema_kwargs = {'only': ('name', )}
  • Decorators in flask-rest-jsonapi follow certain conventions. View Decorators should have function as their parameter.
  • Real example: the inbuilt jwt_required has been modified from
    def jwt_required(realm=None):
        """View decorator that requires a valid JWT token to be present in the request
        :param realm: an optional realm
        """
        def wrapper(fn):
            @wraps(fn)
            def decorator(*args, **kwargs):
                _jwt_required(realm or current_app.config['JWT_DEFAULT_REALM'])
                return fn(*args, **kwargs)
            return decorator
        return wrapper
    
    Refer here to
    def jwt_required(fn, realm=None):
        """
        Modified from original jwt_required to comply with `flask-rest-jsonapi` decorator conventions
        View decorator that requires a valid JWT token to be present in the request
        :param realm: an optional realm
        """
        @wraps(fn)
        def decorator(*args, **kwargs):
            _jwt_required(realm or current_app.config['JWT_DEFAULT_REALM'])
            return fn(*args, **kwargs)
        return decorator
    Refer here
  • When defining the view names in the schema i.e self_view, self_view_many, etc. Make sure you prefix your blueprint name, e.g self_view = 'v1.user_detail', the framework will not do this automatically for you.
  • Make sure you bookmark http://marshmallow.readthedocs.io/en/latest/api_reference.html#module-marshmallow.fields , this will be a constant point of reference initially.