2017-08-29 04:21:32 +00:00
|
|
|
import json
|
|
|
|
import urllib2
|
|
|
|
|
2018-05-25 12:31:32 +00:00
|
|
|
API_VERSION = 6
|
2018-05-07 21:34:41 +00:00
|
|
|
API_URL = 'http://localhost:8765'
|
2018-05-07 01:45:56 +00:00
|
|
|
|
|
|
|
|
2018-05-07 21:34:41 +00:00
|
|
|
def request(action, **params):
|
|
|
|
return {'action': action, 'params': params, 'version': API_VERSION}
|
2018-05-07 01:45:56 +00:00
|
|
|
|
2018-05-07 21:34:41 +00:00
|
|
|
|
|
|
|
def invoke(action, **params):
|
|
|
|
requestJson = json.dumps(request(action, **params))
|
|
|
|
response = json.load(urllib2.urlopen(urllib2.Request(API_URL, requestJson)))
|
2018-05-07 02:01:24 +00:00
|
|
|
if len(response) != 2:
|
|
|
|
raise Exception('response has an unexpected number of fields')
|
|
|
|
if 'error' not in response:
|
|
|
|
raise Exception('response is missing required error field')
|
|
|
|
if 'result' not in response:
|
|
|
|
raise Exception('response is missing required result field')
|
|
|
|
if response['error'] is not None:
|
|
|
|
raise Exception(response['error'])
|
|
|
|
return response['result']
|