Skip to main content

Flask extension to create REST web api according to JSON:API 1.0 specification with Flask, Marshmallow and data provider of your choice (SQLAlchemy, MongoDB, ...)

Project description

flask-combo-jsonapi actions flask-combo-jsonapi coverage PyPI

Flask-COMBO-JSONAPI

Flask-COMBO-JSONAPI is a flask extension for building REST APIs. It combines the power of Flask-Restless and the flexibility of Flask-RESTful around a strong specification JSONAPI 1.0. This framework is designed to quickly build REST APIs and fit the complexity of real life projects with legacy data and multiple data storages.

The main goal is to make it flexible using plugin system

Install

pip install Flask-COMBO-JSONAPI

A minimal API

from flask import Flask
from flask_combo_jsonapi import Api, ResourceDetail, ResourceList
from flask_sqlalchemy import SQLAlchemy
from marshmallow import pre_load
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/api_minimal.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.String()

    @pre_load
    def remove_id_before_deserializing(self, data, **kwargs):
        """
        We don't want to allow editing ID on POST / PATCH

        Related issues:
        https://github.com/AdCombo/flask-combo-jsonapi/issues/34
        https://github.com/miLibris/flask-rest-jsonapi/issues/193
        """
        if 'id' in data:
            del data['id']
        return data

# 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/<int:person_id>

GET

person_detail

Get person details

/persons/<int:person_id>

PATCH

person_detail

Update a person

/persons/<int:person_id>

DELETE

person_detail

Delete a person

More detailed example in the docs

Flask-COMBO-JSONAPI vs Flask-RESTful

  • In contrast to Flask-RESTful, Flask-COMBO-JSONAPI provides a default implementation of get, post, patch and delete methods around a strong specification JSONAPI 1.0. Thanks to this you can build REST API very quickly.

  • Flask-COMBO-JSONAPI is as flexible as Flask-RESTful. You can rewrite every default method implementation to make custom work like distributing object creation.

Flask-COMBO-JSONAPI vs Flask-Restless

  • Flask-COMBO-JSONAPI is a real implementation of JSONAPI 1.0 specification. So in contrast to Flask-Restless, Flask-COMBO-JSONAPI forces you to create a real logical abstration over your data models with Marshmallow. So you can create complex resource over your data.

  • In contrast to Flask-Restless, Flask-COMBO-JSONAPI can use any ORM or data storage through the data layer concept, not only SQLAlchemy. A data layer is a CRUD interface between your resource and one or more data storage so you can fetch data from any data storage of your choice or create resource that use multiple data storages.

  • Like I said previously, Flask-COMBO-JSONAPI is a real implementation of JSONAPI 1.0 specification. So in contrast to Flask-Restless you can manage relationships via REST. You can create dedicated URL to create a CRUD API to manage relationships.

  • Plus Flask-COMBO-JSONAPI helps you to design your application with strong separation between resource definition (schemas), resource management (resource class) and route definition to get a great organization of your source code.

  • In contrast to Flask-Restless, Flask-COMBO-JSONAPI is highly customizable. For example you can entirely customize your URLs, define multiple URLs for the same resource manager, control serialization parameters of each method and lots of very useful parameters.

  • Finally in contrast to Flask-Restless, Flask-COMBO-JSONAPI provides a great error handling system according to JSONAPI 1.0. Plus the exception handling system really helps the API developer to quickly find missing resources requirements.

Documentation

Documentation available here: https://flask-combo-jsonapi.readthedocs.io/

Thanks

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-COMBO-JSONAPI-1.1.0.tar.gz (28.2 kB view details)

Uploaded Source

Built Distribution

Flask_COMBO_JSONAPI-1.1.0-py2.py3-none-any.whl (34.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file Flask-COMBO-JSONAPI-1.1.0.tar.gz.

File metadata

  • Download URL: Flask-COMBO-JSONAPI-1.1.0.tar.gz
  • Upload date:
  • Size: 28.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.11

File hashes

Hashes for Flask-COMBO-JSONAPI-1.1.0.tar.gz
Algorithm Hash digest
SHA256 0ec90d1b5f00b78cd1a388a6cbe9a546bf8632fefbf6974f51e4fb0d4aac07dd
MD5 e936a8d20f1c9e1b556db878f86224ac
BLAKE2b-256 cc3365b32493de1a1fa4b4e63e9b5bb838202ec3c25f387b9f339c72bfdb5276

See more details on using hashes here.

File details

Details for the file Flask_COMBO_JSONAPI-1.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: Flask_COMBO_JSONAPI-1.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 34.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.11

File hashes

Hashes for Flask_COMBO_JSONAPI-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e28700fa4c606c106dd3e2d32b150b98e30ef074885a00a2a994d39a7a695d0d
MD5 b1e785af4c2e5627f359e7a94d32baa9
BLAKE2b-256 572b184c3f2da461ccc2eba76ecbaf30a3d1fc14af6ca409450972f650138d7f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page