Skip to content

Commit f23fe86

Browse files
committed
update ws
1 parent c656c8d commit f23fe86

2 files changed

Lines changed: 57 additions & 14 deletions

File tree

src/huobi/spot/ws/account.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def __init__(self, access_key: str, secret_key: str, host: str = HOST_SPOT):
1313

1414
def sub(self, data: dict, call_back_fun) -> Ws:
1515
id = data['ch']
16-
ws = Ws(self.__host, self.__path, data, call_back_fun, self.__access_key, self.__secret_key)
16+
ws = Ws(self.__host, self.__path, data, call_back_fun, self.__access_key, self.__secret_key, be_spot_account=True)
1717
ws.connect()
1818
self.__sub_dict[id] = ws
1919
return ws

src/huobi/utils/ws.py

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class Ws:
1616
def __init__(self, host: str, path: str, sub_str: dict, call_back_fun,
1717
access_key: str = None, secret_key: str = None,
18-
auto_reconnect=True):
18+
auto_reconnect=True, be_spot_account = False):
1919
self.__host = host
2020
self.__path = path
2121
self.__url = 'wss://{}{}'.format(self.__host, self.__path)
@@ -24,6 +24,7 @@ def __init__(self, host: str, path: str, sub_str: dict, call_back_fun,
2424
self.__access_key = access_key
2525
self.__secret_key = secret_key
2626
self.__auto_reconnect = auto_reconnect
27+
self.__be_spot_account = be_spot_account
2728

2829
self.__ws = None
2930
self.__be_open = False
@@ -54,24 +55,42 @@ def __send_auth_data(self, method: str, host: str, path: str, access_key: str, s
5455
timestamp = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S")
5556

5657
# get Signature
57-
suffix = 'AccessKeyId={}&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp={}'.format(
58-
access_key, parse.quote(timestamp))
58+
if self.__be_spot_account:
59+
suffix = 'accessKey={}&signatureMethod=HmacSHA256&signatureVersion=2.1&timestamp={}'.format(
60+
access_key, parse.quote(timestamp))
61+
else:
62+
suffix = 'AccessKeyId={}&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp={}'.format(
63+
access_key, parse.quote(timestamp))
5964
payload = '{}\n{}\n{}\n{}'.format(method.upper(), host, path, suffix)
6065

6166
digest = hmac.new(secret_key.encode('utf8'), payload.encode(
6267
'utf8'), digestmod=sha256).digest()
6368
signature = base64.b64encode(digest).decode()
6469

6570
# data
66-
data = {
67-
"op": "auth",
68-
"type": "api",
69-
"AccessKeyId": access_key,
70-
"SignatureMethod": "HmacSHA256",
71-
"SignatureVersion": "2",
72-
"Timestamp": timestamp,
73-
"Signature": signature
74-
}
71+
if self.__be_spot_account:
72+
data = {
73+
"action": "req",
74+
"ch": "auth",
75+
"params": {
76+
"authType": "api",
77+
"accessKey": access_key,
78+
"signatureMethod": "HmacSHA256",
79+
"signatureVersion": "2.1",
80+
"timestamp": timestamp,
81+
"signature": signature
82+
}
83+
}
84+
else:
85+
data = {
86+
"op": "auth",
87+
"type": "api",
88+
"AccessKeyId": access_key,
89+
"SignatureMethod": "HmacSHA256",
90+
"SignatureVersion": "2",
91+
"Timestamp": timestamp,
92+
"Signature": signature
93+
}
7594
data = json.dumps(data)
7695
self.__ws.send(data)
7796
logger.info('send data: {}'.format(data))
@@ -89,11 +108,35 @@ def __on_open(self, ws):
89108
logger.info('send data: {}'.format(data))
90109

91110
def __on_msg(self, ws, message):
92-
plain = gzip.decompress(message).decode()
111+
if self.__be_spot_account:
112+
plain = message
113+
else:
114+
plain = gzip.decompress(message).decode()
93115
jdata = json.loads(plain)
94116
if 'ping' in jdata:
95117
sdata = plain.replace('ping', 'pong')
118+
# logger.info(sdata)
96119
self.__ws.send(sdata)
120+
elif 'action' in jdata:
121+
act = jdata['action']
122+
if act == 'ping':
123+
sdata = plain.replace('ping', 'pong')
124+
# logger.info(sdata)
125+
self.__ws.send(sdata)
126+
elif act == 'req':
127+
logger.info(plain)
128+
if 'ch' in jdata and jdata['ch'] == 'auth' and jdata['code'] == 200:
129+
if self.__sub_str is not None:
130+
data = json.dumps(self.__sub_str)
131+
self.__ws.send(data)
132+
logger.info('send data: {}'.format(data))
133+
elif act == 'push':
134+
if self.__call_back_fun is not None:
135+
self.__call_back_fun(jdata)
136+
elif act == 'sub' or act == 'unsub':
137+
logger.info(plain)
138+
else:
139+
logger.error('unknow data: {}'.format(jdata))
97140
elif 'op' in jdata:
98141
opdata = jdata['op']
99142
if opdata == 'ping':

0 commit comments

Comments
 (0)