Improve Python 2.7 compatibility ZTK-1.1
authorThierry Florac <tflorac@ulthar.net>
Wed, 04 Jul 2012 13:40:21 +0200
branchZTK-1.1
changeset 161 33b95dfd6142
parent 160 5fdc01c95cad
child 162 2537855e0f97
Improve Python 2.7 compatibility
src/ztfy/utils/protocol/xmlrpc.py
--- a/src/ztfy/utils/protocol/xmlrpc.py	Wed Jul 04 08:25:39 2012 +0200
+++ b/src/ztfy/utils/protocol/xmlrpc.py	Wed Jul 04 13:40:21 2012 +0200
@@ -61,6 +61,7 @@
         self.timeout = timeout
         if self._connection_required_compat():
             self.make_connection = self._make_connection_compat
+            self.get_response = self._get_response_compat
 
     def _connection_required_compat(self):
         # Compatibility code copied from EULExistDB (https://github.com/emory-libraries/eulexistdb)
@@ -102,19 +103,32 @@
             auth = 'Basic %s' % base64.encodestring("%s:%s" % self.credentials).strip()
             connection.putheader('Authorization', auth)
 
-    def request(self, host, handler, request_body, verbose=False):
+    # dummy request class for extracting cookies
+    class CookieRequest(urllib2.Request):
+        pass
 
-        # dummy request class for extracting cookies
-        class CookieRequest(urllib2.Request):
-            pass
+    # dummy response info headers helper
+    class CookieResponseHelper:
+        def __init__(self, response):
+            self.response = response
+        def getheaders(self, header):
+            return self.response.msg.getallmatchingheaders(header)
 
-        # dummy response class for extracting cookies
-        class CookieResponse:
-            def __init__(self, headers):
-                self.headers = headers
-            def info(self):
-                return self.headers
+    # dummy response class for extracting cookies
+    class CookieResponse:
+        def __init__(self, response):
+            self.response = response
+        def info(self):
+            return XMLRPCCookieAuthTransport.CookieResponseHelper(self.response)
 
+    # dummy compat response class for extracting cookies
+    class CompatCookieResponse:
+        def __init__(self, headers):
+            self.headers = headers
+        def info(self):
+            return self.headers
+
+    def request(self, host, handler, request_body, verbose=False):
         # issue XML-RPC request
         connection = self.make_connection(host)
         self.verbose = verbose
@@ -125,11 +139,25 @@
         self.send_user_agent(connection)
         self.send_content(connection, request_body)
         # get response
+        return self.get_response(connection, host, handler)
+
+    def get_response(self, connection, host, handler):
+        response = connection.getresponse()
+        # extract cookies from response headers
+        if self.cookies is not None:
+            crequest = XMLRPCCookieAuthTransport.CookieRequest('http://%s/' % host)
+            cresponse = XMLRPCCookieAuthTransport.CookieResponse(response)
+            self.cookies.extract_cookies(cresponse, crequest)
+        if response.status != 200:
+            raise xmlrpclib.ProtocolError(host + handler, response.status, response.reason, response.getheaders())
+        return self.parse_response(response)
+
+    def _get_response_compat(self, connection, host, handler):
         errcode, errmsg, headers = connection.getreply()
         # extract cookies from response headers
-        crequest = CookieRequest('http://%s/' % host)
-        cresponse = CookieResponse(headers)
         if self.cookies is not None:
+            crequest = XMLRPCCookieAuthTransport.CookieRequest('http://%s/' % host)
+            cresponse = XMLRPCCookieAuthTransport.CompatCookieResponse(headers)
             self.cookies.extract_cookies(cresponse, crequest)
         if errcode != 200:
             raise xmlrpclib.ProtocolError(host + handler, errcode, errmsg, headers)