Skip to content
Snippets Groups Projects
Commit be1199be authored by inso's avatar inso
Browse files

Parse errors

parent 23a08a2a
No related branches found
No related tags found
No related merge requests found
......@@ -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")
......@@ -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
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment