2 JSONRPCLIB -- started by Josh Marshall
4 This library is a JSON-RPC v.2 (proposed) implementation which
5 follows the xmlrpclib API for portability between clients. It
6 uses the same Server / ServerProxy, loads, dumps, etc. syntax,
7 while providing features not present in XML-RPC like:
12 * Batches and batch notifications
14 Eventually, I'll add a SimpleXMLRPCServer compatible library,
15 and other things to tie the thing off nicely. :)
17 For a quick-start, just open a console and type the following,
18 replacing the server address, method, and parameters
21 >>> server = jsonrpclib.Server('http://localhost:8181')
24 >>> jsonrpclib.__notify('add', (5, 6))
26 See http://code.google.com/p/jsonrpclib/ for more info.
31 from xmlrpclib import Transport as XMLTransport
32 from xmlrpclib import SafeTransport as XMLSafeTransport
33 from xmlrpclib import ServerProxy as XMLServerProxy
34 from xmlrpclib import _Method as XML_Method
37 # JSON library importing
49 if not cjson and not json:
51 import simplejson as json
53 raise ImportError('You must have the cjson, json, or simplejson ' +
54 'module(s) available.')
60 _user_agent = 'jsonrpclib/0.1 (Python %s)' % \
61 '.'.join([str(ver) for ver in sys.version_info[0:3]])
65 def jdumps(obj, encoding='utf-8'):
66 # Do 'serialize' test at some point for other classes
69 return cjson.encode(obj)
71 return json.dumps(obj, encoding=encoding)
73 def jloads(json_string):
76 return cjson.decode(json_string)
78 return json.loads(json_string)
81 # XMLRPClib re-implemntations
83 class ProtocolError(Exception):
86 class Transport(XMLTransport):
87 """ Just extends the XMLRPC transport where necessary. """
88 user_agent = _user_agent
90 def send_content(self, connection, request_body):
91 connection.putheader("Content-Type", "text/json")
92 connection.putheader("Content-Length", str(len(request_body)))
93 connection.endheaders()
95 connection.send(request_body)
97 def _parse_response(self, file_h, sock):
101 response = sock.recv(1024)
103 response = file_h.read(1024)
107 print 'body: %s' % response
108 response_body += response
109 return_obj = loads(response_body)
112 class SafeTransport(XMLSafeTransport):
113 """ Just extends for HTTPS calls """
114 user_agent = Transport.user_agent
115 send_content = Transport.send_content
116 _parse_response = Transport._parse_response
118 class ServerProxy(XMLServerProxy):
120 Unfortunately, much more of this class has to be copied since
121 so much of it does the serialization.
124 def __init__(self, uri, transport=None, encoding=None,
125 verbose=0, version=None):
130 self.__version = version
131 schema, uri = urllib.splittype(uri)
132 if schema not in ('http', 'https'):
133 raise IOError('Unsupported JSON-RPC protocol.')
134 self.__host, self.__handler = urllib.splithost(uri)
135 if not self.__handler:
136 # Not sure if this is in the JSON spec?
137 self.__handler = '/RPC2'
138 if transport is None:
139 if schema == 'https':
140 transport = SafeTransport()
142 transport = Transport()
143 self.__transport = transport
144 self.__encoding = encoding
145 self.__verbose = verbose
147 def __request(self, methodname, params, rpcid=None):
148 request = dumps(params, methodname, encoding=self.__encoding,
149 rpcid=rpcid, version=self.__version)
150 response = self.__run_request(request)
151 return response['result']
153 def __notify(self, methodname, params, rpcid=None):
154 request = dumps(params, methodname, encoding=self.__encoding,
155 rpcid=rpcid, version=self.__version, notify=True)
156 response = self.__run_request(request, notify=True)
159 def __run_request(self, request, notify=None):
161 global _last_response
162 _last_request = request
165 _last_response = None
168 response = self.__transport.request(
172 verbose=self.__verbose
175 # Here, the XMLRPC library translates a single list
176 # response to the single value -- should we do the
177 # same, and require a tuple / list to be passed to
178 # the response object, or expect the Server to be
179 # outputting the response appropriately?
181 _last_response = response
182 return check_for_errors(response)
184 def __getattr__(self, name):
185 # Same as original, just with new _Method and wrapper
187 if name in ('__notify', '__run_request'):
188 wrapped_name = '_%s%s' % (self.__class__.__name__, name)
189 return getattr(self, wrapped_name)
190 return _Method(self.__request, name)
192 class _Method(XML_Method):
193 def __call__(self, *args, **kwargs):
194 if len(args) > 0 and len(kwargs) > 0:
195 raise ProtocolError('Cannot use both positional ' +
196 'and keyword arguments (according to JSON-RPC spec.)')
198 return self.__send(self.__name, args)
200 return self.__send(self.__name, kwargs)
202 # Batch implementation
206 def __init__(self, method, notify=False):
211 def __call__(self, *args, **kwargs):
212 if len(kwargs) > 0 and len(args) > 0:
213 raise ProtocolError('A Job cannot have both positional ' +
214 'and keyword arguments.')
220 def request(self, encoding=None, rpcid=None):
221 return dumps(self.params, self.method, version=2.0,
222 encoding=encoding, rpcid=rpcid, notify=self.notify)
225 return '%s' % self.request()
227 class MultiCall(ServerProxy):
229 def __init__(self, uri, *args, **kwargs):
231 ServerProxy.__init__(self, uri, *args, **kwargs)
233 def __run_request(self, request_body):
234 run_request = getattr(ServerProxy, '_ServerProxy__run_request')
235 return run_request(self, request_body)
238 if len(self.__job_list) < 1:
239 # Should we alert? This /is/ pretty obvious.
241 request_body = '[ %s ]' % ','.join([job.request() for
242 job in self.__job_list])
243 responses = self.__run_request(request_body)
244 del self.__job_list[:]
245 return [ response['result'] for response in responses ]
247 def __notify(self, method, params):
248 new_job = Job(method, notify=True)
249 self.__job_list.append(new_job)
251 def __getattr__(self, name):
252 if name in ('__run', '__notify'):
253 wrapped_name = '_%s%s' % (self.__class__.__name__, name)
254 return getattr(self, wrapped_name)
256 self.__job_list.append(new_job)
261 # These lines conform to xmlrpclib's "compatibility" line.
262 # Not really sure if we should include these, but oh well.
266 # JSON-RPC error class
267 def __init__(self, code=-32000, message='Server error'):
268 self.faultCode = code
269 self.faultString = message
272 return {'code':self.faultCode, 'message':self.faultString}
274 def response(self, rpcid=None, version=None):
278 return dumps(self, rpcid=None, methodresponse=True,
281 def random_id(length=8):
285 choices = string.lowercase+string.digits
287 for i in range(length):
288 return_id += random.choice(choices)
292 def __init__(self, rpcid=None, version=None):
297 self.version = float(version)
299 def request(self, method, params=[]):
300 if type(method) not in types.StringTypes:
301 raise ValueError('Method name must be a string.')
303 self.id = random_id()
304 request = {'id':self.id, 'method':method, 'params':params}
305 if self.version >= 2:
306 request['jsonrpc'] = str(self.version)
309 def notify(self, method, params=[]):
310 request = self.request(method, params)
311 if self.version >= 2:
317 def response(self, result=None):
318 response = {'result':result, 'id':self.id}
319 if self.version >= 2:
320 response['jsonrpc'] = str(self.version)
322 response['error'] = None
325 def error(self, code=-32000, message='Server error.'):
326 error = self.response()
327 if self.version >= 2:
330 error['result'] = None
331 error['error'] = {'code':code, 'message':message}
334 def dumps(params=[], methodname=None, methodresponse=None,
335 encoding=None, rpcid=None, version=None, notify=None):
337 This differs from the Python implementation in that it implements
338 the rpcid argument since the 2.0 spec requires it for responses.
343 valid_params = (types.TupleType, types.ListType, types.DictType)
344 if methodname in types.StringTypes and \
345 type(params) not in valid_params and \
346 not isinstance(params, Fault):
348 If a method, and params are not in a listish or a Fault,
351 raise TypeError('Params must be a dict, list, tuple or Fault ' +
353 if type(methodname) not in types.StringTypes and methodresponse != True:
354 raise ValueError('Method name must be a string, or methodresponse '+
355 'must be set to True.')
356 if isinstance(params, Fault) and not methodresponse:
357 raise TypeError('You can only use a Fault for responses.')
358 # Begin parsing object
359 payload = Payload(rpcid=rpcid, version=version)
362 if type(params) is Fault:
363 response = payload.error(params.faultCode, params.faultString)
364 return jdumps(response, encoding=encoding)
365 if methodresponse is True:
367 raise ValueError('A method response must have an rpcid.')
368 response = payload.response(params)
369 return jdumps(response, encoding=encoding)
372 request = payload.notify(methodname, params)
374 request = payload.request(methodname, params)
375 return jdumps(request, encoding=encoding)
379 This differs from the Python implementation, in that it returns
380 the request structure in Dict format instead of the method, params.
381 It will return a list in the case of a batch request / response.
383 result = jloads(data)
384 # if the above raises an error, the implementing server code
385 # should return something like the following:
386 # { 'jsonrpc':'2.0', 'error': fault.error(), id: None }
389 def check_for_errors(result):
391 if not isbatch(result):
392 result_list.append(result)
395 for entry in result_list:
396 if 'jsonrpc' in entry.keys() and float(entry['jsonrpc']) > 2.0:
397 raise NotImplementedError('JSON-RPC version not yet supported.')
398 if 'error' in entry.keys() and entry['error'] != None:
399 code = entry['error']['code']
400 message = entry['error']['message']
401 raise ProtocolError('ERROR %s: %s' % (code, message))
406 if type(result) not in (types.ListType, types.TupleType):
410 if type(result[0]) is not types.DictType:
412 if 'jsonrpc' not in result[0].keys():
415 version = float(result[0]['jsonrpc'])
417 raise ProtocolError('"jsonrpc" key must be a float(able) value.')