Skip to content

Commit be1199b

Browse files
committed
Parse errors
1 parent 23a08a2 commit be1199b

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

_ucoinpy_test/api/test_bma.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,12 @@ def test_reverse_url_only_ipv6(self):
1919
endpoint = BMAEndpoint(None, None, "2001:0db8:0000:85a3:0000:0000:ac1f:8001", 9092)
2020
api = API(endpoint.conn_handler(), "any")
2121
self.assertEqual(api.reverse_url("http", "/test/url"), "http://2001:0db8:0000:85a3:0000:0000:ac1f:8001:9092/any/test/url")
22+
23+
def test_parse_error(self):
24+
api = API(None, "any")
25+
error = api.parse_error("""{
26+
"ucode": 1005,
27+
"message": "Document has unkown fields or wrong line ending format"
28+
}""")
29+
self.assertEqual(error["ucode"], 1005)
30+
self.assertEqual(error["message"], "Document has unkown fields or wrong line ending format")

ucoinpy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
MANAGED_API=["BASIC_MERKLED_API"]
2222

2323
__author__ = 'Caner Candan & inso'
24-
__version__ = '0.14.0'
24+
__version__ = '0.14.1'
2525
__nonsense__ = 'uCoin'
2626

2727
from . import api, documents, key

ucoinpy/api/bma/__init__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ def __str__(self):
4646
class API(object):
4747
"""APIRequest is a class used as an interface. The intermediate derivated classes are the modules and the leaf classes are the API requests."""
4848

49+
error_schema = {
50+
"type": "object",
51+
"properties": {
52+
"ucode": {
53+
"type": "number"
54+
},
55+
"message": {
56+
"type": "string"
57+
}
58+
},
59+
"required": ["ucode", "message"]
60+
}
61+
4962
def __init__(self, connection_handler, module):
5063
"""
5164
Asks a module in order to create the url used then by derivated classes.
@@ -113,6 +126,20 @@ def parse_text(self, text):
113126
except TypeError:
114127
raise jsonschema.ValidationError("Could not parse json")
115128

129+
def parse_error(self, text):
130+
"""
131+
Validate and parse the BMA answer from websocket
132+
133+
:param str text: the bma error
134+
:return: the json data
135+
"""
136+
try:
137+
data = json.loads(text)
138+
jsonschema.validate(data, self.error_schema)
139+
return data
140+
except TypeError:
141+
raise jsonschema.ValidationError("Could not parse json")
142+
116143
async def parse_response(self, response):
117144
"""
118145
Validate and parse the BMA answer

0 commit comments

Comments
 (0)