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

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_jsonapi-1.4.1-py3-none-any.whl (37.8 kB view details)

Uploaded Python 3

File details

Details for the file flask_jsonapi-1.4.1.tar.gz.

File metadata

  • Download URL: flask_jsonapi-1.4.1.tar.gz
  • Upload date:
  • Size: 20.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for flask_jsonapi-1.4.1.tar.gz
Algorithm Hash digest
SHA256 c78a7550f846d77b511706f204e949acdba48cfaa1039f9e23db76c81a9fdf8f
MD5 5839b8df3fccb0e7b2992dc0b181c481
BLAKE2b-256 d16719cdeb70b8f79b77bce798ef5cd9302e592689ef3a9c950da21ed4fb5184

See more details on using hashes here.

File details

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

File metadata

  • Download URL: flask_jsonapi-1.4.1-py3-none-any.whl
  • Upload date:
  • Size: 37.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for flask_jsonapi-1.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f636ab88ff2e63688bd6a292d0452a4a8216700ddee148b46389c2214056a226
MD5 9a494f82e8ec1c4e1e6a8e3588f1e706
BLAKE2b-256 95a9d61678fab5f8db1d0413cc7467646dbe0dd573f44748746b99cd2a2b8f6f

See more details on using hashes here.

Supported by

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