Simplify schema and error handling
This commit is contained in:
@@ -8,10 +8,56 @@ from core.util import logs
|
||||
STRICT_VALIDATION = False
|
||||
|
||||
# Raise exception if the conversion schema is not found
|
||||
STRICT_CONVERSTION = False
|
||||
STRICT_CONVERSION = False
|
||||
|
||||
# TODO: Set them to True when all message types are implemented
|
||||
|
||||
|
||||
class NoSchema(Exception):
|
||||
"""
|
||||
Raised when:
|
||||
- The schema for the message type is not found
|
||||
- The conversion schema is not found
|
||||
- There is no schema library for the exchange
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class NoSuchMethod(Exception):
|
||||
"""
|
||||
Exchange library has no such method.
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class GenericAPIError(Exception):
|
||||
"""
|
||||
Generic API error.
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class ExchangeError(Exception):
|
||||
"""
|
||||
Exchange error.
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
def is_camel_case(s):
|
||||
return s != s.lower() and s != s.upper() and "_" not in s
|
||||
|
||||
|
||||
def snake_to_camel(word):
|
||||
if is_camel_case(word):
|
||||
return word
|
||||
return "".join(x.capitalize() or "_" for x in word.split("_"))
|
||||
|
||||
|
||||
class BaseExchange(object):
|
||||
def __init__(self, account):
|
||||
name = self.__class__.__name__
|
||||
@@ -20,7 +66,6 @@ class BaseExchange(object):
|
||||
self.log = logs.get_logger(self.name)
|
||||
self.client = None
|
||||
|
||||
self.set_schema()
|
||||
self.connect()
|
||||
|
||||
def set_schema(self):
|
||||
@@ -29,53 +74,91 @@ class BaseExchange(object):
|
||||
def connect(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def convert_spec(self, response, msg_type):
|
||||
@property
|
||||
def schema(self):
|
||||
"""
|
||||
Get the schema library for the exchange.
|
||||
"""
|
||||
# Does the schemas library have a library for this exchange name?
|
||||
if hasattr(schemas, f"{self.name}_s"):
|
||||
schema_instance = getattr(schemas, f"{self.name}_s")
|
||||
else:
|
||||
raise Exception(f"No schema for {self.name} in schema mapping")
|
||||
# Does the message type have a conversion spec for this message type?
|
||||
if hasattr(schema_instance, f"{msg_type}_schema"):
|
||||
schema = getattr(schema_instance, f"{msg_type}_schema")
|
||||
raise NoSchema(f"No schema for {self.name} in schema mapping")
|
||||
|
||||
return schema_instance
|
||||
|
||||
def get_schema(self, method, convert=False):
|
||||
if isinstance(method, str):
|
||||
to_camel = snake_to_camel(method)
|
||||
else:
|
||||
# Let us know so we can implement it, but don't do anything with it
|
||||
self.log.error(f"No schema for message: {msg_type} - {response}")
|
||||
if STRICT_CONVERSION:
|
||||
raise Exception(f"No schema for {msg_type} in schema mapping")
|
||||
to_camel = snake_to_camel(method.__class__.__name__)
|
||||
if convert:
|
||||
to_camel += "Schema"
|
||||
# if hasattr(self.schema, method):
|
||||
# schema = getattr(self.schema, method)
|
||||
if hasattr(self.schema, to_camel):
|
||||
schema = getattr(self.schema, to_camel)
|
||||
else:
|
||||
raise NoSchema
|
||||
return schema
|
||||
|
||||
def call_method(self, method, *args, **kwargs):
|
||||
"""
|
||||
Get a method from the exchange library.
|
||||
"""
|
||||
if hasattr(self.client, method):
|
||||
response = getattr(self.client, method)(*args, **kwargs)
|
||||
if isinstance(response, list):
|
||||
response = {"itemlist": response}
|
||||
return response
|
||||
else:
|
||||
raise NoSuchMethod
|
||||
|
||||
def convert_spec(self, response, method):
|
||||
"""
|
||||
Convert an API response to the requested spec.
|
||||
:raises NoSchema: If the conversion schema is not found
|
||||
"""
|
||||
schema = self.get_schema(method, convert=True)
|
||||
|
||||
# Use glom to convert the response to the schema
|
||||
converted = glom(response, schema)
|
||||
print(f"[{self.name}] Converted of {msg_type}: {converted}")
|
||||
print(f"[{self.name}] Converted of {method}: {converted}")
|
||||
return converted
|
||||
|
||||
def call(self, method, *args, **kwargs) -> (bool, dict):
|
||||
|
||||
if hasattr(self.client, method):
|
||||
def call(self, method, *args, **kwargs):
|
||||
"""
|
||||
Call the exchange API and validate the response
|
||||
:raises NoSchema: If the method is not in the schema mapping
|
||||
:raises ValidationError: If the response cannot be validated
|
||||
"""
|
||||
try:
|
||||
response = self.call_method(method, *args, **kwargs)
|
||||
try:
|
||||
response = getattr(self.client, method)(*args, **kwargs)
|
||||
if isinstance(response, list):
|
||||
response = {"itemlist": response}
|
||||
if method not in self.schema:
|
||||
self.log.error(f"Method cannot be validated: {method}")
|
||||
self.log.debug(f"Response: {response}")
|
||||
if STRICT_VALIDATION:
|
||||
return (False, f"Method cannot be validated: {method}")
|
||||
return (True, response)
|
||||
schema = self.get_schema(method)
|
||||
# Return a dict of the validated response
|
||||
response_valid = self.schema[method](**response).dict()
|
||||
# Convert the response to a format that we can use
|
||||
response_converted = self.convert_spec(response_valid, method)
|
||||
return (True, response_converted)
|
||||
except ValidationError as e:
|
||||
self.log.error(f"Could not validate response: {e}")
|
||||
return (False, e)
|
||||
except Exception as e:
|
||||
self.log.error(f"Error calling {method}: {e}")
|
||||
return (False, e)
|
||||
else:
|
||||
return (False, "No such method")
|
||||
response_valid = schema(**response).dict()
|
||||
except NoSchema:
|
||||
self.log.error(f"Method cannot be validated: {method}")
|
||||
self.log.debug(f"Response: {response}")
|
||||
if STRICT_VALIDATION:
|
||||
raise
|
||||
# Return the response as is
|
||||
response_valid = response
|
||||
|
||||
# Convert the response to a format that we can use
|
||||
response_converted = self.convert_spec(response_valid, method)
|
||||
# return (True, response_converted)
|
||||
return response_converted
|
||||
except ValidationError as e:
|
||||
self.log.error(f"Could not validate response: {e}")
|
||||
raise
|
||||
except NoSuchMethod:
|
||||
self.log.error(f"Method not found: {method}")
|
||||
raise
|
||||
except Exception as e:
|
||||
self.log.error(f"Error calling method: {e}")
|
||||
raise GenericAPIError(e)
|
||||
|
||||
def get_account(self):
|
||||
raise NotImplementedError
|
||||
|
||||
Reference in New Issue
Block a user