python: improve api error handling
This commit is contained in:
parent
65cafdd0f8
commit
f3cde63e37
1 changed files with 18 additions and 10 deletions
|
|
@ -7,6 +7,22 @@ from .errors import APIError
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def checkApiError(cstr):
|
||||||
|
result = ffi.string(cstr).decode("utf-8")
|
||||||
|
lib.FreeCString(cstr)
|
||||||
|
|
||||||
|
if result.startswith("ERR:"):
|
||||||
|
raise APIError(result)
|
||||||
|
|
||||||
|
if result == "SUCCESS.":
|
||||||
|
return
|
||||||
|
|
||||||
|
logger.debug(result)
|
||||||
|
|
||||||
|
result_dict = json.loads(result)
|
||||||
|
return result_dict
|
||||||
|
|
||||||
|
|
||||||
class _MXClient:
|
class _MXClient:
|
||||||
"""
|
"""
|
||||||
core binding
|
core binding
|
||||||
|
|
@ -49,19 +65,11 @@ class _MXClient:
|
||||||
|
|
||||||
def _sync(self):
|
def _sync(self):
|
||||||
r = lib.apiv0_startclient(self.client_id)
|
r = lib.apiv0_startclient(self.client_id)
|
||||||
result = ffi.string(r)
|
checkApiError(r)
|
||||||
lib.FreeCString(r)
|
|
||||||
# if result.startswith(b"ERR:"):
|
|
||||||
# raise APIError(result)
|
|
||||||
print("_sync: ", result)
|
|
||||||
|
|
||||||
def _stopsync(self):
|
def _stopsync(self):
|
||||||
r = lib.apiv0_stopclient(self.client_id)
|
r = lib.apiv0_stopclient(self.client_id)
|
||||||
result = ffi.string(r)
|
checkApiError(r)
|
||||||
lib.FreeCString(r)
|
|
||||||
# if result.startswith(b"ERR:"):
|
|
||||||
# raise APIError(result)
|
|
||||||
print("_stopsync: ", result)
|
|
||||||
|
|
||||||
def _sendmessage(self, data_dict):
|
def _sendmessage(self, data_dict):
|
||||||
data = json.dumps(data_dict).encode(encoding="utf-8")
|
data = json.dumps(data_dict).encode(encoding="utf-8")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue