Serrano

Hypermedia API for Avocado

This project is maintained by cbmi

Serrano

Serrano implements a hypermedia API for Avocado. Avocado is a Django app which targets developers who are interested in letting their data do the work for them.

Install

pip install serrano

Configure

Include serrano.urls in your ROOT_URLCONF (the main urls.py):

urlpatterns = patterns('',
    ...
    url(r'^api/', include('serrano.urls')),
    ...
)

Add django.contrib.sessions to your project's INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    'django.contrib.sessions',
)

In this example, /api/ is the root endpoint for Serrano. Hitting this endpoint will expose the available URLs for the API.

Media Types

Serrano is a Hypermedia API implementation for Avocado. Defined here are the media types Serrano uses to represent data structures corresponding to those in Avocado.

Note that currently Serrano exposed plain application/json representations of the below mediatypes.

application/vnd.serrano.datacontext+json

Definition

A JSON-based media type representing a condition tree structure corresponding to Avocado's DataContext model.

Description

A tree structure with two types of nodes, condition and branch.

Node Types

Condition
{
    "id": <id>,
    "operator": <operator>,
    "value": <value>
}

Branch

{
    "type": <type>,
    "children": [<node>, <node> [, ...]]
}

Node Elements

Condition Node
id

The value can be:

operator

A string representing a valid DataField operator. Valid operators vary depending on the implementation, but should be validated downstream.

value

Any valid JSON data type.

Branch Node
type

A string that is "and" or "or" representing the type of branch or logical operation between child nodes defined in the children property.

children

An array of two or more nodes.

Examples

Single Condition
{
    "id": 2,
    "operator": "iexact",
    "value": 50
}
Branch with Two Conditions
{
    "type": "and",
    "children": [{
        "id": 2,
        "operator": "iexact",
        "value": 50
    }, {
        "id": 1,
        "operator": "in",
        "value": ["foo", "bar"]
    }
}
Branch with One Condition and One Branch
{
    "type": "or",
    "children": [{
        "id": 2,
        "operator": "iexact",
        "value": 50
    }, {
        "type": "and",
        "children": [{
            "id": 1,
            "operator": "in",
            "value": ["foo", "bar"]
        }, {
            "id": 1,
            "operator": "in",
            "value": ["baz", "qux"]
        }]
    }]
}

application/vnd.serrano.dataview+json

Definition

A JSON-based media type representing an table-based output format corresponding to Avocado's DataView model.

Description

Contains two properties columns and ordering which represent the selected output columns for the table and column ordering, respectively. Each column in this context corresponds to a DataConcept.

Example

{
    "columns": [3, 1, 8],
    "ordering": [[1, 'desc']],
}

Resources

DataField

Descriptive and other various metadata about DataField objects.

Schema

{
    "id": 4,
    "name": "Ethnicity",
    "name_plural": "Ethnicities",
    "description": "The fact or state of belonging to a social group that has a common national or cultural tradition",
    "keywords": "",
    "unit": null,
    "unit_plural": null,
    "category" : {
        "id": 2,
        "name": "Demographics",
        "order": 1,
        "parent_id": null
    },
    "app_name": "patient",
    "model_name": "demographics",
    "field_name": "ethnicity",
    "modified": "2012-02-04T15:11:03Z",
    "created": "2012-02-04T15:11:03Z",
    "published": true,
    "archived": false,
    "simple_type": "string",
    "internal_type": "char",
    "enumerable": true,
    "data_modified": "2011-10-01T11:05:10Z",
    "links": {
        "self": {
            "rel": "self",
            "href": "http://example.com/api/fields/1/",
        },
        "values": {
            "rel": "data",
            "href": "http://example.com/api/fields/1/values/"
        },
        "stats": {
            "rel": "data",
            "href": "http://example.com/api/fields/1/stats/"
        },
        "distribution": {
            "rel": "data",
            "href": "http://example.com/api/fields/1/distribution/"
        },
        "concepts": {
            "rel": "related",
            "href": "http://example.com/api/fields/1/concepts/"
        }
    }
}

Endpoints

Parameters

sort

direction

published (for privileged users)

archived (for privileged users)

query

Links

DataField Values

Returns an array of distinct values for this DataField.

Schema

[
    {
        "name": "Value 1",
        "value": "value1"
    },
    ...
]

Endpoints

Note: This endpoint may be used when a datafield is flagged as searchable and can same a query parameter q for performing searches on the values themselves.

A convention for better search implementations is to override the URL to point to a different resource which supports the same media types.

Parameters

query

DataField Stats

Various statistics about a particular DataField. The output of this is dependent on the data type.

Schema

{
    "size": null,
    "count": null,
    "max": null,
    "min": null,
    "avg": null,
    "sum": null,
    "stddev": null,
    "variance": null,
    "links": {
        "parent": {
            "rel": "parent",
            "href": "http://example.com/api/fields/1/"
        }
    }
}

Endpoints

Links

DataField Distribution

Dynamic resource for generating distribution data between one or more DataFields. This resource is only available if Numpy and SciPy has been installed as an optional dependency for Avocado.

Schema

{
    "clustered": true,
    "data": [
        {
            "count": "Foo",
            "values": [[...], ...]
        },
        ...
    ],
    "outliers": [
        ...
    ]
}

Endpoints

Parameters

dimension

nulls

cluster

Client Tools & Interfaces

Having a hypermedia API is great, but without a client to consume it, it is somewhat useless. Cilantro is Web browser-based client that provides a clean browsable interface for viewing and interacting with the APIs Serrano provides.