Skip to main content

jsonapi-requests

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

Python client implementation for json api. http://jsonapi.org/


Usage example

import jsonapi_requests

api = jsonapi_requests.Api.config({
    'API_ROOT': 'https://localhost/api/2.0',
    'AUTH': ('basic_auth_login', 'basic_auth_password'),
    'VALIDATE_SSL': False,
    'TIMEOUT': 1,
})

endpoint = api.endpoint('networks/cd9c124a-acc3-4e20-8c02-3a37d460df22/available-profiles')
response = endpoint.get()

for profile in response.data:
    print(profile.attributes['name'])
# Example output: "162 Sushi"

endpoint = api.endpoint('cookies')
endpoint.post(object=jsonapi_requests.JsonApiObject(
    type='cookies',
    attributes={
        'uuid': '09d3a4fff8d64335a1ee9f1d9d054161', 
        'domain': 'some.domain.pl'
    },
))
# Example output: <ApiResponse({'data': {'id': '81', 'attributes': {'uuid': '09d3a4fff8d64335a1ee9f1d9d054161', 'domain': 'some.domain.pl'}, 'type': 'cookies'}})>

Orm example

Lets say we have api endpoint: https://localhost/api/2.0/car/2 which returns

{
    "data":{
        "id": "2",
        "type": "car",
        "attributes": {
            "color": "red"
        },
        "relationships": {
            "driver": {
                "data": {
                    "id": "3", 
                    "type": "person"
                }
            }
        }
    },
    "included": [
        {
            "id": "3",
            "type": "person",
            "attributes": {
                "name": "Kowalski"
            },
            "relationships": {
                "married-to": {
                    "data": {
                        "id": "4", 
                        "type": "person"
                    }
                }
            }
        },
        {
            "id": "4",
            "type": "person",
            "attributes": {
                "name": "Kowalska"
            },
            "relationships": {
                "married-to": {
                    "data": {
                        "id": "3", 
                        "type": "person"
                    }
                }
            }
        },
    ]
}

Then we can run:

import jsonapi_requests

api = jsonapi_requests.orm.OrmApi.config({
    'API_ROOT': 'https://localhost/api/2.0',
    'AUTH': ('basic_auth_login', 'basic_auth_password'),
    'VALIDATE_SSL': False,
    'TIMEOUT': 1,
})

class Person(jsonapi_requests.orm.ApiModel):
    class Meta:
        type = 'person'
        api = api

    name = jsonapi_requests.orm.AttributeField('name')
    married_to = jsonapi_requests.orm.RelationField('married-to')

class Car(jsonapi_requests.orm.ApiModel):
    class Meta:
        type = 'car'
        api = api

    color = jsonapi_requests.orm.AttributeField('color')
    driver = jsonapi_requests.orm.RelationField('driver')

car = Car.from_id("2")

car.color # request happens here
# Example output: 'red'

car.driver.name
# Example output:  'Kowalski'

car.driver.married_to.name
# Example output: 'Kowalska'

car.driver.married_to.married_to.name
# Example output: 'Kowalski'

Authorization HTTP header forwarding in Flask application

When using jsonapi_requests with Flask, we can set jsonapi_requests.auth.FlaskForwardAuth() as AUTH configuration option to copy authorization header from current request context. It can be useful when fetching resources from different microservices.

Installation with flask support:

pip install jsonapi-requests[flask]

Example usage:

import jsonapi_requests

api = jsonapi_requests.Api.config({
    'API_ROOT': 'https://localhost/api/2.0',
    'AUTH': jsonapi_requests.auth.FlaskForwardAuth(),
})

Documentation

For more documentation check our wiki.

Release files for jsonapi-requests 0.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jsonapi-requests 0.8.0
File Size Uploaded
jsonapi_requests-0.8.0.tar.gz 12.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jsonapi-requests 0.8.0
File Interpreter ABI Platform
jsonapi_requests-0.8.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 25.6 kB

Release files / jsonapi_requests-0.8.0.tar.gz

Download URL jsonapi_requests-0.8.0.tar.gz
Size 12.6 kB
Tags Source
SHA-256 checksum
How to use checksums
baacf281d831debeb7233e9cd88936f3bf8b681d6c1948e7c508ad2f403e55ba
BLAKE2b-256 checksum
How to use checksums
7c9c100fb1d24aeb4a86b6fffdf7cab5d393080440dc5c203648ee34d981937c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.2

Release files / jsonapi_requests-0.8.0-py2.py3-none-any.whl

Download URL jsonapi_requests-0.8.0-py2.py3-none-any.whl
Size 13.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
7ab04516cc1f5eb7a30293fd5b3ea00ca6d8c14701994da773ca00d7425aabe4
BLAKE2b-256 checksum
How to use checksums
fa9033e7d7ddf150144af3b99b5a9aba265e1079bd13be7b55605bfb09483a8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.2

Release history Release notifications | RSS feed

This release

0.8.0 This release

2 release files

0.7.0

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1

2 release files

0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page