Permalink
Browse files

Added unit tests, renamed to follow absolute importing guidelines, an…

…d made minor corrections introduced by unit tests.

git-svn-id: http://jsonrpclib.googlecode.com/svn/trunk@18 ae587032-bbab-11de-869a-473eb4776397
  • Loading branch information...
1 parent 9cb9396 commit 1cb797f048ae5131a01086a93d87952c03e8abe7 catchjosh committed Sep 4, 2010
Showing with 62 additions and 513 deletions.
  1. +42 −19 jsonrpclib/SimpleJSONRPCServer.py
  2. +6 −3 jsonrpclib/__init__.py
  3. +7 −2 jsonrpclib/config.py
  4. +7 −2 jsonrpclib/history.py
  5. +0 −487 jsonrpclib/jsonrpclib.py
@@ -8,20 +8,35 @@
import sys
def get_version(request):
- if type(request) not in (types.ListType, types.DictType):
- return None
- if type(request) is types.ListType:
- if len(request) == 0:
- return None
- if 'jsonrpc' not in request[0].keys():
- return None
- return '2.0'
# must be a dict
if 'jsonrpc' in request.keys():
return 2.0
if 'id' in request.keys():
return 1.0
return None
+
+def validate_request(request):
+ if type(request) is not types.DictType:
+ fault = Fault(
+ -32600, 'Request must be {}, not %s.' % type(request)
+ )
+ return fault
+ rpcid = request.get('id', None)
+ version = get_version(request)
+ if not version:
+ fault = Fault(-32600, 'Request %s invalid.' % request, rpcid=rpcid)
+ return fault
+ request.setdefault('params', [])
+ method = request.get('method', None)
+ params = request.get('params')
+ param_types = (types.ListType, types.DictType, types.TupleType)
+ if not method or type(method) not in types.StringTypes or \
+ type(params) not in param_types:
+ fault = Fault(
+ -32600, 'Invalid request parameters or method.', rpcid=rpcid
+ )
+ return fault
+ return True
class SimpleJSONRPCDispatcher(SimpleXMLRPCServer.SimpleXMLRPCDispatcher):
@@ -34,34 +49,42 @@ def _marshaled_dispatch(self, data, dispatch_method = None):
response = None
try:
request = jsonrpclib.loads(data)
- except:
- fault = Fault(-32600, 'Request %s invalid.' % data)
- response = fault.response()
- return response
- version = get_version(request)
- if not version:
- fault = Fault(-32600, 'Request %s invalid.' % data)
+ except Exception, e:
+ fault = Fault(-32700, 'Request %s invalid. (%s)' % (data, e))
response = fault.response()
return response
+ if not request:
+ fault = Fault(-32600, 'Request invalid -- no request data.')
+ return fault.response()
if type(request) is types.ListType:
# This SHOULD be a batch, by spec
responses = []
for req_entry in request:
+ result = validate_request(req_entry)
+ if type(result) is Fault:
+ responses.append(result.response())
+ continue
resp_entry = self._marshaled_single_dispatch(req_entry)
if resp_entry is not None:
responses.append(resp_entry)
- response = '[%s]' % ','.join(responses)
- else:
+ if len(responses) > 0:
+ response = '[%s]' % ','.join(responses)
+ else:
+ response = ''
+ else:
+ result = validate_request(request)
+ if type(result) is Fault:
+ return result.response()
response = self._marshaled_single_dispatch(request)
return response
def _marshaled_single_dispatch(self, request):
# TODO - Use the multiprocessing and skip the response if
# it is a notification
- method = request['method']
- params = request['params']
# Put in support for custom dispatcher here
# (See SimpleXMLRPCServer._marshaled_dispatch)
+ method = request.get('method')
+ params = request.get('params')
try:
response = self._dispatch(method, params)
except:
View
@@ -1,3 +1,6 @@
-from jsonrpclib import *
-from config import config
-from history import history
+from jsonrpclib.config import Config
+config = Config.instance()
+from jsonrpclib.history import History
+history = History.instance()
+from jsonrpclib.jsonrpc import Server, MultiCall, Fault
+from jsonrpclib.jsonrpc import ProtocolError, loads, dumps
View
@@ -29,5 +29,10 @@ class Config(object):
user_agent = 'jsonrpclib/0.1 (Python %s)' % \
'.'.join([str(ver) for ver in sys.version_info[0:3]])
# User agent to use for calls.
-
-config = Config
+ _instance = None
+
+ @classmethod
+ def instance(cls):
+ if not cls._instance:
+ cls._instance = cls()
+ return cls._instance
View
@@ -8,6 +8,13 @@ class History(object):
"""
requests = []
responses = []
+ _instance = None
+
+ @classmethod
+ def instance(cls):
+ if not cls._instance:
+ cls._instance = cls()
+ return cls._instance
def add_response(self, response_obj):
self.responses.append(response_obj)
@@ -32,5 +39,3 @@ def response(self):
def clear(self):
del self.requests[:]
del self.responses[:]
-
-history = History()
Oops, something went wrong.

0 comments on commit 1cb797f

Please sign in to comment.