Skip to main content

JSONAPI 1.0 implementation for Flask.

Project description

Flask-jsonapi

Build Status Documentation Status Coverage Status Latest Version Supported Python versions Wheel Status License

JSONAPI 1.0 server implementation for Flask.

Installation

This package requires at least python 3.7. To install run: pip install flask-jsonapi. You can install SQLAlchemy support with: pip install flask-jsonapi[sqlalchemy].

Documentation

Full documentation is available at: https://flask-jsonapi.readthedocs.io/.

Simple example

Let’s create a working example of a minimal Flask application. It will expose a single resource Article as a REST endpoint with fetch/create/update/delete operations. For persistence layer, it will use an in-memory SQLite database with SQLAlchemy for storage.

Configuration

import flask
import sqlalchemy
from sqlalchemy.orm import scoped_session
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from marshmallow_jsonapi import Schema, fields

import flask_jsonapi
from flask_jsonapi.resource_repositories import sqlalchemy_repositories

db_engine = sqlalchemy.create_engine('sqlite:///')
session = scoped_session(sessionmaker(bind=db_engine))
Base = declarative_base()
Base.query = session.query_property()

class Article(Base):
    __tablename__ = 'articles'
    id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
    title = sqlalchemy.Column(sqlalchemy.String)

Base.metadata.create_all(db_engine)

class ArticleRepository(sqlalchemy_repositories.SqlAlchemyModelRepository):
    model = Article
    instance_name = 'articles'
    session = session

class ArticleSchema(Schema):
    id = fields.Int()
    title = fields.Str()

    class Meta:
        type_ = 'articles'
        strict = True

class ArticleRepositoryViewSet(flask_jsonapi.resource_repository_views.ResourceRepositoryViewSet):
    schema = ArticleSchema
    repository = ArticleRepository()

app = flask.Flask(__name__)
api = flask_jsonapi.Api(app)
api.repository(ArticleRepositoryViewSet(), 'articles', '/articles/')
app.run(host='127.0.0.1', port=5000)

Usage

Create a new Article with title “First article”:

curl -H 'Content-Type: application/vnd.api+json' \
    -H 'Accept: application/vnd.api+json' \
    http://localhost:5000/articles/ \
    --data '{"data": {"attributes": {"title": "First article"}, "type": "articles"}}' \
    2>/dev/null | python -m json.tool

Result:

{
    "data": {
        "type": "articles",
        "id": 1,
        "attributes": {
            "title": "First article"
        }
    },
    "jsonapi": {
        "version": "1.0"
    }
}

Get the list of Articles:

curl -H 'Accept: application/vnd.api+json' \
    http://localhost:5000/articles/ \
    2>/dev/null | python -m json.tool

Result:

{
    "data": [
        {
            "type": "articles",
            "id": 1,
            "attributes": {
                "title": "First article"
            }
        }
    ],
    "jsonapi": {
        "version": "1.0"
    },
    "meta": {
        "count": 1
    }
}

Running tests

virtualenv -p python3.7 ~/flask-jsonapi-virtualenv
. ~/flask-jsonapi-virtualenv/bin/activate
pip install -r base_requirements.txt
pip install -U pytest==3.0.5
pytest

Credits

Some parts of this project were written based on Flask-REST-JSONAPI.

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-jsonapi-1.3.0.tar.gz (20.6 kB view details)

Uploaded Source

Built Distribution

flask_jsonapi-1.3.0-py3-none-any.whl (36.4 kB view details)

Uploaded Python 3

File details

Details for the file flask-jsonapi-1.3.0.tar.gz.

File metadata

  • Download URL: flask-jsonapi-1.3.0.tar.gz
  • Upload date:
  • Size: 20.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.25.1 requests-toolbelt/0.9.1 urllib3/1.26.5 tqdm/4.63.0 importlib-metadata/4.0.1 keyring/23.0.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.7

File hashes

Hashes for flask-jsonapi-1.3.0.tar.gz
Algorithm Hash digest
SHA256 735fb0834264e8d8890f4e66d3b7e60cd3fdb60cebfeaef0a7d3322212e66c84
MD5 f5f03c44e19c09eb08b72205c42042f3
BLAKE2b-256 0e22f8f175953c47015c824586d443152be4fa2052b07670f97d5d2feffe2553

See more details on using hashes here.

File details

Details for the file flask_jsonapi-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: flask_jsonapi-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 36.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.25.1 requests-toolbelt/0.9.1 urllib3/1.26.5 tqdm/4.63.0 importlib-metadata/4.0.1 keyring/23.0.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.7

File hashes

Hashes for flask_jsonapi-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 96377386b28953bc8f7c0352b6e36a6d880ff9af868a76d33d1aaefdf7f9d7fa
MD5 ad79e1adcc6b6865857b40e958fe6666
BLAKE2b-256 9a1d9a294cf2abc3eaa0a9d593081a4222796cb6523a192fd4900497c84523e1

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