Giter Club home page Giter Club logo

flask-rest-jsonapi's Introduction

Documentation Status

Flask-REST-JSONAPI

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

Install

pip install Flask-REST-JSONAPI

A minimal api

# -*- 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.Str(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
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 this api:

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

Flask-REST-JSONAPI vs Flask-RESTful

  • Instead of Flask-RESTful, Flask-REST-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-REST-JSONAPI is as flexible as Flask-RESTful. You can rewrite every default methods implementation to make custom work like distributing object creation.

Flask-REST-JSONAPI vs Flask-Restless

  • Flask-REST-JSONAPI is a real implementation of JSONAPI 1.0 specification. So instead of Flask-Restless, Flask-REST-JSONAPI force you to create a real logical abstration over your data models with Marshmallow. So you can create complex resource over your data.
  • Instead of Flask-Restless, Flask-REST-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 storage.
  • Like i said previously, Flask-REST-JSONAPI is a real implementation of JSONAPI 1.0 specification. So instead of Flask-Restless you can manage relationships via REST. You can create dedicated url to create a CRUD api to manage relationships.
  • Plus Flask-REST-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.
  • Instead of Flask-Restless, Flask-REST-JSONAPI is highly customizable. For example you can entirely customize you urls, define multiple urls for the same resource manager,control serialization parameters of each methods and lot of very useful parameters.
  • Finally instead of Flask-Restless, Flask-REST-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: http://flask-rest-jsonapi.readthedocs.io/en/latest/

Thanks

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

flask-rest-jsonapi's People

Contributors

akira-dev avatar karec avatar raveline avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.