Flask extension to create REST web api according to JSONAPI 1.0 specification with Flask, Marshmallow and data provider of your choice (SQLAlchemy, MongoDB, ...)
Project description
flask-rest-jsonapi-next
This is a fork of miLibris/flask-rest-jsonapi project.
flask-rest-jsonapi-next
is a flask extension for building REST APIs around a strong specification
JSON:API 1.0.
Documentation: http://flask-rest-jsonapi-next.readthedocs.io/en/latest/
Install
pip install flask-rest-jsonapi-next
A minimal API
from flask import Flask
from flask_rest_jsonapi_next 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/<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 |
Thanks
Flask, marshmallow, marshmallow_jsonapi, sqlalchemy, Flask-RESTful and Flask-Restless are awesome projects. These libraries gave me inspiration to create flask-rest-jsonapi-next, so huge thanks to authors and contributors.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for flask-rest-jsonapi-next-0.43.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b6e91db161676d76b58f221b3424246b9a7b524e92248eed3efc1199d8ac74f |
|
MD5 | bd98ab852a253949e2a68477c5856986 |
|
BLAKE2b-256 | 0f4940b937e403b00f5614bf53985c97cb29c69532636db9ce8dda0d36ff2aa5 |
Hashes for flask_rest_jsonapi_next-0.43.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5dea5d79ef652b20781ae57c893316d4ab0f39c4a3f842b27093450d34940181 |
|
MD5 | 10a9bb292ecfbc5597dde081051dd909 |
|
BLAKE2b-256 | e908fab2fdffc37aa033f5b3d635d0c64295e8be78f2593e2429c59da30c6d74 |