diff options
author | Gavin Andresen <gavinandresen@gmail.com> | 2014-07-09 10:18:34 -0400 |
---|---|---|
committer | Gavin Andresen <gavinandresen@gmail.com> | 2014-07-09 10:18:34 -0400 |
commit | ae28a7c72db2776d7b2bf878b05c091e9ed11ace (patch) | |
tree | 078aa592ab107938681c663cf23a7e9afd4edc1f | |
parent | 52a7d52ffb84fb1c7bcdffd8c82e546832498a3b (diff) | |
parent | 1b2bc71de158ae272933e74450836f43a1fe55fc (diff) |
Merge pull request #4489 from gavinandresen/python_bitcoinrpc_update
Improved bitcoinrpc logging and Decimal support
-rw-r--r-- | qa/rpc-tests/python-bitcoinrpc/bitcoinrpc/authproxy.py | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/qa/rpc-tests/python-bitcoinrpc/bitcoinrpc/authproxy.py b/qa/rpc-tests/python-bitcoinrpc/bitcoinrpc/authproxy.py index c2e5406c2f..bc7d655fdf 100644 --- a/qa/rpc-tests/python-bitcoinrpc/bitcoinrpc/authproxy.py +++ b/qa/rpc-tests/python-bitcoinrpc/bitcoinrpc/authproxy.py @@ -39,8 +39,9 @@ try: except ImportError: import httplib import base64 -import json import decimal +import json +import logging try: import urllib.parse as urlparse except ImportError: @@ -50,6 +51,7 @@ USER_AGENT = "AuthServiceProxy/0.1" HTTP_TIMEOUT = 30 +log = logging.getLogger("BitcoinRPC") class JSONRPCException(Exception): def __init__(self, rpc_error): @@ -57,7 +59,14 @@ class JSONRPCException(Exception): self.error = rpc_error +def EncodeDecimal(o): + if isinstance(o, decimal.Decimal): + return round(o, 8) + raise TypeError(repr(o) + " is not JSON serializable") + class AuthServiceProxy(object): + __id_count = 0 + def __init__(self, service_url, service_name=None, timeout=HTTP_TIMEOUT, connection=None): self.__service_url = service_url self.__service_name = service_name @@ -66,7 +75,6 @@ class AuthServiceProxy(object): port = 80 else: port = self.__url.port - self.__id_count = 0 (user, passwd) = (self.__url.username, self.__url.password) try: user = user.encode('utf8') @@ -99,12 +107,14 @@ class AuthServiceProxy(object): return AuthServiceProxy(self.__service_url, name, connection=self.__conn) def __call__(self, *args): - self.__id_count += 1 + AuthServiceProxy.__id_count += 1 + log.debug("-%s-> %s %s"%(AuthServiceProxy.__id_count, self.__service_name, + json.dumps(args, default=EncodeDecimal))) postdata = json.dumps({'version': '1.1', 'method': self.__service_name, 'params': args, - 'id': self.__id_count}) + 'id': AuthServiceProxy.__id_count}, default=EncodeDecimal) self.__conn.request('POST', self.__url.path, postdata, {'Host': self.__url.hostname, 'User-Agent': USER_AGENT, @@ -121,7 +131,8 @@ class AuthServiceProxy(object): return response['result'] def _batch(self, rpc_call_list): - postdata = json.dumps(list(rpc_call_list)) + postdata = json.dumps(list(rpc_call_list), default=EncodeDecimal) + log.debug("--> "+postdata) self.__conn.request('POST', self.__url.path, postdata, {'Host': self.__url.hostname, 'User-Agent': USER_AGENT, @@ -136,5 +147,10 @@ class AuthServiceProxy(object): raise JSONRPCException({ 'code': -342, 'message': 'missing HTTP response from server'}) - return json.loads(http_response.read().decode('utf8'), - parse_float=decimal.Decimal) + responsedata = http_response.read().decode('utf8') + response = json.loads(responsedata, parse_float=decimal.Decimal) + if "error" in response and response["error"] is None: + log.debug("<-%s- %s"%(response["id"], json.dumps(response["result"], default=EncodeDecimal))) + else: + log.debug("<-- "+responsedata) + return response |