-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmm2rpclib_req.py
53 lines (46 loc) · 1.72 KB
/
mm2rpclib_req.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
import requests
from itertools import count
import ujson
DEFAULT_HTTP_TIMEOUT = 120
DEFAULT_RPC_PORT = 7783
class MMProxy:
_ids = count(0)
def __init__(self, conf_dict=None, timeout=DEFAULT_HTTP_TIMEOUT):
self.timeout = timeout
self.config = conf_dict
self.userpass = conf_dict.get('userpass')
if not conf_dict.get('rpcport'):
self.config['rpcport'] = DEFAULT_RPC_PORT
def __getattr__(self, req):
_id = next(self._ids)
upass = self.userpass
def call(**params):
if req != 'batch':
post_val = {
'jsonrpc': '2.0',
'userpass': upass,
'method': req,
'id': _id
}
for param, value in params.items():
post_val.update({param: value})
else:
post_val = []
for key in params:
post_dict = {
'jsonrpc': '2.0',
'userpass': upass,
'method': params.get(key).get('method'),
'id': _id
}
for param, value in params.get(key).items():
post_dict.update({param: value})
post_val.append(post_dict)
url = 'http://%s:%s' % (self.config['rpchost'], self.config['rpcport'])
post_data = ujson.dumps(post_val)
try:
resp = requests.post(url, data=post_data, timeout=self.timeout).json()
except ValueError:
resp = str(requests.post(url, data=post_data, timeout=self.timeout).content)
return resp
return call