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 if response_body == '':
112 return_obj = loads(response_body)
115 class SafeTransport(XMLSafeTransport):
116 """ Just extends for HTTPS calls """
117 user_agent = Transport.user_agent
118 send_content = Transport.send_content
119 _parse_response = Transport._parse_response
121 class ServerProxy(XMLServerProxy):
123 Unfortunately, much more of this class has to be copied since
124 so much of it does the serialization.
127 def __init__(self, uri, transport=None, encoding=None,
128 verbose=0, version=None):
133 self.__version = version
134 schema, uri = urllib.splittype(uri)
135 if schema not in ('http', 'https'):
136 raise IOError('Unsupported JSON-RPC protocol.')
137 self.__host, self.__handler = urllib.splithost(uri)
138 if not self.__handler:
139 # Not sure if this is in the JSON spec?
140 self.__handler = '/RPC2'
141 if transport is None:
142 if schema == 'https':
143 transport = SafeTransport()
145 transport = Transport()
146 self.__transport = transport
147 self.__encoding = encoding
148 self.__verbose = verbose
150 def __request(self, methodname, params, rpcid=None):
151 request = dumps(params, methodname, encoding=self.__encoding,
152 rpcid=rpcid, version=self.__version)
153 response = self.__run_request(request)
154 return response['result']
156 def __notify(self, methodname, params, rpcid=None):
157 request = dumps(params, methodname, encoding=self.__encoding,
158 rpcid=rpcid, version=self.__version, notify=True)
159 response = self.__run_request(request, notify=True)
162 def __run_request(self, request, notify=None):
164 global _last_response
165 _last_request = request
167 response = self.__transport.request(
171 verbose=self.__verbose
174 # Here, the XMLRPC library translates a single list
175 # response to the single value -- should we do the
176 # same, and require a tuple / list to be passed to
177 # the response object, or expect the Server to be
178 # outputting the response appropriately?
180 _last_response = response
182 # notification, no result
184 return check_for_errors(response)
186 def __getattr__(self, name):
187 # Same as original, just with new _Method and wrapper
189 if name in ('__notify', '__run_request'):
190 wrapped_name = '_%s%s' % (self.__class__.__name__, name)
191 return getattr(self, wrapped_name)
192 return _Method(self.__request, name)
194 class _Method(XML_Method):
195 def __call__(self, *args, **kwargs):
196 if len(args) > 0 and len(kwargs) > 0:
197 raise ProtocolError('Cannot use both positional ' +
198 'and keyword arguments (according to JSON-RPC spec.)')
200 return self.__send(self.__name, args)
202 return self.__send(self.__name, kwargs)
204 # Batch implementation
206 class MultiCallMethod(object):
208 def __init__(self, method, notify=False):
213 def __call__(self, *args, **kwargs):
214 if len(kwargs) > 0 and len(args) > 0:
215 raise ProtocolError('A Job cannot have both positional ' +
216 'and keyword arguments.')
222 def request(self, encoding=None, rpcid=None):
223 return dumps(self.params, self.method, version=2.0,
224 encoding=encoding, rpcid=rpcid, notify=self.notify)
227 return '%s' % self.request()
229 class MultiCall(object):
231 def __init__(self, server):
232 self.__server = server
235 def __run_request(self, request_body):
236 run_request = getattr(self.__server, '_ServerProxy__run_request')
237 return run_request(request_body)
240 if len(self.__job_list) < 1:
241 # Should we alert? This /is/ pretty obvious.
243 request_body = '[ %s ]' % ','.join([job.request() for
244 job in self.__job_list])
245 responses = self.__run_request(request_body)
246 del self.__job_list[:]
247 return [ response['result'] for response in responses ]
249 def __notify(self, method, params=[]):
250 new_job = MultiCallMethod(method, notify=True)
251 new_job.params = params
252 self.__job_list.append(new_job)
254 def __getattr__(self, name):
255 if name in ('__run', '__notify'):
256 wrapped_name = '_%s%s' % (self.__class__.__name__, name)
257 return getattr(self, wrapped_name)
258 new_job = MultiCallMethod(name)
259 self.__job_list.append(new_job)
264 # These lines conform to xmlrpclib's "compatibility" line.
265 # Not really sure if we should include these, but oh well.
269 # JSON-RPC error class
270 def __init__(self, code=-32000, message='Server error'):
271 self.faultCode = code
272 self.faultString = message
275 return {'code':self.faultCode, 'message':self.faultString}
277 def response(self, rpcid=None, version=None):
281 return dumps(self, rpcid=rpcid, version=version)
284 return '<Fault %s: %s>' % (self.faultCode, self.faultString)
286 def random_id(length=8):
290 choices = string.lowercase+string.digits
292 for i in range(length):
293 return_id += random.choice(choices)
297 def __init__(self, rpcid=None, version=None):
302 self.version = float(version)
304 def request(self, method, params=[]):
305 if type(method) not in types.StringTypes:
306 raise ValueError('Method name must be a string.')
308 self.id = random_id()
309 request = {'id':self.id, 'method':method, 'params':params}
310 if self.version >= 2:
311 request['jsonrpc'] = str(self.version)
314 def notify(self, method, params=[]):
315 request = self.request(method, params)
316 if self.version >= 2:
322 def response(self, result=None):
323 response = {'result':result, 'id':self.id}
324 if self.version >= 2:
325 response['jsonrpc'] = str(self.version)
327 response['error'] = None
330 def error(self, code=-32000, message='Server error.'):
331 error = self.response()
332 if self.version >= 2:
335 error['result'] = None
336 error['error'] = {'code':code, 'message':message}
339 def dumps(params=[], methodname=None, methodresponse=None,
340 encoding=None, rpcid=None, version=None, notify=None):
342 This differs from the Python implementation in that it implements
343 the rpcid argument since the 2.0 spec requires it for responses.
348 valid_params = (types.TupleType, types.ListType, types.DictType)
349 if methodname in types.StringTypes and \
350 type(params) not in valid_params and \
351 not isinstance(params, Fault):
353 If a method, and params are not in a listish or a Fault,
356 raise TypeError('Params must be a dict, list, tuple or Fault ' +
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 type(methodname) not in types.StringTypes and methodresponse != True:
366 raise ValueError('Method name must be a string, or methodresponse '+
367 'must be set to True.')
368 if methodresponse is True:
370 raise ValueError('A method response must have an rpcid.')
371 response = payload.response(params)
372 return jdumps(response, encoding=encoding)
375 request = payload.notify(methodname, params)
377 request = payload.request(methodname, params)
378 return jdumps(request, encoding=encoding)
382 This differs from the Python implementation, in that it returns
383 the request structure in Dict format instead of the method, params.
384 It will return a list in the case of a batch request / response.
389 result = jloads(data)
390 # if the above raises an error, the implementing server code
391 # should return something like the following:
392 # { 'jsonrpc':'2.0', 'error': fault.error(), id: None }
395 def check_for_errors(result):
397 if not isbatch(result):
398 result_list.append(result)
401 for entry in result_list:
402 if 'jsonrpc' in entry.keys() and float(entry['jsonrpc']) > 2.0:
403 raise NotImplementedError('JSON-RPC version not yet supported.')
404 if 'error' in entry.keys() and entry['error'] != None:
405 code = entry['error']['code']
406 message = entry['error']['message']
407 raise ProtocolError('ERROR %s: %s' % (code, message))
412 if type(result) not in (types.ListType, types.TupleType):
416 if type(result[0]) is not types.DictType:
418 if 'jsonrpc' not in result[0].keys():
421 version = float(result[0]['jsonrpc'])
423 raise ProtocolError('"jsonrpc" key must be a float(able) value.')