From be1199be74620d2e9865755c27b566c58400a1a0 Mon Sep 17 00:00:00 2001 From: Inso <insomniak.fr@gmail.com> Date: Fri, 29 Jan 2016 19:20:20 +0100 Subject: [PATCH] Parse errors --- _ucoinpy_test/api/test_bma.py | 9 +++++++++ ucoinpy/__init__.py | 2 +- ucoinpy/api/bma/__init__.py | 27 +++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/_ucoinpy_test/api/test_bma.py b/_ucoinpy_test/api/test_bma.py index 803393b1..c3a0f2b1 100644 --- a/_ucoinpy_test/api/test_bma.py +++ b/_ucoinpy_test/api/test_bma.py @@ -19,3 +19,12 @@ class Test_BMA_API(unittest.TestCase): endpoint = BMAEndpoint(None, None, "2001:0db8:0000:85a3:0000:0000:ac1f:8001", 9092) api = API(endpoint.conn_handler(), "any") self.assertEqual(api.reverse_url("http", "/test/url"), "http://2001:0db8:0000:85a3:0000:0000:ac1f:8001:9092/any/test/url") + + def test_parse_error(self): + api = API(None, "any") + error = api.parse_error("""{ +"ucode": 1005, +"message": "Document has unkown fields or wrong line ending format" +}""") + self.assertEqual(error["ucode"], 1005) + self.assertEqual(error["message"], "Document has unkown fields or wrong line ending format") diff --git a/ucoinpy/__init__.py b/ucoinpy/__init__.py index 3c2a706f..d233edc8 100644 --- a/ucoinpy/__init__.py +++ b/ucoinpy/__init__.py @@ -21,7 +21,7 @@ PROTOCOL_VERSION="1" MANAGED_API=["BASIC_MERKLED_API"] __author__ = 'Caner Candan & inso' -__version__ = '0.14.0' +__version__ = '0.14.1' __nonsense__ = 'uCoin' from . import api, documents, key \ No newline at end of file diff --git a/ucoinpy/api/bma/__init__.py b/ucoinpy/api/bma/__init__.py index d524b2f0..3004086a 100644 --- a/ucoinpy/api/bma/__init__.py +++ b/ucoinpy/api/bma/__init__.py @@ -46,6 +46,19 @@ class ConnectionHandler(object): class API(object): """APIRequest is a class used as an interface. The intermediate derivated classes are the modules and the leaf classes are the API requests.""" + error_schema = { + "type": "object", + "properties": { + "ucode": { + "type": "number" + }, + "message": { + "type": "string" + } + }, + "required": ["ucode", "message"] + } + def __init__(self, connection_handler, module): """ Asks a module in order to create the url used then by derivated classes. @@ -113,6 +126,20 @@ class API(object): except TypeError: raise jsonschema.ValidationError("Could not parse json") + def parse_error(self, text): + """ + Validate and parse the BMA answer from websocket + + :param str text: the bma error + :return: the json data + """ + try: + data = json.loads(text) + jsonschema.validate(data, self.error_schema) + return data + except TypeError: + raise jsonschema.ValidationError("Could not parse json") + async def parse_response(self, response): """ Validate and parse the BMA answer -- GitLab