-
Notifications
You must be signed in to change notification settings - Fork 0
/
chainx_websocket_client.py
executable file
·55 lines (45 loc) · 1.4 KB
/
chainx_websocket_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from websocket import create_connection
import json
import logging
class RPCError(Exception):
pass
class WebsocketClient():
def __init__(self, websocket_url):
self.ws = create_connection(websocket_url)
self.request_id = 0
def request(self, method_name, params):
payload = {
'id': self.request_id,
"jsonrpc":"2.0",
'method': method_name,
'params': params
}
request_string = json.dumps(payload)
logging.info('> {}'.format(request_string))
#print('> {}'.format(request_string))
try:
self.ws.send(request_string)
self.request_id += 1
except:
raise RPCError(request_string)
try:
reply = self.ws.recv()
except:
raise RPCError("ws.recv error when send req: " + request_string)
#print('< {}'.format(reply))
ret = {}
try:
ret = json.loads(reply, strict=False)
except ValueError:
raise ValueError("Client returned invalid format. Expected JSON!")
if 'error' in ret:
if 'detail' in ret['error']:
raise RPCError(ret['error']['detail'])
else:
raise RPCError(ret['error']['message'])
else:
return ret["result"]
def close(self):
self.ws.close()
import config
client = WebsocketClient(config.WEBSOCKET_URL)