39
39
except ImportError :
40
40
import httplib
41
41
import base64
42
- import json
43
42
import decimal
43
+ import json
44
+ import logging
44
45
try :
45
46
import urllib .parse as urlparse
46
47
except ImportError :
50
51
51
52
HTTP_TIMEOUT = 30
52
53
54
+ log = logging .getLogger ("BitcoinRPC" )
53
55
54
56
class JSONRPCException (Exception ):
55
57
def __init__ (self , rpc_error ):
56
58
Exception .__init__ (self )
57
59
self .error = rpc_error
58
60
59
61
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
+
60
67
class AuthServiceProxy (object ):
68
+ __id_count = 0
69
+
61
70
def __init__ (self , service_url , service_name = None , timeout = HTTP_TIMEOUT , connection = None ):
62
71
self .__service_url = service_url
63
72
self .__service_name = service_name
@@ -66,7 +75,6 @@ def __init__(self, service_url, service_name=None, timeout=HTTP_TIMEOUT, connect
66
75
port = 80
67
76
else :
68
77
port = self .__url .port
69
- self .__id_count = 0
70
78
(user , passwd ) = (self .__url .username , self .__url .password )
71
79
try :
72
80
user = user .encode ('utf8' )
@@ -99,12 +107,14 @@ def __getattr__(self, name):
99
107
return AuthServiceProxy (self .__service_url , name , connection = self .__conn )
100
108
101
109
def __call__ (self , * args ):
102
- self .__id_count += 1
110
+ AuthServiceProxy .__id_count += 1
103
111
112
+ log .debug ("-%s-> %s %s" % (AuthServiceProxy .__id_count , self .__service_name ,
113
+ json .dumps (args , default = EncodeDecimal )))
104
114
postdata = json .dumps ({'version' : '1.1' ,
105
115
'method' : self .__service_name ,
106
116
'params' : args ,
107
- 'id' : self .__id_count })
117
+ 'id' : AuthServiceProxy .__id_count }, default = EncodeDecimal )
108
118
self .__conn .request ('POST' , self .__url .path , postdata ,
109
119
{'Host' : self .__url .hostname ,
110
120
'User-Agent' : USER_AGENT ,
@@ -121,7 +131,8 @@ def __call__(self, *args):
121
131
return response ['result' ]
122
132
123
133
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 )
125
136
self .__conn .request ('POST' , self .__url .path , postdata ,
126
137
{'Host' : self .__url .hostname ,
127
138
'User-Agent' : USER_AGENT ,
@@ -136,5 +147,10 @@ def _get_response(self):
136
147
raise JSONRPCException ({
137
148
'code' : - 342 , 'message' : 'missing HTTP response from server' })
138
149
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