Wrap API calls in helper and validate response
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
from pydantic import ValidationError
|
||||
|
||||
from core.util import logs
|
||||
|
||||
|
||||
@@ -8,11 +10,36 @@ class BaseExchange(object):
|
||||
self.log = logs.get_logger(name)
|
||||
self.client = None
|
||||
|
||||
self.set_schema()
|
||||
self.connect()
|
||||
|
||||
def set_schema(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def connect(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def call(self, method, *args, **kwargs) -> (bool, dict):
|
||||
|
||||
if hasattr(self.client, method):
|
||||
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}")
|
||||
return (False, f"Method cannot be validated: {method}")
|
||||
return (True, self.schema[method](**response).dict())
|
||||
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")
|
||||
|
||||
def get_account(self):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
Reference in New Issue
Block a user