pyms icon indicating copy to clipboard operation
pyms copied to clipboard

Support jsonapi

Open avara1986 opened this issue 5 years ago • 0 comments

An API returns an marshmallow.Schema object or a list of dictionaries as usually to jsonify parameter like:

from marshmallow import Schema
schema.dump(query.items, many=True)
jsonify(result)

Create a output:

[
  {
    "id": 1,
    "name": "Avengers",
    "pub_date": "2020-01-20"
  },
  {
    "id": 3,
    "name": "Iron Man 2",
    "pub_date": "2010-05-02"
  }
]

But the standard jsonapi say that the response MUST be like:

{
  "links": {
    "self": "http://example.com/articles",
    "next": "http://example.com/articles?page[offset]=2",
    "last": "http://example.com/articles?page[offset]=10"
  },
  "data": [{
    "id": 1,
    "name": "Avengers",
    "pub_date": "2020-01-20"
  },
  {
    "id": 3,
    "name": "Iron Man 2",
    "pub_date": "2010-05-02"
  }
]}
}

Create a wrapper over jsonfy to accept a SQLAlchemy paginated instance and a list of elements and create this JSON object?

An example of lask-rest-api: https://github.com/Nobatek/flask-rest-api/blob/master/flask_rest_api/pagination.py

avara1986 avatar Jan 21 '20 17:01 avara1986