Convert API responses with Glom

This commit is contained in:
2022-11-02 18:25:34 +00:00
parent 396d838416
commit 5cb7d08614
8 changed files with 140 additions and 16 deletions

View File

@@ -1,5 +1,6 @@
from oandapyV20 import API
from oandapyV20.endpoints import accounts, orders, positions, trades
from pydantic import ValidationError
from core.exchanges import BaseExchange
from core.lib.schemas import oanda_s
@@ -18,7 +19,11 @@ class OANDAExchange(BaseExchange):
self.log.debug(f"Response: {response}")
return (False, f"Method cannot be validated: {method}")
try:
return (True, self.schema[method](**response).dict())
# 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)
@@ -66,16 +71,21 @@ class OANDAExchange(BaseExchange):
def cancel_trade(self, trade_id):
raise NotImplementedError
def get_position_info(self, asset_id):
r = positions.PositionDetails(self.account_id, asset_id)
def get_position_info(self, symbol):
r = positions.PositionDetails(self.account_id, symbol)
self.client.request(r)
return r.response
def get_all_positions(self):
items = []
r = positions.OpenPositions(accountID=self.account_id)
success, response = self.call("OpenPositions", r)
if not success:
return (success, response)
print("Positions", response)
return (True, [])
for item in response["itemlist"]:
item["account_id"] = self.account.id
item["unrealized_pl"] = float(item["unrealized_pl"])
items.append(item)
return (True, items)