Skip to content

Getting started with flask rest jsonapi

Bhavesh Anand edited this page Jan 27, 2018 · 3 revisions

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.