Updated XML-RPC call status codes ZTK-1.1
authorThierry Florac <tflorac@ulthar.net>
Mon, 03 Apr 2023 14:46:50 +0200
branchZTK-1.1
changeset 281 5f84a8151bf6
parent 280 82cc56661718
child 282 a5d9d79e6382
Updated XML-RPC call status codes
src/ztfy/utils/protocol/xmlrpc.py
--- a/src/ztfy/utils/protocol/xmlrpc.py	Wed Jun 01 18:47:23 2022 +0200
+++ b/src/ztfy/utils/protocol/xmlrpc.py	Mon Apr 03 14:46:50 2023 +0200
@@ -17,7 +17,6 @@
 ##############################################################################
 
 
-# import standard packages
 import base64
 import cookielib
 import httplib
@@ -25,14 +24,6 @@
 import urllib2
 import xmlrpclib
 
-# import Zope3 interfaces
-
-# import local interfaces
-
-# import Zope3 packages
-
-# import local packages
-
 
 class TimeoutHTTP(httplib.HTTP):
     def __init__(self, host='', port=None, strict=None, timeout=None):
@@ -40,6 +31,7 @@
             port = None
         self._setup(self._connection_class(host, port, strict, timeout))
 
+
 class TimeoutHTTPS(httplib.HTTPS):
     def __init__(self, host='', port=None, strict=None, timeout=None):
         if port == 0:
@@ -53,7 +45,8 @@
     _http_connection = httplib.HTTPConnection
     _http_connection_compat = TimeoutHTTP
 
-    def __init__(self, user_agent, credentials=(), cookies=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, headers=None):
+    def __init__(self, user_agent, credentials=(), cookies=None,
+                 timeout=socket._GLOBAL_DEFAULT_TIMEOUT, headers=None):
         xmlrpclib.Transport.__init__(self)
         self.user_agent = user_agent
         self.credentials = credentials
@@ -158,8 +151,9 @@
                 if cookie.name.startswith('Set-Cookie'):
                     cookie.name = cookie.name.split(': ', 1)[1]
                 self.cookies.set_cookie(cookie)
-        if response.status != 200:
-            raise xmlrpclib.ProtocolError(host + handler, response.status, response.reason, response.getheaders())
+        if response.status >= 300:
+            raise xmlrpclib.ProtocolError(host + handler, response.status,
+                                          response.reason, response.getheaders())
         return self.parse_response(response)
 
     def _get_response_compat(self, connection, host, handler):
@@ -169,7 +163,7 @@
             crequest = XMLRPCCookieAuthTransport.CookieRequest('http://%s/' % host)
             cresponse = XMLRPCCookieAuthTransport.CompatCookieResponse(headers)
             self.cookies.extract_cookies(cresponse, crequest)
-        if errcode != 200:
+        if errcode >= 300:
             raise xmlrpclib.ProtocolError(host + handler, errcode, errmsg, headers)
         try:
             sock = connection._conn.sock
@@ -185,24 +179,29 @@
     _http_connection_compat = TimeoutHTTPS
 
 
-def getClient(uri, credentials=(), verbose=False, allow_none=0, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, headers=None):
+def getClient(uri, credentials=(), verbose=False, allow_none=0,
+              timeout=socket._GLOBAL_DEFAULT_TIMEOUT, headers=None):
     """Get an XML-RPC client which supports basic authentication"""
     if uri.startswith('https:'):
-        transport = SecureXMLRPCCookieAuthTransport('Python XML-RPC Client/0.1 (ZTFY secure transport)', credentials, timeout=timeout, headers=headers)
+        transport = SecureXMLRPCCookieAuthTransport('Python XML-RPC Client/0.1 (ZTFY secure transport)',
+                                                    credentials, timeout=timeout, headers=headers)
     else:
-        transport = XMLRPCCookieAuthTransport('Python XML-RPC Client/0.1 (ZTFY basic transport)', credentials, timeout=timeout, headers=headers)
+        transport = XMLRPCCookieAuthTransport('Python XML-RPC Client/0.1 (ZTFY basic transport)',
+                                              credentials, timeout=timeout, headers=headers)
     return xmlrpclib.Server(uri, transport=transport, verbose=verbose, allow_none=allow_none)
 
 
-def getClientWithCookies(uri, credentials=(), verbose=False, allow_none=0, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
-                         headers=None, cookies=None):
+def getClientWithCookies(uri, credentials=(), verbose=False, allow_none=0,
+                         timeout=socket._GLOBAL_DEFAULT_TIMEOUT, headers=None, cookies=None):
     """Get an XML-RPC client which supports authentication through cookies"""
     if cookies is None:
         cookies = cookielib.CookieJar()
     if uri.startswith('https:'):
-        transport = SecureXMLRPCCookieAuthTransport('Python XML-RPC Client/0.1 (ZTFY secure cookie transport)',
+        transport = SecureXMLRPCCookieAuthTransport('Python XML-RPC Client/0.1 (ZTFY secure '
+                                                    'cookie transport)',
                                                     credentials, cookies, timeout, headers)
     else:
-        transport = XMLRPCCookieAuthTransport('Python XML-RPC Client/0.1 (ZTFY basic cookie transport)',
+        transport = XMLRPCCookieAuthTransport('Python XML-RPC Client/0.1 (ZTFY basic '
+                                              'cookie transport)',
                                               credentials, cookies, timeout, headers)
     return xmlrpclib.Server(uri, transport=transport, verbose=verbose, allow_none=allow_none)