Path: blob/master/bitget-python-sdk-api/bitget/client.py
732 views
import requests1import json2from . import consts as c, utils, exceptions345class Client(object):67def __init__(self, api_key, api_secret_key, passphrase, use_server_time=False, first=False):89self.API_KEY = api_key10self.API_SECRET_KEY = api_secret_key11self.PASSPHRASE = passphrase12self.use_server_time = use_server_time13self.first = first1415def _request(self, method, request_path, params, cursor=False):16if method == c.GET:17request_path = request_path + utils.parse_params_to_str(params)18# url19url = c.API_URL + request_path2021# 获取本地时间22timestamp = utils.get_timestamp()2324# sign & header25if self.use_server_time:26# 获取服务器时间接口27timestamp = self._get_timestamp()2829body = json.dumps(params) if method == c.POST else ""30sign = utils.sign(utils.pre_hash(timestamp, method, request_path, str(body)), self.API_SECRET_KEY)31if c.SIGN_TYPE == c.RSA:32sign = utils.signByRSA(utils.pre_hash(timestamp, method, request_path, str(body)), self.API_SECRET_KEY)33header = utils.get_header(self.API_KEY, sign, timestamp, self.PASSPHRASE)3435if self.first:36print("url:", url)37print("method:", method)38print("body:", body)39print("headers:", header)40# print("sign:", sign)41self.first = False4243444546# send request47response = None48if method == c.GET:49response = requests.get(url, headers=header)50print("response : ",response.text)51elif method == c.POST:52response = requests.post(url, data=body, headers=header)53print("response : ",response.text)54#response = requests.post(url, json=body, headers=header)55elif method == c.DELETE:56response = requests.delete(url, headers=header)5758print("status:", response.status_code)59# exception handle60if not str(response.status_code).startswith('2'):61raise exceptions.BitgetAPIException(response)62try:63res_header = response.headers64if cursor:65r = dict()66try:67r['before'] = res_header['OK-BEFORE']68r['after'] = res_header['OK-AFTER']69except:70pass71return response.json(), r72else:73return response.json()7475except ValueError:76raise exceptions.BitgetRequestException('Invalid Response: %s' % response.text)7778def _request_without_params(self, method, request_path):79return self._request(method, request_path, {})8081def _request_with_params(self, method, request_path, params, cursor=False):82return self._request(method, request_path, params, cursor)8384def _get_timestamp(self):85url = c.API_URL + c.SERVER_TIMESTAMP_URL86response = requests.get(url)87if response.status_code == 200:88return response.json()['timestamp']89else:90return ""919293