aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ccxt_wrapper.py16
-rw-r--r--store.py31
-rw-r--r--test.py33
3 files changed, 66 insertions, 14 deletions
diff --git a/ccxt_wrapper.py b/ccxt_wrapper.py
index 260d49d..bedf84b 100644
--- a/ccxt_wrapper.py
+++ b/ccxt_wrapper.py
@@ -3,6 +3,8 @@ import decimal
3import time 3import time
4from retry.api import retry_call 4from retry.api import retry_call
5import re 5import re
6from requests.exceptions import RequestException
7from ssl import SSLError
6 8
7def _cw_exchange_sum(self, *args): 9def _cw_exchange_sum(self, *args):
8 return sum([arg for arg in args if isinstance(arg, (float, int, decimal.Decimal))]) 10 return sum([arg for arg in args if isinstance(arg, (float, int, decimal.Decimal))])
@@ -45,10 +47,16 @@ class poloniexE(poloniex):
45 self.session._parent = self 47 self.session._parent = self
46 48
47 def request_wrap(self, *args, **kwargs): 49 def request_wrap(self, *args, **kwargs):
48 r = self.origin_request(*args, **kwargs) 50 try:
49 self._parent._market.report.log_http_request(args[0], 51 r = self.origin_request(*args, **kwargs)
50 args[1], kwargs["data"], kwargs["headers"], r) 52 self._parent._market.report.log_http_request(args[0],
51 return r 53 args[1], kwargs["data"], kwargs["headers"], r)
54 return r
55 except (SSLError, RequestException) as e:
56 self._parent._market.report.log_http_request(args[0],
57 args[1], kwargs["data"], kwargs["headers"], e)
58 raise e
59
52 self.session.request = request_wrap.__get__(self.session, 60 self.session.request = request_wrap.__get__(self.session,
53 self.session.__class__) 61 self.session.__class__)
54 62
diff --git a/store.py b/store.py
index 2b5c18a..3f3718f 100644
--- a/store.py
+++ b/store.py
@@ -176,15 +176,28 @@ class ReportStore:
176 }) 176 })
177 177
178 def log_http_request(self, method, url, body, headers, response): 178 def log_http_request(self, method, url, body, headers, response):
179 self.add_log({ 179 if isinstance(response, Exception):
180 "type": "http_request", 180 self.add_log({
181 "method": method, 181 "type": "http_request",
182 "url": url, 182 "method": method,
183 "body": body, 183 "url": url,
184 "headers": headers, 184 "body": body,
185 "status": response.status_code, 185 "headers": headers,
186 "response": response.text 186 "status": -1,
187 }) 187 "response": None,
188 "error": response.__class__.__name__,
189 "error_message": str(response),
190 })
191 else:
192 self.add_log({
193 "type": "http_request",
194 "method": method,
195 "url": url,
196 "body": body,
197 "headers": headers,
198 "status": response.status_code,
199 "response": response.text
200 })
188 201
189 def log_error(self, action, message=None, exception=None): 202 def log_error(self, action, message=None, exception=None):
190 self.print_log("[Error] {}".format(action)) 203 self.print_log("[Error] {}".format(action))
diff --git a/test.py b/test.py
index 29403d4..bf679bf 100644
--- a/test.py
+++ b/test.py
@@ -71,7 +71,8 @@ class poloniexETest(unittest.TestCase):
71 super().tearDown() 71 super().tearDown()
72 72
73 def test__init(self): 73 def test__init(self):
74 with mock.patch("market.ccxt.poloniexE.session") as session: 74 with self.subTest("Nominal case"), \
75 mock.patch("market.ccxt.poloniexE.session") as session:
75 session.request.return_value = "response" 76 session.request.return_value = "response"
76 ccxt = market.ccxt.poloniexE() 77 ccxt = market.ccxt.poloniexE()
77 ccxt._market = mock.Mock 78 ccxt._market = mock.Mock
@@ -82,6 +83,21 @@ class poloniexETest(unittest.TestCase):
82 ccxt._market.report.log_http_request.assert_called_with('GET', 'URL', 'data', 83 ccxt._market.report.log_http_request.assert_called_with('GET', 'URL', 'data',
83 'headers', 'response') 84 'headers', 'response')
84 85
86 with self.subTest("Raising"),\
87 mock.patch("market.ccxt.poloniexE.session") as session:
88 session.request.side_effect = market.ccxt.RequestException("Boo")
89
90 ccxt = market.ccxt.poloniexE()
91 ccxt._market = mock.Mock
92 ccxt._market.report = mock.Mock()
93
94 with self.assertRaises(market.ccxt.RequestException, msg="Boo") as cm:
95 ccxt.session.request("GET", "URL", data="data",
96 headers="headers")
97 ccxt._market.report.log_http_request.assert_called_with('GET', 'URL', 'data',
98 'headers', cm.exception)
99
100
85 def test_nanoseconds(self): 101 def test_nanoseconds(self):
86 with mock.patch.object(market.ccxt.time, "time") as time: 102 with mock.patch.object(market.ccxt.time, "time") as time:
87 time.return_value = 123456.7890123456 103 time.return_value = 123456.7890123456
@@ -4349,6 +4365,21 @@ class ReportStoreTest(WebMockTestCase):
4349 'response': 'Hey' 4365 'response': 'Hey'
4350 }) 4366 })
4351 4367
4368 add_log.reset_mock()
4369 report_store.log_http_request("method", "url", "body",
4370 "headers", ValueError("Foo"))
4371 add_log.assert_called_once_with({
4372 'type': 'http_request',
4373 'method': 'method',
4374 'url': 'url',
4375 'body': 'body',
4376 'headers': 'headers',
4377 'status': -1,
4378 'response': None,
4379 'error': 'ValueError',
4380 'error_message': 'Foo',
4381 })
4382
4352 @mock.patch.object(market.ReportStore, "add_log") 4383 @mock.patch.object(market.ReportStore, "add_log")
4353 def test_log_market(self, add_log): 4384 def test_log_market(self, add_log):
4354 report_store = market.ReportStore(self.m) 4385 report_store = market.ReportStore(self.m)