diff --git a/_ucoinpy_test/api/test_bma.py b/_ucoinpy_test/api/test_bma.py index 803393b1f3a12717fb775b8238a74d7e97917be3..c3a0f2b1e2c73a6b5af822eaec45fc87aaa51849 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 3c2a706fe390c730914557fb89b01742284fe568..d233edc8473def83ae7e6f137ff275ebc809baa1 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 d524b2f03a5198fc48f87e9232cfb902fb7280a6..3004086a3ea49e4a974922861398c84433ba71ba 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