Package: pluct
A JSON Hyper Schema client that allows hypermedia navigation and
resource validation.Basic Usageimportpluct# Load a resourceitem=pluct.resource('http://myapi.com/api/item',timeout=2)# Works with connect timeout# Verifying if the resource is valid for the current schemaitem.is_valid()# Use the resource as a dictionaryfirst_title=item['subitems'][0]['title']# Accessing the item schemaitem.schema['properties']['title']# Loading a related resourcecategory=item.rel('category')# With additional parameterscategory=item.rel('category',timeout=(1,2))# You can choose from request parameters: http://docs.python-requests.org/en/latest/api/#requests.Session.requestAuthentication / Custom HTTP ClientPluctuses theSessionobject from therequestspackage as a HTTP client.Any other client with the same interface can be used.Here is an example usingalf, an OAuth 2 client:frompluctimportPluctfromalf.clientimportClientalf=Client(token_endpoint='http://myapi.com/token',client_id='client-id',client_secret='secret')# Create a pluct session using the clientpluct=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 expansionURI Templatesare supported when following resource links.The context for URL expansion will be a merge of the resourcedataattribute and theparamsparameter passed to the resource’srelmethod.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 thehreffrom thesearchlink to/api/search/article?q=fooand will load articles containing the text
“foo”:importpluct# Load a resourceitem=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 differenttypein theparamsargument, as follows:galleries=item.rel('search',params={'type':'gallery','q':'foo'})To send your own body data you can send the object as data. This will follow
your method (PUT, POST, GET or DELETE) with all data from object:galleries=item.rel('create',data=item)Schema loadingWhen a resource is loaded, a lazy-schema schema will be created and its
data will only be loaded when accessed.Pluctlooks for a schema URL on theprofileparameter of theContent-typeheader:Content-Type:application/json;profile="http://myapi.com/api/schema"References ($ref)JSON Pointerson schemas are
also supported.Pointers are identified by a dictionary with a$refkey pointing to an
external URL or a local pointer.Considering the following definitions on the/api/definitionsurl:{"address":{"type":"object","properties":{"line1":{"type":"string"},"line2":{"type":"string"},"zipcode":{"type":"integer"},}}}And this schema on/api/schemathat uses the above definitions:{"properties":{"shippingAddress":{"$ref":"http://myapi.com/api/definitions#/address"},"billingAddress":{"$ref":"http://myapi.com/api/definitions#/address"},}}ThebillingAddresscan be accessed as follows:importpluctschema=pluct.schema('http://myapi.com/api/schema')schema['properties']['billingAddress']['zipcode']=={"type":"integer"}ContributingFork the repository on Github:https://github.com/globocom/pluctCreate a virtualenv and install the dependencies:makesetupTests are on thepluct/testsdirectory, run the test suite with:maketest