JANUS Web API


In [1]:
from __future__ import print_function

import json
import requests

Query API database with get requests


In [2]:
uri = 'http://localhost:5000/api/monte'

models = json.loads(requests.get(uri).text)
tmp = map(print, models['models'])


{u'name': u'rengers'}
{u'name': u'showq'}

In [3]:
uri = 'http://localhost:5000/api/monte/rengers'

jobs = json.loads(requests.get(uri).text)
tmp = map(print, jobs['jobs'])


{u'id': 2567556}
{u'id': 2567557}
{u'id': 2567571}
{u'id': 2567598}
{u'id': 2567779}
{u'id': 2567781}
{u'id': 2567784}
{u'id': 2567970}
{u'id': 2567984}
{u'id': 2567986}
{u'id': 2567989}
{u'id': 2567992}
{u'id': 2567993}
{u'id': 2567994}

Submit a job with post


In [4]:
uri = 'http://localhost:5000/api/monte/rengers'

Define the model parameters


In [5]:
params = dict()
params['pmmphr'] = 40
params['taucwepp'] = 6.2
params['hcelev'] = 2.0
params['time'] = 15

Call the model


In [6]:
r = requests.post(uri, data=json.dumps(params))
print(r.text)


{
  "success": "True"
}

Print last 5 jobs


In [ ]:
jobs = json.loads(requests.get(uri).text)
ids = map(lambda x: x['id'], jobs['jobs'])
tmp = map(print, ids[-5:])

In [8]:
ids[-1]


Out[8]:
2577701

Check the output of a job


In [10]:
uri = 'http://localhost:5000/api/monte/rengers/' + str(ids[-1])

r = requests.get(uri)
print(r.text)


---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-10-12efe567a7b6> in <module>()
      1 uri = 'http://localhost:5000/api/monte/rengers/' + str(ids[-1])
      2 
----> 3 r = requests.get(uri)
      4 print(r.text)

/Users/mlunacek/anaconda/lib/python2.7/site-packages/requests/api.pyc in get(url, **kwargs)
     53 
     54     kwargs.setdefault('allow_redirects', True)
---> 55     return request('get', url, **kwargs)
     56 
     57 

/Users/mlunacek/anaconda/lib/python2.7/site-packages/requests/api.pyc in request(method, url, **kwargs)
     42 
     43     session = sessions.Session()
---> 44     return session.request(method=method, url=url, **kwargs)
     45 
     46 

/Users/mlunacek/anaconda/lib/python2.7/site-packages/requests/sessions.pyc in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert)
    381             'allow_redirects': allow_redirects,
    382         }
--> 383         resp = self.send(prep, **send_kwargs)
    384 
    385         return resp

/Users/mlunacek/anaconda/lib/python2.7/site-packages/requests/sessions.pyc in send(self, request, **kwargs)
    484         start = datetime.utcnow()
    485         # Send the request
--> 486         r = adapter.send(request, **kwargs)
    487         # Total elapsed time of the request (approximately)
    488         r.elapsed = datetime.utcnow() - start

/Users/mlunacek/anaconda/lib/python2.7/site-packages/requests/adapters.pyc in send(self, request, stream, timeout, verify, cert, proxies)
    328                     decode_content=False,
    329                     retries=self.max_retries,
--> 330                     timeout=timeout
    331                 )
    332 

/Users/mlunacek/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.pyc in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, **response_kw)
    478             httplib_response = self._make_request(conn, method, url,
    479                                                   timeout=timeout,
--> 480                                                   body=body, headers=headers)
    481 
    482             # If we're going to release the connection in ``finally:``, then

/Users/mlunacek/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.pyc in _make_request(self, conn, method, url, timeout, **httplib_request_kw)
    311         try:
    312             try: # Python 2.7+, use buffering of HTTP responses
--> 313                 httplib_response = conn.getresponse(buffering=True)
    314             except TypeError: # Python 2.6 and older
    315                 httplib_response = conn.getresponse()

/Users/mlunacek/anaconda/lib/python2.7/httplib.pyc in getresponse(self, buffering)
   1043         response = self.response_class(*args, **kwds)
   1044 
-> 1045         response.begin()
   1046         assert response.will_close != _UNKNOWN
   1047         self.__state = _CS_IDLE

/Users/mlunacek/anaconda/lib/python2.7/httplib.pyc in begin(self)
    407         # read until we get a non-100 response
    408         while True:
--> 409             version, status, reason = self._read_status()
    410             if status != CONTINUE:
    411                 break

/Users/mlunacek/anaconda/lib/python2.7/httplib.pyc in _read_status(self)
    363     def _read_status(self):
    364         # Initialize with Simple-Response defaults
--> 365         line = self.fp.readline(_MAXLINE + 1)
    366         if len(line) > _MAXLINE:
    367             raise LineTooLong("header line")

/Users/mlunacek/anaconda/lib/python2.7/socket.pyc in readline(self, size)
    474             while True:
    475                 try:
--> 476                     data = self._sock.recv(self._rbufsize)
    477                 except error, e:
    478                     if e.args[0] == EINTR:

KeyboardInterrupt: 

In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt

x = map( lambda x: float(x.strip()), r.text.split(',')[1:-1])
plt.plot(x)
plt.ylim(990,1025)

In [ ]: