X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=ccxt_wrapper.py;h=d2c9b4ce26550b0074f6632ddc2bdc972eb17bd2;hb=17fd3f752d5e37df906abddf1f13fd7ad1de6c00;hp=c500659ae99ce82ed85587b7d9b15579cc178c1d;hpb=c7c1e0b26821fdd5622f81fb456f1028d4c9ab09;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/ccxt_wrapper.py b/ccxt_wrapper.py index c500659..d2c9b4c 100644 --- a/ccxt_wrapper.py +++ b/ccxt_wrapper.py @@ -3,6 +3,8 @@ import decimal import time from retry.api import retry_call import re +from requests.exceptions import RequestException +from ssl import SSLError def _cw_exchange_sum(self, *args): return sum([arg for arg in args if isinstance(arg, (float, int, decimal.Decimal))]) @@ -21,7 +23,7 @@ class poloniexE(poloniex): Wrapped to allow retry of non-posting requests" """ - origin_request = super(poloniexE, self).request + origin_request = super().request kwargs = { "api": api, "method": method, @@ -33,10 +35,33 @@ class poloniexE(poloniex): retriable = any(re.match(call, path) for call in self.RETRIABLE_CALLS) if api == "public" or method == "GET" or retriable: return retry_call(origin_request, fargs=[path], fkwargs=kwargs, - tries=10, delay=1, exceptions=(RequestTimeout,)) + tries=10, delay=1, exceptions=(RequestTimeout, InvalidNonce)) else: return origin_request(path, **kwargs) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + # For requests logging + self.session.origin_request = self.session.request + self.session._parent = self + + def request_wrap(self, *args, **kwargs): + kwargs["headers"]["X-market-id"] = str(self._parent._market.market_id) + kwargs["headers"]["X-user-id"] = str(self._parent._market.user_id) + try: + r = self.origin_request(*args, **kwargs) + self._parent._market.report.log_http_request(args[0], + args[1], kwargs["data"], kwargs["headers"], r) + return r + except (SSLError, RequestException) as e: + self._parent._market.report.log_http_request(args[0], + args[1], kwargs["data"], kwargs["headers"], e) + raise e + + self.session.request = request_wrap.__get__(self.session, + self.session.__class__) + @staticmethod def nanoseconds(): return int(time.time() * 1000000000) @@ -208,7 +233,7 @@ class poloniexE(poloniex): return all_balances def create_exchange_order(self, symbol, type, side, amount, price=None, params={}): - return super(poloniexE, self).create_order(symbol, type, side, amount, price=price, params=params) + return super().create_order(symbol, type, side, amount, price=price, params=params) def create_margin_order(self, symbol, type, side, amount, price=None, lending_rate=None, params={}): if type == 'market': @@ -366,4 +391,14 @@ class poloniexE(poloniex): else: raise NotImplementedError + def common_currency_code(self, currency): + """ + Wrapped to avoid the currency translation + """ + return currency + def currency_id(self, currency): + """ + Wrapped to avoid the currency translation + """ + return currency