Data API Example Usage

There are two steps to using the interface:

  1. Authenticate the request.
  2. Invoke the API.

Note that the API provides no discovery interfaces to understand the structure and format of the data. The invoker of the interface must be familiar with the dataset they are using, and what dimensions, aggregates, and filters are appropriate for each use case.

Here is an example of python code that interfaces with the Cereals dataset that ships as a sample within most Arcadia Enterprise installations. Note that the dataset id used in this example is 35, but it may be different on your system.

import requests
import json

# Change this to a site-specific url
prefix = 'http://127.0.0.1:8000/arc'

def _get_ticket():
  url = '%s/trustedauth/getticket' % prefix
  payload = {
    'username': 'tuser1',
  }

  r = requests.post(url, data=payload)
  if r.status_code != 200:
    print 'Error', r.status_code, r.content
    raise Exception('no ticket')

  ticket = r.content.strip()
  return ticket

def _fetch_data(ticket, dataset, dims=None, aggs=None, filters=None):
  url = '%s/trustedauth/trusted/%s/dataapi' % (prefix, ticket)
  url += '?dataset=%s' % dataset
  if dims:
    url += '&dimensions=%s' % (','.join(dims))
  if aggs:
    url += '&aggregates=%s' % (','.join(aggs))
  if filters:
    url += '&filters=%s' % (','.join(filters))

  r = requests.get(url)
  if r.status_code != 200:
    print 'Error', r.status_code, r.content
    return

  raw = r.content
  d = json.loads(raw)
  print 'data', d

def main():
  ticket = _get_ticket()
  print 'ticket', ticket

  _fetch_data(ticket, 35,
    ['manufacturer_code', 'manufacturer'],
    ['sum(sodium_mg)'], ['cereal_name=%27All-Bran%27'])
  _fetch_data(ticket, 35, 
    ['manufacturer_code', 'manufacturer'])
  _fetch_data(ticket, 35, [], ['sum(sodium_mg)'],
    ['cereal_name=%27All-Bran%27'])

if __name__ == '__main__':
  main()