JSON API (jsonapi.org) client for Python
OTHER License
.. image:: https://travis-ci.org/qvantel/jsonapi-client.svg?branch=master :target: https://travis-ci.org/qvantel/jsonapi-client
.. image:: https://coveralls.io/repos/github/qvantel/jsonapi-client/badge.svg :target: https://coveralls.io/github/qvantel/jsonapi-client
.. image:: https://img.shields.io/pypi/v/jsonapi-client.svg :target: https://pypi.python.org/pypi/jsonapi-client
.. image:: https://img.shields.io/pypi/pyversions/jsonapi-client.svg :target: https://pypi.python.org/pypi/jsonapi-client
.. image:: https://img.shields.io/badge/licence-BSD%203--clause-blue.svg :target: https://github.com/qvantel/jsonapi-client/blob/master/LICENSE.txt
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 )
From Pypi::
pip install jsonapi-client
Or from sources::
./setup.py install
.. code-block:: python
from jsonapi_client import Session, Filter, ResourceTuple
s = Session('http://localhost:8080/')
s = Session('http://localhost:8080/', enable_async=True)
s = Session('http://localhost:8080/', request_kwargs=dict(auth=HTTPBasicAuth('user', 'password'))
with Session(...) as s: your code
async with Session(..., enable_async=True): your code
s.close()
await s.close()
documents = s.get('resource_type')
documents = s.get('resource_type', 'id_of_document')
documents = await s.get('resource_type')
.. code-block:: python
filter = Filter(attribute='something', attribute2='something_else')
filter = Filter(some_dict__some_attr='something'))
include = Inclusion('related_field', 'other_related_field')
modifier = Modifier('filter[post]=1&filter[author]=2')
modifier_sum = filter + include + modifier
filtered = s.get('resource_type', modifier_sum) # AsyncIO with await
r1 = document.resources[0] # first ResourceObject of document. r2 = document.resource # if there is only 1 resource we can use this
.. code-block:: python
next_doc = document.links.next.fetch()
next_doc = await document.links.next.fetch()
for r in s.iterate('resource_type'): print(r)
async for r in s.iterate('resource_type'): print(r)
.. code-block:: python
attr1 = r1.some_attr nested_attr = r1.some_dict.some_attr
nested_attr = r1['some-dict']['some-attr']
attr2 = r1.fields.some_attr
rel = r1.some_relation attr3 = r1.some_relation.some_attr # Relationship attribute can be accessed directly
rel = r1.some_relation
await r1.some_relation.fetch()
res = r1.some_relation.resource attr3 = res.some_attr # Attribute access through ResourceObject
rel_obj = r1.relationships.relation_name
.. code-block:: python
r1.some_attr = 'something else'
r1.some_dict.some_dict.some_attr = 'something else'
r1.comments = ['1', '2']
r1.comments_or_people = [ResourceTuple('1', 'comments'), ResourceTuple('2', 'people')]
r1.comments_or_people += [ResourceTuple('1', 'people')] r1.commit()
r1.author = '3' # accepts also ResourceObjects/ResourceIdentifiers/ResourceTuple
r1.author = ResourceTuple('3', 'people')
r1.commit(meta={'some_meta': 'data'}) # Resource committing supports optional meta data
await r1.commit(meta={'some_meta': 'data'})
.. code-block:: python
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']} }} }
s = Session('http://localhost:8080/', schema=models_as_jsonschema) a = s.create('articles') # Creates empty ResourceObject of 'articles' type a.title = 'Test title'
a.commit(meta={'some_meta': 'data'})
await a.commit(meta={'some_meta': 'data'})
a.commit_metadata = {'some_meta': 'data'}
s.commit()
await s.commit()
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'} )
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'} )
.. code-block:: python
# Delete resource
cust1.delete() # Mark to be deleted
cust1.commit() # Actually delete
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:
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.