Improved logging and Decimal support
This commit is contained in:
parent
418d4ad110
commit
1b2bc71de1
1 changed files with 23 additions and 7 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue