Skip to main content

Asynchronous JSON API

Project description

ajsonapi: asynchronous JSON API

coverage

What is it?

ajsonapi is a Python package for creating a JSON API web server backed by a database from a user-provided object model.

How to specify an object model?

Let's look at a simple object model specification.

# model.py

from ajsonapi import (JSON_API,
                      OneToManyRelationship,
                      ManyToOneRelationship,
                      Attribute,
                      String)

class Persons(JSON_API):
    name = Attribute(String)
    articles = OneToManyRelationship('Articles', rfkey='person_id')

class Articles(JSON_API):
    title = Attribute(String)
    author = ManyToOneRelationship('Persons', lfkey='person_id')

This model contains two class definitions: Persons and Articles. A person has a name and can author zero of more articles. An article has a title and has exactly one author (who is a person). The only parts in the model that may be unobvious are the lfkey and rfkey parameters in the relationship definitions. They are abbreviations for local foreign key and remote foreign key, respectively. Ajsonapi uses these parameters to identify that Persons.articles and Articles.author are each other's reverse relationship and to persist objects and their relationships in the database.

For a more elaborate (albeit abstract) object model see ajsonapi's model for functional testing.

How to create a web server?

# app.py

from aiohttp.web import run_app
from ajsonapi import Application

import model  # Or directly include the above code snippet

async def make_app():
    app = Application()
    await app.connect_database('postgresql://user:password@localhost:5432/db')
    await app.create_tables()
    app.add_json_api_routes()
    return app.app

run_app(make_app())

What does ajsonapi provide?

From the above six line model, ajsonapi creates a web server that supports the following eighteen operations (combinations of HTTP method and URI) as described by the JSON API specification.

GET, POST                 /persons
GET, PATCH, DELETE        /persons/{id}
GET, POST, PATCH, DELETE  /persons/{id}/relationships/articles
GET                       /persons/{id}/articles
GET, POST                 /articles
GET, PATCH, DELETE        /articles/{id}
GET, PATCH                /articles/{id}/relationships/author
GET                       /articles/{id}/author

All GET operations that return a collection support the ?include, ?fields, ?filter, ?sort, and ?page query parameters. All objects created and manipulated through the web server are persisted in a Postgres database by ajsonapi.

Where to get it?

pip install ajsonapi

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

ajsonapi-0.8.6.tar.gz (43.9 kB view details)

Uploaded Source

Built Distribution

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

ajsonapi-0.8.6-py3-none-any.whl (71.6 kB view details)

Uploaded Python 3

File details

Details for the file ajsonapi-0.8.6.tar.gz.

File metadata

  • Download URL: ajsonapi-0.8.6.tar.gz
  • Upload date:
  • Size: 43.9 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.3 CPython/3.9.5

File hashes

Hashes for ajsonapi-0.8.6.tar.gz
Algorithm Hash digest
SHA256 0e11ae1ea167b45e03b6a29e5c8011077b5cb0b23fa7c1391cfed45305298c18
MD5 83e67b021adc2f45e6fd0534247a16bb
BLAKE2b-256 972b285bf76d868ca4ecd11bfc14b21e7f58e65fab9ad64bcc358d432c7f6abe

See more details on using hashes here.

File details

Details for the file ajsonapi-0.8.6-py3-none-any.whl.

File metadata

  • Download URL: ajsonapi-0.8.6-py3-none-any.whl
  • Upload date:
  • Size: 71.6 kB
  • Tags: 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.3 CPython/3.9.5

File hashes

Hashes for ajsonapi-0.8.6-py3-none-any.whl
Algorithm Hash digest
SHA256 0f2c1e738fc28ba43d9edb53afd3499a280ec9aa2b4d4c5e460f73f0d3e2ada2
MD5 2fb0c2337a0c77fa827cebc200bc8a72
BLAKE2b-256 bc5205f0d7b8bc913d6fa45ecd0fd3eb787aad863cbfa952ec2fa5c09475008c

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