Implement more validation and conversion

This commit is contained in:
2022-11-04 07:20:14 +00:00
parent d34ac39d68
commit 60979652d9
8 changed files with 294 additions and 158 deletions

View File

@@ -1,5 +1,4 @@
from glom import glom
from pydantic import ValidationError
from core.lib import schemas
from core.util import logs
@@ -12,6 +11,8 @@ STRICT_CONVERSION = False
# TODO: Set them to True when all message types are implemented
log = logs.get_logger("exchanges")
class NoSchema(Exception):
"""
@@ -63,14 +64,10 @@ class BaseExchange(object):
name = self.__class__.__name__
self.name = name.replace("Exchange", "").lower()
self.account = account
self.log = logs.get_logger(self.name)
self.client = None
self.connect()
def set_schema(self):
raise NotImplementedError
def connect(self):
raise NotImplementedError
@@ -83,7 +80,8 @@ class BaseExchange(object):
if hasattr(schemas, f"{self.name}_s"):
schema_instance = getattr(schemas, f"{self.name}_s")
else:
raise NoSchema(f"No schema for {self.name} in schema mapping")
log.error(f"No schema library for {self.name}")
raise Exception(f"No schema library for exchange {self.name}")
return schema_instance
@@ -93,14 +91,13 @@ class BaseExchange(object):
else:
to_camel = snake_to_camel(method.__class__.__name__)
if convert:
to_camel += "Schema"
to_camel = f"{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:
self.log.error(f"Method cannot be validated: {to_camel}")
raise NoSchema(f"Method cannot be validated: {to_camel}")
raise NoSchema(f"Could not get schema: {to_camel}")
return schema
def call_method(self, method, *args, **kwargs):
@@ -124,40 +121,37 @@ class BaseExchange(object):
# Use glom to convert the response to the schema
converted = glom(response, schema)
print(f"[{self.name}] Converted of {method}: {converted}")
return converted
def validate_response(self, response, method):
schema = self.get_schema(method)
# Return a dict of the validated response
response_valid = schema(**response).dict()
return response_valid
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
"""
response = self.call_method(method, *args, **kwargs)
try:
response_valid = self.validate_response(response, method)
except NoSchema as e:
log.error(f"{e} - {response}")
response_valid = response
# Convert the response to a format that we can use
try:
response = self.call_method(method, *args, **kwargs)
try:
schema = self.get_schema(method)
# Return a dict of the validated response
response_valid = schema(**response).dict()
except NoSchema:
self.log.debug(f"No schema: {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 NoSchema as e:
log.error(f"{e} - {response}")
response_converted = response_valid
# return (True, response_converted)
return response_converted
# except Exception as e:
# self.log.error(f"Error calling method: {e}")
# log.error(f"Error calling method: {e}")
# raise GenericAPIError(e)
def get_account(self):