· 7 years ago · May 22, 2018, 06:06 AM
1import hmac
2import hashlib
3import json
4import base64
5import urllib
6import urllib.parse
7import datetime
8import requests
9
10
11class Huobi(object):
12 MARKET_URL = "https://api.huobi.pro"
13 TRADE_URL = "https://api.huobi.pro"
14
15 def __init__(self, auth):
16 self._secret = auth.get_secret()
17 self._key = auth.get_key()
18
19 def http_get_request(self, url, params, add_to_headers=None):
20 headers = {
21 "Content-type": "application/x-www-form-urlencoded",
22 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36',
23 }
24 if add_to_headers:
25 headers.update(add_to_headers)
26 postdata = urllib.parse.urlencode(params)
27 response = requests.get(url, postdata, headers=headers, timeout=5)
28 try:
29
30 if response.status_code == 200:
31 return response.json()
32 else:
33 return
34 except BaseException as e:
35 print("httpGet failed, detail is:%s,%s" % (response.text, e))
36 return
37
38 def http_post_request(self, url, params, add_to_headers=None):
39 headers = {
40 "Accept": "application/json",
41 'Content-Type': 'application/json'
42 }
43 if add_to_headers:
44 headers.update(add_to_headers)
45 postdata = json.dumps(params)
46 response = requests.post(url, postdata, headers=headers, timeout=10)
47 try:
48
49 if response.status_code == 200:
50 return response.json()
51 else:
52 return
53 except BaseException as e:
54 print("httpPost failed, detail is:%s,%s" % (response.text, e))
55 return
56
57 def api_key_get(self, params, request_path):
58 method = 'GET'
59 timestamp = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S')
60 print(timestamp)
61 params.update({'AccessKeyId': self._key,
62 'SignatureMethod': 'HmacSHA256',
63 'SignatureVersion': '2',
64 'Timestamp': timestamp})
65
66 host_url = self.TRADE_URL
67 host_name = urllib.parse.urlparse(host_url).hostname
68 host_name = host_name.lower()
69 params['Signature'] = self.createSign(params, method, host_name, request_path, self._secret)
70
71 url = host_url + request_path
72 return self.http_get_request(url, params)
73
74 def api_key_post(self, params, request_path):
75 method = 'POST'
76 timestamp = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S')
77 params_to_sign = {'AccessKeyId': self._key,
78 'SignatureMethod': 'HmacSHA256',
79 'SignatureVersion': '2',
80 'Timestamp': timestamp}
81
82 host_url = self.TRADE_URL
83 host_name = urllib.parse.urlparse(host_url).hostname
84 host_name = host_name.lower()
85 params_to_sign['Signature'] = self.createSign(params_to_sign, method, host_name, request_path, self._secret)
86 url = host_url + request_path + '?' + urllib.parse.urlencode(params_to_sign)
87 return self.http_post_request(url, params)
88
89 def createSign(self, pParams, method, host_url, request_path, secret_key):
90 sorted_params = sorted(pParams.items(), key=lambda d: d[0], reverse=False)
91 encode_params = urllib.parse.urlencode(sorted_params)
92 payload = [method, host_url, request_path, encode_params]
93 payload = '\n'.join(payload)
94 payload = payload.encode(encoding='UTF8')
95 secret_key = secret_key.encode(encoding='UTF8')
96
97 digest = hmac.new(secret_key, payload, digestmod=hashlib.sha256).digest()
98 signature = base64.b64encode(digest)
99 signature = signature.decode()
100 return signature
101
102 def get_orderbook(self, symbol):
103 pass
104
105 def get_feeinfo(self):
106 pass
107
108 def get_last_price(self, symbol, action, amount):
109 pass
110
111 def new_order(self, rate, order_type, amount, symbol, market=False):
112 pass
113
114 def move_order(self, order, rate, amount):
115 pass
116
117 def get_open_orders(self):
118 pass
119
120 def get_symbols(self):
121 pass
122
123 def cancel_order(self, order):
124 pass
125
126 def close_order(self, order):
127 pass
128
129 def get_full_balance(self):
130 pass
131
132 def get_balance(self):
133 path = "/v1/account/accounts"
134 return self.api_key_get({}, path)
135
136 def get_tickers(self, currency=None):
137 pass
138
139 def get_all_usdt_balance(self):
140 pass
141
142 def get_all_btc_balance(self):
143 pass
144
145 def get_trade_history(self, start=None, end=None, limit=1000, pairs=None):
146 pass
147
148 def get_trade_history_v2(self, start=None, end=None, limit=1000, pairs=None):
149 pass
150
151 def get_margin_position(self):
152 pass
153
154 def close_margin_position(self, symbol):
155 pass
156
157 def get_margin_info(self):
158 pass
159
160 def open_margin_position(self, symbol, rate, amount, side):
161 pass
162
163 def toggle_margin_positions(self, margin_position):
164 pass
165
166 def is_order_fulfilled(self, order):
167 pass
168
169
170class Trade(object):
171
172 @classmethod
173 def create_object_from_json(cls, data):
174 raise NotImplementedError
175
176
177class Balance(object):
178 def __init__(self, currency, amount, type=None):
179 self.currency = currency
180 self.amount = amount
181 self.type = type
182
183
184class Order(object):
185
186 @classmethod
187 def create_object_from_json(cls, data):
188 raise NotImplementedError
189
190
191class MarginPosition(object):
192
193 @classmethod
194 def create_object_from_json(cls, data):
195 raise NotImplementedError
196
197
198class MarginInfo(object):
199
200 @classmethod
201 def create_object_from_json(cls, data):
202 raise NotImplementedError
203
204 def get_json(self):
205 return {
206 "balance": self.balance,
207 "net_balance": self.net_balance,
208 "tradable_balance": self.tradable_balance,
209 "pl": self.pl
210 }