miLibris / flask-rest-jsonapi

Flask extension to build REST APIs around JSONAPI 1.0 specification.
http://flask-rest-jsonapi.readthedocs.io
MIT License
597 stars 153 forks source link
flask jsonapi marshmallow sqlalchemy

.. image:: https://badge.fury.io/py/Flask-REST-JSONAPI.svg :target: https://badge.fury.io/py/Flask-REST-JSONAPI .. image:: https://travis-ci.org/miLibris/flask-rest-jsonapi.svg :target: https://travis-ci.org/miLibris/flask-rest-jsonapi .. image:: https://coveralls.io/repos/github/miLibris/flask-rest-jsonapi/badge.svg :target: https://coveralls.io/github/miLibris/flask-rest-jsonapi .. image:: https://readthedocs.org/projects/flask-rest-jsonapi/badge/?version=latest :target: http://flask-rest-jsonapi.readthedocs.io/en/latest/?badge=latest :alt: Documentation Status

Flask-REST-JSONAPI ##################

Flask-REST-JSONAPI is a flask extension for building REST APIs. It combines the power of Flask-Restless <https://flask-restless.readthedocs.io/> and the flexibility of Flask-RESTful <https://flask-restful.readthedocs.io/> around a strong specification JSONAPI 1.0 <http://jsonapi.org/>_. This framework is designed to quickly build REST APIs and fit the complexity of real life projects with legacy data and multiple data storages.

Install

pip install Flask-REST-JSONAPI

A minimal API

.. code-block:: python

# -*- coding: utf-8 -*-

from flask import Flask
from flask_rest_jsonapi import Api, ResourceDetail, ResourceList
from flask_sqlalchemy import SQLAlchemy
from marshmallow_jsonapi.flask import Schema
from marshmallow_jsonapi import fields

# Create the Flask application and the Flask-SQLAlchemy object.
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)

# Create model
class Person(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)

# Create the database.
db.create_all()

# Create schema
class PersonSchema(Schema):
    class Meta:
        type_ = 'person'
        self_view = 'person_detail'
        self_view_kwargs = {'id': '<id>'}
        self_view_many = 'person_list'

    id = fields.Integer(as_string=True, dump_only=True)
    name = fields.Str()

# Create resource managers
class PersonList(ResourceList):
    schema = PersonSchema
    data_layer = {'session': db.session,
                  'model': Person}

class PersonDetail(ResourceDetail):
    schema = PersonSchema
    data_layer = {'session': db.session,
                  'model': Person}

# Create the API object
api = Api(app)
api.route(PersonList, 'person_list', '/persons')
api.route(PersonDetail, 'person_detail', '/persons/<int:id>')

# Start the flask loop
if __name__ == '__main__':
    app.run()

This example provides the following API structure:

======================== ====== ============= =========================== URL method endpoint Usage ======================== ====== ============= =========================== /persons GET person_list Get a collection of persons /persons POST person_list Create a person /persons/ GET person_detail Get person details /persons/ PATCH person_detail Update a person /persons/ DELETE person_detail Delete a person ======================== ====== ============= ===========================

Flask-REST-JSONAPI vs Flask-RESTful <http://flask-restful-cn.readthedocs.io/en/0.3.5/a>_

Flask-REST-JSONAPI vs Flask-Restless <https://flask-restless.readthedocs.io/en/stable/>_

Documentation

Documentation available here: http://flask-rest-jsonapi.readthedocs.io/en/latest/

Thanks

Flask, marshmallow, marshmallow_jsonapi, sqlalchemy, Flask-RESTful and Flask-Restless are awesome projects. These libraries gave me inspiration to create Flask-REST-JSONAPI, so huge thanks to authors and contributors.