forked from mshuaic/Blockchain_med
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Savoir.py
91 lines (83 loc) · 3 KB
/
Savoir.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Author: reiven
# github: https://github.com/DXMarkets/Savoir
import requests
import json
from base64 import b64encode
import logging
log = logging.getLogger('Savoir')
log.setLevel('WARNING')
class Savoir():
__id_count = 0
def __init__(self,
rpcuser,
rpcpasswd,
rpchost,
rpcport,
chainname,
rpc_call=None
):
self.__rpcuser = rpcuser
self.__rpcpasswd = rpcpasswd
self.__rpchost = rpchost
self.__rpcport = rpcport
self.__chainname = chainname
self.__auth_header = ' '.join(
['Basic', b64encode(
':'.join([rpcuser, rpcpasswd]).encode()).decode()]
)
self.__headers = {'Host': self.__rpchost,
'User-Agent': 'Savoir v0.1',
'Authorization': self.__auth_header,
'Content-type': 'application/json'
}
self.__rpc_call = rpc_call
def __getattr__(self, name):
if name.startswith('__') and name.endswith('__'):
# Python internal stuff
raise AttributeError
if self.__rpc_call is not None:
name = "%s.%s" % (self.__rpc_call, name)
return Savoir(self.__rpcuser,
self.__rpcpasswd,
self.__rpchost,
self.__rpcport,
self.__chainname,
name)
def __call__(self, *args):
Savoir.__id_count += 1
postdata = {'chain_name': self.__chainname,
'version': '1.1',
'jsonrpc': '2.0',
'params': args,
'method': self.__rpc_call,
'id': Savoir.__id_count}
url = ''.join(['http://', self.__rpchost, ':', self.__rpcport])
encoded = json.dumps(postdata)
log.info("Request: %s" % encoded)
r = requests.post(url, data=encoded, headers=self.__headers)
if r.status_code == 200:
log.info("Response: %s" % r.json())
return r.json()
else:
log.error("Error! Status code: %s" % r.status_code)
log.error("Text: %s" % r.text)
log.error("Json: %s" % r.json())
return r.json()
def batch(self, rpc_call, largs):
encoded = []
for args in largs:
# print(args)
# input()
Savoir.__id_count += 1
postdata = {'chain_name': self.__chainname,
'version': '1.1',
'jsonrpc': '2.0',
'params': args,
'method': rpc_call,
'id': Savoir.__id_count}
encoded.append(postdata)
url = ''.join(['http://', self.__rpchost, ':', self.__rpcport])
r = requests.post(url, data=json.dumps(
encoded), headers=self.__headers)
# print(r.json())
return r.json()