Skip to main content

An object-oriented framework for JSON:API clients.

Project description

jsonapi-client-framework

Json:API Client Framework provides an object-oriented approach to build your Json:API clients.

Installing

pip install jsonapi-client-framework

Usage

from jsonapi_client import JsonAPICollection, JsonAPIResourceSchema


# Create your dataclass
class Person(JsonAPIResourceSchema):
    first_name: str
    last_name: str
    year_of_birth: int


# Easy setup
class People(JsonAPICollection[Person]):
    endpoint = "/people"
    schema = Person


people = People(base_url="https://your_api.domain.com/v1")

Features

Get full results as a list

# GET https://your_api.domain.com/v1/people?page[number]=1
# GET https://your_api.domain.com/v1/people?page[number]=2
# ...
# GET https://your_api.domain.com/v1/people?page[number]=23
people_list = people.list().all()

Get a single result's page

# GET https://your_api.domain.com/v1/people?page[number]=2
people_list, meta = people.list().paginated(page=2)

# GET https://your_api.domain.com/v1/people?page[number]=2&page[size]=30
people_list, meta = people.list().paginated(page=2, size=30)

Filter results

# GET https://your_api.domain.com/v1/people?filter[date_of_birth]=1984&page=1
# ...
# GET https://your_api.domain.com/v1/people?filter[date_of_birth]=1984&page=4
people_list = people.list(filters={"date_of_birth": 1984}).all()

Sort results

# GET https://your_api.domain.com/v1/people?sort=first_name,last_name&page=1
# ...
# GET https://your_api.domain.com/v1/people?sort=first_name,last_name&page=23
people_list = people.list(sort=["first_name", "last_name"]).all()

Related resources

from jsonapi_client import JsonAPICollection, JsonAPIResourceIdentifier


@dataclass
class Movie(JsonAPIResourceSchema):
    title: str
    year: int
    # By default, the Json:API payload contain the identifer only (id and type)
    director: Person | JsonAPIResourceIdentifier


class JsonAPIMovies(JsonAPICollection[Movie]):
    endpoint = "/movies"
    schema = Movie


movies = Movies(base_url="https://your_api.domain.com/v1")
# GET https://your_api.domain.com/v1/movies/178
movie = movies.resource("178").get()
movie.director.id  # => "7"

movies_with_director = Movies(base_url="https://your_api.domain.com/v1", include="diretor")
# GET https://your_api.domain.com/v1/movies/178?include=director
movie = movies_with_director.resource("178").get()
movie.director.year_of_birth  # => 1961

# GET https://your_api.domain.com/v1/movies/178?include=director&page=1
# ...
# GET https://your_api.domain.com/v1/movies/178?include=director&page=117
movies_list = movies_with_director.list().all()

Get a single resource as an object

# GET https://your_api.domain.com/v1/people/49
person = people.resource("49").get()

Update a resource

# PUT https://your_api.domain.com/v1/movies/179
#
# Request payload
#
# {
#   "data": {
#     "attributes": {
#       "year": 1993
#     },
#     "relationships": {
#       "director": {
#         "data": {
#           "id": "55"
#         }
#       }
#     }
#   }
# }
updated_movies = movies.resource("179").update(year=1993, director={"id": "55"})

Advanced features

Authentication

Json:API Client framework uses requests library, so you can take advantage of its authentication feature.

from requests.auth import HTTPBasicAuth

people = People(base_url="https://your_api.domain.com/v1", auth=HTTPBasicAuth('user', 'pass'))

Sub-collections

from jsonapi_client import JsonAPIResourceSchema


class Movie(JsonAPIResourceSchema):
    title: str


class Theater(JsonAPIResourceSchema):
    name: str


class Theaters(JsonAPICollection[Theater]):
    endpoint = "/theaters"
    schema = Theater


class Movies(JsonAPICollection[Movie]):
    endpoint = "/movies"
    schema = Movie

    def theaters(self):
        return Theaters(base_url=f"{self.base_url}{self.endpoint}", auth=self.auth)


movies = Movies(base_url="https://your_api.domain.com/v1", auth=HTTPBasicAuth('user', 'pass'))

# GET https://your_api.domain.com/v1/movies/theaters?page=1
# ...
# GET https://your_api.domain.com/v1/movies/theaters&page=6
theaters_list = movies.theaters().list().all()

Sub-resources

from jsonapi_client import JsonAPIResourceSchema
from jsonapi_client.resource import JsonAPIResource


class Movie(JsonAPIResourceSchema):
    title: str


class Character(JsonAPIResourceSchema):
    name: str


class Characters(JsonAPICollection[Character]):
    endpoint = "/characters"
    schema = Theater


class MovieResource(JsonAPIResource[Movie]):
    def characters(self):
        return Characters(base_url=self.url, auth=self.auth)


class Movies(JsonAPICollection[Movie]):
    endpoint = "/movies"
    schema = Movie


movies = Movies(base_url="https://your_api.domain.com/v1", auth=HTTPBasicAuth('user', 'pass'))

# GET https://your_api.domain.com/v1/movies/34/characters?page=1
# ...
# GET https://your_api.domain.com/v1/movies/34/characters?page=5
characters_list = movies.resource("34").characters().list().all()

Custom encoding/decoding

from datetime import datetime

from jsonapi_client import encoders, decoders

def to_timestamp(datetime):
    return datetime.timestamp()

# Encode/decode datetime objects as timestamps
encoders.register(datetime, to_timestamp)
decoders.register(datetime, datetime.fromtimestamp)

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

jsonapi_client_framework-1.0.2.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

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

jsonapi_client_framework-1.0.2-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file jsonapi_client_framework-1.0.2.tar.gz.

File metadata

File hashes

Hashes for jsonapi_client_framework-1.0.2.tar.gz
Algorithm Hash digest
SHA256 67bec7474a501889d4625f1c86be88f6b8af1f928542e822267962b09af8cfc2
MD5 2d16adb34e166240c3dd9bcd4079f34d
BLAKE2b-256 5ed7af1817a2c10557ad040acab0d9c0bfc1a8cb60bd46538a08b37269784dbb

See more details on using hashes here.

File details

Details for the file jsonapi_client_framework-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for jsonapi_client_framework-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 48b7310989cd2200c12b892bfc708ce90affed70a4f1cd22ab520dad7f5af24d
MD5 3a4a02d5ffec5c3daba3c22df1a68199
BLAKE2b-256 f11c84a39b8853a94dede21727ef15343d2a787ff3a9b1d656a44fb2aef51c52

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