+ RETRIABLE_CALLS = [
+ re.compile(r"^return"),
+ re.compile(r"^cancel"),
+ re.compile(r"^closeMarginPosition$"),
+ re.compile(r"^getMarginPosition$"),
+ ]
+
+ def request(self, path, api='public', method='GET', params={}, headers=None, body=None):
+ """
+ Wrapped to allow retry of non-posting requests"
+ """
+
+ origin_request = super().request
+ kwargs = {
+ "api": api,
+ "method": method,
+ "params": params,
+ "headers": headers,
+ "body": body
+ }
+
+ 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, 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__)
+