Ai
2 Star 4 Fork 3

Suuuch/Open-API-SDK-V5

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
client.py 1.83 KB
一键复制 编辑 原始数据 按行查看 历史
刘玉超 提交于 2021-05-31 15:35 +08:00 . add http2 request
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 ""
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/suuuch/Open-API-SDK-V5.git
git@gitee.com:suuuch/Open-API-SDK-V5.git
suuuch
Open-API-SDK-V5
Open-API-SDK-V5
main

搜索帮助