From 7f9a76d9a624f81c4776b447ca36579fbb723bb9 Mon Sep 17 00:00:00 2001 From: zhangjiexian Date: Fri, 17 Mar 2023 22:26:51 +0800 Subject: [PATCH 1/5] fix request path of server timestamp --- okx/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/okx/client.py b/okx/client.py index 9911080..1f74516 100644 --- a/okx/client.py +++ b/okx/client.py @@ -49,7 +49,7 @@ class Client(object): return self._request(method, request_path, params) def _get_timestamp(self): - request_path = c.API_URL + c.SERVER_TIMESTAMP_URL + request_path = base_api + c.SERVER_TIMESTAMP_URL response = self.client.get(request_path) if response.status_code == 200: return response.json()['ts'] From 317ccc9bf3149077cdde416aec6995f29cec1cd5 Mon Sep 17 00:00:00 2001 From: zhangjiexian Date: Mon, 20 Mar 2023 20:52:01 +0800 Subject: [PATCH 2/5] retry while http request failed --- okx/client.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/okx/client.py b/okx/client.py index 1f74516..920f8bb 100644 --- a/okx/client.py +++ b/okx/client.py @@ -4,6 +4,7 @@ import httpx from . import consts as c, utils, exceptions +import time class Client(object): @@ -38,15 +39,32 @@ class Client(object): response = self.client.get(request_path, headers=header) elif method == c.POST: response = self.client.post(request_path, data=body, headers=header) + return response + + 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) + break + except: + 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) + time.sleep(1) if not str(response.status_code).startswith('2'): raise exceptions.OkxAPIException(response) return response.json() def _request_without_params(self, method, request_path): - return self._request(method, request_path, {}) + return self._request_with_params(method, request_path, {}) def _request_with_params(self, method, request_path, params): - return self._request(method, request_path, params) + return self._request_until_success(method, request_path, params) def _get_timestamp(self): request_path = base_api + c.SERVER_TIMESTAMP_URL From 1c8b51a7b7fb92895f7cedb0f8bde34752685f4c Mon Sep 17 00:00:00 2001 From: zhangjiexian Date: Mon, 20 Mar 2023 23:45:49 +0800 Subject: [PATCH 3/5] show traceback msg while exception --- okx/client.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/okx/client.py b/okx/client.py index 920f8bb..62ee6cf 100644 --- a/okx/client.py +++ b/okx/client.py @@ -6,6 +6,8 @@ from . import consts as c, utils, exceptions import time +import traceback + class Client(object): def __init__(self, api_key = '-1', api_secret_key = '-1', passphrase = '-1', use_server_time=False, flag='1', base_api = 'https://www.okx.com',debug = False): @@ -49,7 +51,9 @@ class Client(object): try: response = self._request(method, request_path, params) break - except: + except Exception as e: + msg = traceback.format_exc() + print(msg) retry_times += 1 if retry_times > retry_times_max: print('reach max retry times, exit loop.') From 3084294434eb99bf4ae79833b8b322e9c204a0db Mon Sep 17 00:00:00 2001 From: zhangjiexian Date: Tue, 21 Mar 2023 03:59:50 +0800 Subject: [PATCH 4/5] re connect while reaching the max request times --- okx/client.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/okx/client.py b/okx/client.py index 62ee6cf..78326ce 100644 --- a/okx/client.py +++ b/okx/client.py @@ -8,6 +8,8 @@ import time import traceback +from httpx import _client + class Client(object): def __init__(self, api_key = '-1', api_secret_key = '-1', passphrase = '-1', use_server_time=False, flag='1', base_api = 'https://www.okx.com',debug = False): @@ -19,6 +21,7 @@ class Client(object): self.flag = flag self.domain = base_api self.debug = debug + self.request_times = 0 self.client = httpx.Client(base_url=base_api, http2=True, verify=False, timeout=None) def _request(self, method, request_path, params): @@ -41,6 +44,13 @@ class Client(object): response = self.client.get(request_path, headers=header) elif method == c.POST: response = self.client.post(request_path, data=body, headers=header) + self.request_times += 1 + # print('request times:', self.request_times) + if (self.request_times > 512): + self.client.close() + self.client._state = _client.ClientState.UNOPENED + self.request_times = 0 + # print('close the current tcp connection while request times larger than 512.') return response def _request_until_success(self, method, request_path, params): From b47f519f79087e6d3c51ced82b5e9ed24d118a9d Mon Sep 17 00:00:00 2001 From: zhangjiexian Date: Tue, 21 Mar 2023 16:20:19 +0800 Subject: [PATCH 5/5] 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']