Merge pull request #8 from Hearty07/master
fix request path of server timestamp
This commit is contained in:
commit
b414a60057
|
|
@ -4,6 +4,11 @@ import httpx
|
|||
|
||||
from . import consts as c, utils, exceptions
|
||||
|
||||
import time
|
||||
|
||||
import traceback
|
||||
|
||||
from httpx import _client
|
||||
|
||||
class Client(object):
|
||||
|
||||
|
|
@ -16,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):
|
||||
|
|
@ -38,18 +44,46 @@ 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)
|
||||
if not str(response.status_code).startswith('2'):
|
||||
raise exceptions.OkxAPIException(response)
|
||||
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):
|
||||
response = ''
|
||||
retry_times = 0
|
||||
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(e)
|
||||
retry_times += 1
|
||||
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)
|
||||
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 = c.API_URL + 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']
|
||||
|
|
|
|||
Loading…
Reference in New Issue