Commits

Olemis Lang  committed ce14948

BH RPC : Patch jsonrpclib for enhanced compat with xmlrpclib

  • Participants
  • Parent commits 9435b3d

Comments (0)

Files changed (3)

File trunk/tracrpc/tests/__init__.py

 
 import jsonrpclib.jsonrpc
 
+# Patch jsonrpclib
+import tracrpc.tests._jsonrpclib
+
 if sys.version_info[:2] < (2, 7):
     import unittest2 as unittest
 else:

File trunk/tracrpc/tests/_jsonrpclib.py

+# -*- coding: utf-8 -*-
+"""
+License: BSD
+
+(c) 2013      ::: Olemis Lang (olemis+trac@gmail.com)
+"""
+
+import types
+import xmlrpclib
+import jsonrpclib.jsonrpc
+
+#--------------------------------------------------
+#   jsonrpclib compatibility upgrade
+#--------------------------------------------------
+
+def isbatch(result):
+    if type(result) not in (types.ListType, types.TupleType):
+        return False
+    if len(result) < 1:
+        return False
+    if type(result[0]) is not types.DictType:
+        return False
+    if 'jsonrpc' not in result[0].keys():
+        return False
+    try:
+        version = float(result[0]['jsonrpc'])
+    except ValueError:
+        raise xmlrpclib.ResponseError("'jsonrpc' key must be a float(able) value.")
+    if version < 2:
+        return False
+    return True
+
+def check_for_errors(result):
+    if not result:
+        # Notification
+        return result
+    if type(result) is not types.DictType:
+        raise xmlrpclib.ResponseError('Response is not a dict.')
+    if 'jsonrpc' in result.keys() and float(result['jsonrpc']) > 2.0:
+        raise xmlrpclib.ResponseError('JSON-RPC version not yet supported.')
+    if 'result' not in result.keys() and 'error' not in result.keys():
+        raise xmlrpclib.ResponseError('Response does not have a result or error key.')
+    if 'error' in result.keys() and result['error'] != None:
+        code = result['error']['code']
+        message = result['error']['message']
+        raise xmlrpclib.Fault(code, message)
+    return result
+
+jsonrpclib.jsonrpc.check_for_errors = check_for_errors
+jsonrpclib.jsonrpc.isbatch = isbatch

File trunk/tracrpc/tests/xml_rpc.py

 class RpcXmlTestCase(TracRpcTestCase):
     
     def setUp(self):
+        TracRpcTestCase.setUp(self)
         if not getattr(self._tester, 'xmlrpclib_compat', False):
             raise unittest.SkipTest('Protocol not compatible with xmlrpclib')
-        TracRpcTestCase.setUp(self)
         self.anon = self._tester.server_proxy(self.url_anon)
         self.user = self._tester.server_proxy(self.url_auth, user='user')
         self.admin = self._tester.server_proxy(self.url_auth, user='admin')