retry in 1 second but not raise exception

This commit is contained in:
zhangjiexian 2023-03-21 16:20:19 +08:00
parent 3084294434
commit b47f519f79
1 changed files with 11 additions and 9 deletions

View File

@ -56,22 +56,24 @@ class Client(object):
def _request_until_success(self, method, request_path, params): def _request_until_success(self, method, request_path, params):
response = '' response = ''
retry_times = 0 retry_times = 0
retry_times_max = 15
while True: while True:
try: try:
response = self._request(method, request_path, params) 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 break
except Exception as e: except Exception as e:
msg = traceback.format_exc() msg = traceback.format_exc()
print(msg) print(e)
retry_times += 1 retry_times += 1
if retry_times > retry_times_max: print('http request retry in 1 seconds, retry times:', retry_times)
print('reach max retry times, exit loop.')
break
print('http request failed, retry in 1 seconds ... retry times:', retry_times)
time.sleep(1) time.sleep(1)
if not str(response.status_code).startswith('2'): # if not str(response.status_code).startswith('2'):
raise exceptions.OkxAPIException(response) # raise exceptions.OkxAPIException(response)
return response.json() return response.json()
def _request_without_params(self, method, request_path): def _request_without_params(self, method, request_path):
@ -81,7 +83,7 @@ class Client(object):
return self._request_until_success(method, request_path, params) return self._request_until_success(method, request_path, params)
def _get_timestamp(self): 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) response = self.client.get(request_path)
if response.status_code == 200: if response.status_code == 200:
return response.json()['ts'] return response.json()['ts']