From: Ismaƫl Bouya Date: Wed, 4 Apr 2018 11:02:56 +0000 (+0200) Subject: Merge branch 'dev' X-Git-Tag: v1.1.2 X-Git-Url: https://git.immae.eu/?p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git;a=commitdiff_plain;h=7d5da019e51adea12a1ad52e07fc71c1ee1de6ab;hp=79ac9364372ad0cd99f3d29b7dbc2b79dcc84bf5 Merge branch 'dev' --- diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..227f825 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +*.py diff=python +/store.py export-subst 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 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))]) @@ -45,10 +47,16 @@ class poloniexE(poloniex): 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 + 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__) diff --git a/market.py b/market.py index ac3aa14..10d1ad8 100644 --- a/market.py +++ b/market.py @@ -28,6 +28,9 @@ class Market: for key in ["user_id", "market_id", "report_path", "pg_config"]: setattr(self, key, kwargs.get(key, None)) + self.report.log_market(self.args, self.user_id, self.market_id, + self.report_path, self.debug) + @classmethod def from_config(cls, config, args, **kwargs): config["apiKey"] = config.pop("key", None) diff --git a/store.py b/store.py index b3ada45..3f3718f 100644 --- a/store.py +++ b/store.py @@ -176,15 +176,28 @@ class ReportStore: }) def log_http_request(self, method, url, body, headers, response): - self.add_log({ - "type": "http_request", - "method": method, - "url": url, - "body": body, - "headers": headers, - "status": response.status_code, - "response": response.text - }) + if isinstance(response, Exception): + self.add_log({ + "type": "http_request", + "method": method, + "url": url, + "body": body, + "headers": headers, + "status": -1, + "response": None, + "error": response.__class__.__name__, + "error_message": str(response), + }) + else: + self.add_log({ + "type": "http_request", + "method": method, + "url": url, + "body": body, + "headers": headers, + "status": response.status_code, + "response": response.text + }) def log_error(self, action, message=None, exception=None): self.print_log("[Error] {}".format(action)) @@ -209,6 +222,17 @@ class ReportStore: "action": action, }) + def log_market(self, args, user_id, market_id, report_path, debug): + self.add_log({ + "type": "market", + "commit": "$Format:%H$", + "args": vars(args), + "user_id": user_id, + "market_id": market_id, + "report_path": report_path, + "debug": debug, + }) + class BalanceStore: def __init__(self, market): self.market = market diff --git a/test.py b/test.py index a9a80c5..bf679bf 100644 --- a/test.py +++ b/test.py @@ -71,7 +71,8 @@ class poloniexETest(unittest.TestCase): super().tearDown() def test__init(self): - with mock.patch("market.ccxt.poloniexE.session") as session: + with self.subTest("Nominal case"), \ + mock.patch("market.ccxt.poloniexE.session") as session: session.request.return_value = "response" ccxt = market.ccxt.poloniexE() ccxt._market = mock.Mock @@ -82,6 +83,21 @@ class poloniexETest(unittest.TestCase): ccxt._market.report.log_http_request.assert_called_with('GET', 'URL', 'data', 'headers', 'response') + with self.subTest("Raising"),\ + mock.patch("market.ccxt.poloniexE.session") as session: + session.request.side_effect = market.ccxt.RequestException("Boo") + + ccxt = market.ccxt.poloniexE() + ccxt._market = mock.Mock + ccxt._market.report = mock.Mock() + + with self.assertRaises(market.ccxt.RequestException, msg="Boo") as cm: + ccxt.session.request("GET", "URL", data="data", + headers="headers") + ccxt._market.report.log_http_request.assert_called_with('GET', 'URL', 'data', + 'headers', cm.exception) + + def test_nanoseconds(self): with mock.patch.object(market.ccxt.time, "time") as time: time.return_value = 123456.7890123456 @@ -1181,9 +1197,12 @@ class MarketTest(WebMockTestCase): with self.subTest(quiet=False): m = market.Market(self.ccxt, self.market_args(quiet=False)) report_store.assert_called_with(m, verbose_print=True) + report_store().log_market.assert_called_once() + report_store.reset_mock() with self.subTest(quiet=True): m = market.Market(self.ccxt, self.market_args(quiet=True)) report_store.assert_called_with(m, verbose_print=False) + report_store().log_market.assert_called_once() @mock.patch("market.ccxt") def test_from_config(self, ccxt): @@ -4346,6 +4365,40 @@ class ReportStoreTest(WebMockTestCase): 'response': 'Hey' }) + add_log.reset_mock() + report_store.log_http_request("method", "url", "body", + "headers", ValueError("Foo")) + add_log.assert_called_once_with({ + 'type': 'http_request', + 'method': 'method', + 'url': 'url', + 'body': 'body', + 'headers': 'headers', + 'status': -1, + 'response': None, + 'error': 'ValueError', + 'error_message': 'Foo', + }) + + @mock.patch.object(market.ReportStore, "add_log") + def test_log_market(self, add_log): + report_store = market.ReportStore(self.m) + class Args: + def __init__(self): + self.debug = True + self.quiet = False + + report_store.log_market(Args(), 4, 1, "report", True) + add_log.assert_called_once_with({ + "type": "market", + "commit": "$Format:%H$", + "args": { "debug": True, "quiet": False }, + "user_id": 4, + "market_id": 1, + "report_path": "report", + "debug": True + }) + @mock.patch.object(market.ReportStore, "print_log") @mock.patch.object(market.ReportStore, "add_log") def test_log_error(self, add_log, print_log):