代码拉取完成,页面将自动刷新
import json
import httpx
from . import consts as c, utils, exceptions
class Client(object):
def __init__(self, api_key, api_secret_key, passphrase, use_server_time=False, flag='1'):
self.API_KEY = api_key
self.API_SECRET_KEY = api_secret_key
self.PASSPHRASE = passphrase
self.use_server_time = use_server_time
self.flag = flag
self.client = httpx.Client(base_url='https://www.okex.com', http2=True)
def _request(self, method, request_path, params):
if method == c.GET:
request_path = request_path + utils.parse_params_to_str(params)
timestamp = utils.get_timestamp()
if self.use_server_time:
timestamp = self._get_timestamp()
body = json.dumps(params) if method == c.POST else ""
sign = utils.sign(utils.pre_hash(timestamp, method, request_path, str(body)), self.API_SECRET_KEY)
header = utils.get_header(self.API_KEY, sign, timestamp, self.PASSPHRASE, self.flag)
response = None
if method == c.GET:
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.OkexAPIException(response)
return response.json()
def _request_without_params(self, method, request_path):
return self._request(method, request_path, {})
def _request_with_params(self, method, request_path, params):
return self._request(method, request_path, params)
def _get_timestamp(self):
request_path = c.API_URL + c.SERVER_TIMESTAMP_URL
response = self.client.get(request_path)
if response.status_code == 200:
return response.json()['ts']
else:
return ""
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。