From b47f519f79087e6d3c51ced82b5e9ed24d118a9d Mon Sep 17 00:00:00 2001 From: zhangjiexian Date: Tue, 21 Mar 2023 16:20:19 +0800 Subject: [PATCH] retry in 1 second but not raise exception --- okx/client.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/okx/client.py b/okx/client.py index 78326ce..f050f9f 100644 --- a/okx/client.py +++ b/okx/client.py @@ -56,22 +56,24 @@ class Client(object): def _request_until_success(self, method, request_path, params): response = '' retry_times = 0 - retry_times_max = 15 while True: try: response = self._request(method, request_path, params) + if not str(response.status_code).startswith('2'): + print('response.status_code:', response.status_code) + print('response.json.code:', response.json()['code']) + print('response.json.msg:', response.json()['msg']) + time.sleep(1) + continue break except Exception as e: msg = traceback.format_exc() - print(msg) + print(e) retry_times += 1 - if retry_times > retry_times_max: - print('reach max retry times, exit loop.') - break - print('http request failed, retry in 1 seconds ... retry times:', retry_times) + print('http request retry in 1 seconds, retry times:', retry_times) time.sleep(1) - if not str(response.status_code).startswith('2'): - raise exceptions.OkxAPIException(response) + # if not str(response.status_code).startswith('2'): + # raise exceptions.OkxAPIException(response) return response.json() def _request_without_params(self, method, request_path): @@ -81,7 +83,7 @@ class Client(object): return self._request_until_success(method, request_path, params) def _get_timestamp(self): - request_path = base_api + c.SERVER_TIMESTAMP_URL + request_path = self.domain + c.SERVER_TIMESTAMP_URL response = self.client.get(request_path) if response.status_code == 200: return response.json()['ts']