Skip to main content

JSON Hyper Schema client

Project description

Drone.io Build Status Travis Build Status

A JSON Hyper Schema client that allows hypermedia navigation and resource validation.

Basic Usage

import pluct

# Load a resource
item = pluct.resource('http://myapi.com/api/item')

# Verifying if the resource is valid for the current schema
item.is_valid()

# Use the resource as a dictionary
first_title = item['subitems'][0]['title']

# Accessing the item schema
item.schema['properties']['title']

# Loading a related resource
category = item.rel('category')

Authentication / Custom HTTP Client

Pluct uses the Session object from the requests package as a HTTP client.

Any other client with the same interface can be used.

Here is an example using alf, an OAuth 2 client:

from pluct import Pluct
from alf.client import Client

alf = Client(
    token_endpoint='http://myapi.com/token',
    client_id='client-id',
    client_secret='secret')

# Create a pluct session using the client
pluct = Pluct(client=alf)
item = pluct.resource('http://myapi.com/api/item')

All subsequent requests for schemas or resources in this session will use the same client.

Parameters and URI expansion

URI Templates are supported when following resource links.

The context for URL expansion will be a merge of the resource data attribute and the params parameter passed to the resource’s rel method.

Any variable not consumed by the URL Template will be used on the query string for the request.

Better explained in an example. Consider the following resource and schema snippets:

{
    "type": "article"
}
{
    "...": "..."
    "links": [
        {
            "rel": "search",
            "href": "/api/search/{type}"
        }
    ]
}

The next example will resolve the href from the search link to /api/search/article?q=foo and will load articles containing the text “foo”:

import pluct

# Load a resource
item = pluct.resource('http://myapi.com/api/item')

articles = item.rel('search', params={'q': 'foo'})

To search for galleries is just a matter of passing a different type in the params argument, as follows:

galleries = item.rel('search', params={'type': 'gallery', 'q': 'foo'})

Schema loading

When a resource is loaded, a lazy-schema schema will be created and its data will only be loaded when accessed.

Pluct looks for a schema URL on the profile parameter of the Content-type header:

Content-Type: application/json; profile="http://myapi.com/api/schema"

References ($ref)

JSON Pointers on schemas are also supported.

Pointers are identified by a dictionary with a $ref key pointing to an external URL or a local pointer.

Considering the following definitions on the /api/definitions url:

{
    "address": {
        "type": "object",
        "properties": {
            "line1": {"type": "string"},
            "line2": {"type": "string"},
            "zipcode": {"type": "integer"},
        }
    }
}

And this schema on /api/schema that uses the above definitions:

{
    "properties": {
        "shippingAddress": {"$ref": "http://myapi.com/api/definitions#/address"},
        "billingAddress": {"$ref": "http://myapi.com/api/definitions#/address"},
    }
}

The billingAddress can be accessed as follows:

import pluct
schema = pluct.schema('http://myapi.com/api/schema')

schema['properties']['billingAddress']['zipcode'] == {"type": "integer"}

Contributing

Fork the repository on Github: https://github.com/globocom/pluct

Create a virtualenv and install the dependencies:

make setup

Tests are on the pluct/tests directory, run the test suite with:

make test

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

pluct-1.2.2.tar.gz (6.1 kB view details)

Uploaded Source

File details

Details for the file pluct-1.2.2.tar.gz.

File metadata

  • Download URL: pluct-1.2.2.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pluct-1.2.2.tar.gz
Algorithm Hash digest
SHA256 21c065e37f0e090c7d5a185e4165bd0b022543e567f0c8884a7eb10b64130027
MD5 d5c96a775a409e2636c1073eba61429c
BLAKE2b-256 9ed2f1aec526bdf3df5ab447609046f50f182bcdcd1e0d4a2a56ef06fc3ad05a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page