anki-connect/tests/util.py

22 lines
756 B
Python
Raw Normal View History

2017-08-29 04:21:32 +00:00
import json
2020-01-05 19:24:13 +00:00
import urllib.request
2018-05-07 01:45:56 +00:00
2018-05-07 21:34:41 +00:00
def request(action, **params):
2020-01-05 19:24:13 +00:00
return {'action': action, 'params': params, 'version': 6}
2018-05-07 21:34:41 +00:00
def invoke(action, **params):
2020-01-05 19:24:13 +00:00
requestJson = json.dumps(request(action, **params)).encode('utf-8')
response = json.load(urllib.request.urlopen(urllib.request.Request('http://localhost:8765', 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'])
2020-01-05 19:24:13 +00:00
2018-05-07 02:01:24 +00:00
return response['result']