Skip to content

Commit 1b2bc71

Browse files
committed
Improved logging and Decimal support
1 parent 418d4ad commit 1b2bc71

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

qa/rpc-tests/python-bitcoinrpc/bitcoinrpc/authproxy.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@
3939
except ImportError:
4040
import httplib
4141
import base64
42-
import json
4342
import decimal
43+
import json
44+
import logging
4445
try:
4546
import urllib.parse as urlparse
4647
except ImportError:
@@ -50,14 +51,22 @@
5051

5152
HTTP_TIMEOUT = 30
5253

54+
log = logging.getLogger("BitcoinRPC")
5355

5456
class JSONRPCException(Exception):
5557
def __init__(self, rpc_error):
5658
Exception.__init__(self)
5759
self.error = rpc_error
5860

5961

62+
def EncodeDecimal(o):
63+
if isinstance(o, decimal.Decimal):
64+
return round(o, 8)
65+
raise TypeError(repr(o) + " is not JSON serializable")
66+
6067
class AuthServiceProxy(object):
68+
__id_count = 0
69+
6170
def __init__(self, service_url, service_name=None, timeout=HTTP_TIMEOUT, connection=None):
6271
self.__service_url = service_url
6372
self.__service_name = service_name
@@ -66,7 +75,6 @@ def __init__(self, service_url, service_name=None, timeout=HTTP_TIMEOUT, connect
6675
port = 80
6776
else:
6877
port = self.__url.port
69-
self.__id_count = 0
7078
(user, passwd) = (self.__url.username, self.__url.password)
7179
try:
7280
user = user.encode('utf8')
@@ -99,12 +107,14 @@ def __getattr__(self, name):
99107
return AuthServiceProxy(self.__service_url, name, connection=self.__conn)
100108

101109
def __call__(self, *args):
102-
self.__id_count += 1
110+
AuthServiceProxy.__id_count += 1
103111

112+
log.debug("-%s-> %s %s"%(AuthServiceProxy.__id_count, self.__service_name,
113+
json.dumps(args, default=EncodeDecimal)))
104114
postdata = json.dumps({'version': '1.1',
105115
'method': self.__service_name,
106116
'params': args,
107-
'id': self.__id_count})
117+
'id': AuthServiceProxy.__id_count}, default=EncodeDecimal)
108118
self.__conn.request('POST', self.__url.path, postdata,
109119
{'Host': self.__url.hostname,
110120
'User-Agent': USER_AGENT,
@@ -121,7 +131,8 @@ def __call__(self, *args):
121131
return response['result']
122132

123133
def _batch(self, rpc_call_list):
124-
postdata = json.dumps(list(rpc_call_list))
134+
postdata = json.dumps(list(rpc_call_list), default=EncodeDecimal)
135+
log.debug("--> "+postdata)
125136
self.__conn.request('POST', self.__url.path, postdata,
126137
{'Host': self.__url.hostname,
127138
'User-Agent': USER_AGENT,
@@ -136,5 +147,10 @@ def _get_response(self):
136147
raise JSONRPCException({
137148
'code': -342, 'message': 'missing HTTP response from server'})
138149

139-
return json.loads(http_response.read().decode('utf8'),
140-
parse_float=decimal.Decimal)
150+
responsedata = http_response.read().decode('utf8')
151+
response = json.loads(responsedata, parse_float=decimal.Decimal)
152+
if "error" in response and response["error"] is None:
153+
log.debug("<-%s- %s"%(response["id"], json.dumps(response["result"], default=EncodeDecimal)))
154+
else:
155+
log.debug("<-- "+responsedata)
156+
return response

0 commit comments

Comments
 (0)