]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/commitdiff
Move request wrapper to ccxt
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Sat, 24 Mar 2018 14:18:56 +0000 (15:18 +0100)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Sat, 24 Mar 2018 15:06:44 +0000 (16:06 +0100)
ccxt_wrapper.py
market.py
test.py

index c500659ae99ce82ed85587b7d9b15579cc178c1d..4ed37d9376e53f9935c21db0c40944eb9daef95b 100644 (file)
@@ -37,6 +37,21 @@ class poloniexE(poloniex):
         else:
             return origin_request(path, **kwargs)
 
+    def __init__(self, *args, **kwargs):
+        super(poloniexE, self).__init__(*args, **kwargs)
+
+        # For requests logging
+        self.session.origin_request = self.session.request
+        self.session._parent = self
+
+        def request_wrap(self, *args, **kwargs):
+            r = self.origin_request(*args, **kwargs)
+            self._parent._market.report.log_http_request(args[0],
+                    args[1], kwargs["data"], kwargs["headers"], r)
+            return r
+        self.session.request = request_wrap.__get__(self.session,
+                self.session.__class__)
+
     @staticmethod
     def nanoseconds():
         return int(time.time() * 1000000000)
index 496ec45843319f2145f955e811ea49e1842e4761..055967cd355a0c05c37bdd64e2e6c4f494949073 100644 (file)
--- a/market.py
+++ b/market.py
@@ -33,18 +33,6 @@ class Market:
 
         ccxt_instance = ccxt.poloniexE(config)
 
-        # For requests logging
-        ccxt_instance.session.origin_request = ccxt_instance.session.request
-        ccxt_instance.session._parent = ccxt_instance
-
-        def request_wrap(self, *args, **kwargs):
-            r = self.origin_request(*args, **kwargs)
-            self._parent._market.report.log_http_request(args[0],
-                    args[1], kwargs["data"], kwargs["headers"], r)
-            return r
-        ccxt_instance.session.request = request_wrap.__get__(ccxt_instance.session,
-                ccxt_instance.session.__class__)
-
         return cls(ccxt_instance, args, **kwargs)
 
     def store_report(self):
diff --git a/test.py b/test.py
index 40c64a95d769c2d9e8dc3b6cbbe182309002317e..18616c1c848620d93a19d61c72b4edcd7c773ea1 100644 (file)
--- a/test.py
+++ b/test.py
@@ -70,6 +70,18 @@ class poloniexETest(unittest.TestCase):
         self.wm.stop()
         super(poloniexETest, self).tearDown()
 
+    def test__init(self):
+        with mock.patch("market.ccxt.poloniexE.session") as session:
+            session.request.return_value = "response"
+            ccxt = market.ccxt.poloniexE()
+            ccxt._market = mock.Mock
+            ccxt._market.report = mock.Mock()
+
+            ccxt.session.request("GET", "URL", data="data",
+                    headers="headers")
+            ccxt._market.report.log_http_request.assert_called_with('GET', 'URL', 'data',
+                    'headers', 'response')
+
     def test_nanoseconds(self):
         with mock.patch.object(market.ccxt.time, "time") as time:
             time.return_value = 123456.7890123456
@@ -1177,17 +1189,11 @@ class MarketTest(WebMockTestCase):
     def test_from_config(self, ccxt):
         with mock.patch("market.ReportStore"):
             ccxt.poloniexE.return_value = self.ccxt
-            self.ccxt.session.request.return_value = "response"
 
             m = market.Market.from_config({"key": "key", "secred": "secret"}, self.market_args())
 
             self.assertEqual(self.ccxt, m.ccxt)
 
-            self.ccxt.session.request("GET", "URL", data="data",
-                    headers="headers")
-            m.report.log_http_request.assert_called_with('GET', 'URL', 'data',
-                    'headers', 'response')
-
         m = market.Market.from_config({"key": "key", "secred": "secret"}, self.market_args(debug=True))
         self.assertEqual(True, m.debug)