diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-03-24 15:18:56 +0100 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-03-24 16:06:44 +0100 |
commit | 445b4a7712fb7fe45e17b6b76356dd3be42dd900 (patch) | |
tree | 80b7ac28d5d2ed26a7c52ae02d3dc251396b92fb | |
parent | c7c1e0b26821fdd5622f81fb456f1028d4c9ab09 (diff) | |
download | Trader-445b4a7712fb7fe45e17b6b76356dd3be42dd900.tar.gz Trader-445b4a7712fb7fe45e17b6b76356dd3be42dd900.tar.zst Trader-445b4a7712fb7fe45e17b6b76356dd3be42dd900.zip |
Move request wrapper to ccxt
-rw-r--r-- | ccxt_wrapper.py | 15 | ||||
-rw-r--r-- | market.py | 12 | ||||
-rw-r--r-- | test.py | 18 |
3 files changed, 27 insertions, 18 deletions
diff --git a/ccxt_wrapper.py b/ccxt_wrapper.py index c500659..4ed37d9 100644 --- a/ccxt_wrapper.py +++ b/ccxt_wrapper.py | |||
@@ -37,6 +37,21 @@ class poloniexE(poloniex): | |||
37 | else: | 37 | else: |
38 | return origin_request(path, **kwargs) | 38 | return origin_request(path, **kwargs) |
39 | 39 | ||
40 | def __init__(self, *args, **kwargs): | ||
41 | super(poloniexE, self).__init__(*args, **kwargs) | ||
42 | |||
43 | # For requests logging | ||
44 | self.session.origin_request = self.session.request | ||
45 | self.session._parent = self | ||
46 | |||
47 | def request_wrap(self, *args, **kwargs): | ||
48 | r = self.origin_request(*args, **kwargs) | ||
49 | self._parent._market.report.log_http_request(args[0], | ||
50 | args[1], kwargs["data"], kwargs["headers"], r) | ||
51 | return r | ||
52 | self.session.request = request_wrap.__get__(self.session, | ||
53 | self.session.__class__) | ||
54 | |||
40 | @staticmethod | 55 | @staticmethod |
41 | def nanoseconds(): | 56 | def nanoseconds(): |
42 | return int(time.time() * 1000000000) | 57 | return int(time.time() * 1000000000) |
@@ -33,18 +33,6 @@ class Market: | |||
33 | 33 | ||
34 | ccxt_instance = ccxt.poloniexE(config) | 34 | ccxt_instance = ccxt.poloniexE(config) |
35 | 35 | ||
36 | # For requests logging | ||
37 | ccxt_instance.session.origin_request = ccxt_instance.session.request | ||
38 | ccxt_instance.session._parent = ccxt_instance | ||
39 | |||
40 | def request_wrap(self, *args, **kwargs): | ||
41 | r = self.origin_request(*args, **kwargs) | ||
42 | self._parent._market.report.log_http_request(args[0], | ||
43 | args[1], kwargs["data"], kwargs["headers"], r) | ||
44 | return r | ||
45 | ccxt_instance.session.request = request_wrap.__get__(ccxt_instance.session, | ||
46 | ccxt_instance.session.__class__) | ||
47 | |||
48 | return cls(ccxt_instance, args, **kwargs) | 36 | return cls(ccxt_instance, args, **kwargs) |
49 | 37 | ||
50 | def store_report(self): | 38 | def store_report(self): |
@@ -70,6 +70,18 @@ class poloniexETest(unittest.TestCase): | |||
70 | self.wm.stop() | 70 | self.wm.stop() |
71 | super(poloniexETest, self).tearDown() | 71 | super(poloniexETest, self).tearDown() |
72 | 72 | ||
73 | def test__init(self): | ||
74 | with mock.patch("market.ccxt.poloniexE.session") as session: | ||
75 | session.request.return_value = "response" | ||
76 | ccxt = market.ccxt.poloniexE() | ||
77 | ccxt._market = mock.Mock | ||
78 | ccxt._market.report = mock.Mock() | ||
79 | |||
80 | ccxt.session.request("GET", "URL", data="data", | ||
81 | headers="headers") | ||
82 | ccxt._market.report.log_http_request.assert_called_with('GET', 'URL', 'data', | ||
83 | 'headers', 'response') | ||
84 | |||
73 | def test_nanoseconds(self): | 85 | def test_nanoseconds(self): |
74 | with mock.patch.object(market.ccxt.time, "time") as time: | 86 | with mock.patch.object(market.ccxt.time, "time") as time: |
75 | time.return_value = 123456.7890123456 | 87 | time.return_value = 123456.7890123456 |
@@ -1177,17 +1189,11 @@ class MarketTest(WebMockTestCase): | |||
1177 | def test_from_config(self, ccxt): | 1189 | def test_from_config(self, ccxt): |
1178 | with mock.patch("market.ReportStore"): | 1190 | with mock.patch("market.ReportStore"): |
1179 | ccxt.poloniexE.return_value = self.ccxt | 1191 | ccxt.poloniexE.return_value = self.ccxt |
1180 | self.ccxt.session.request.return_value = "response" | ||
1181 | 1192 | ||
1182 | m = market.Market.from_config({"key": "key", "secred": "secret"}, self.market_args()) | 1193 | m = market.Market.from_config({"key": "key", "secred": "secret"}, self.market_args()) |
1183 | 1194 | ||
1184 | self.assertEqual(self.ccxt, m.ccxt) | 1195 | self.assertEqual(self.ccxt, m.ccxt) |
1185 | 1196 | ||
1186 | self.ccxt.session.request("GET", "URL", data="data", | ||
1187 | headers="headers") | ||
1188 | m.report.log_http_request.assert_called_with('GET', 'URL', 'data', | ||
1189 | 'headers', 'response') | ||
1190 | |||
1191 | m = market.Market.from_config({"key": "key", "secred": "secret"}, self.market_args(debug=True)) | 1197 | m = market.Market.from_config({"key": "key", "secred": "secret"}, self.market_args(debug=True)) |
1192 | self.assertEqual(True, m.debug) | 1198 | self.assertEqual(True, m.debug) |
1193 | 1199 | ||