JSON API (jsonapi.org) client for Python

qvantel qvantel Last update: Feb 05, 2024

image

image

image

image

image

JSON API client for Python

Introduction

Package repository: https://github.com/qvantel/jsonapi-client

This Python (3.6+) library provides easy-to-use, pythonic, ORM-like access to JSON API ( http://jsonapi.org )

  • Optional asyncio implementation
  • Optional model schema definition and validation (=> easy reads even without schema)
  • Resource caching within session

Installation

From Pypi:

pip install jsonapi-client

Or from sources:

./setup.py install

Usage

Client session

Filtering and including

Pagination

Resource attribute and relationship access

Resource updating

Creating new resources

# Creating new resources. Schema must be given. Accepts dictionary of schema models
# (key is model name and value is schema as json-schema.org).

models_as_jsonschema = {
    'articles': {'properties': {
        'title': {'type': 'string'},
        'author': {'relation': 'to-one', 'resource': ['people']},
        'comments': {'relation': 'to-many', 'resource': ['comments']},
    }},
    'people': {'properties': {
        'first-name': {'type': 'string'},
        'last-name': {'type': 'string'},
        'twitter': {'type': ['null', 'string']},
    }},
    'comments': {'properties': {
        'body': {'type': 'string'},
     'author': {'relation': 'to-one', 'resource': ['people']}
 }}
}
# If you type schema by hand, it could be more convenient to type it as yml in a file
# instead

s = Session('http://localhost:8080/', schema=models_as_jsonschema)
a = s.create('articles') # Creates empty ResourceObject of 'articles' type
a.title = 'Test title'

# Validates and performs POST request, and finally updates resource based on server response
a.commit(meta={'some_meta': 'data'})
# Or with AsyncIO, remember to await
await a.commit(meta={'some_meta': 'data'})

# Commit metadata could be also saved in advance:
a.commit_metadata = {'some_meta': 'data'}
# You can also commit all changed resources in session by
s.commit()
# or with AsyncIO
await s.commit()

# Another example of resource creation, setting attributes and relationships & committing:
# If you have underscores in your field names, you can pass them in fields keyword argument as
# a dictionary:
cust1 = s.create_and_commit('articles',
                            attribute='1',
                            dict_object__attribute='2',
                            to_one_relationship='3',
                            to_many_relationship=['1', '2'],
                            fields={'some_field_with_underscore': '1'}
                            )

# Async:
cust1 = await s.create_and_commit('articles',
                                  attribute='1',
                                  dict_object__attribute='2',
                                  to_one_relationship='3',
                                  to_many_relationship=['1', '2'],
                                  fields={'some_field_with_underscore': '1'}
                                  )

Deleting resources

Credits

License

Copyright (c) 2017, Qvantel

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • Neither the name of the Qvantel nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL QVANTEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Subscribe to our newsletter