[Erp5-report] r14424 - /erp5/trunk/utils/oood/oood_common.py

nobody at svn.erp5.org nobody at svn.erp5.org
Tue May 8 13:06:57 CEST 2007


Author: bartek
Date: Tue May  8 13:06:56 2007
New Revision: 14424

URL: http://svn.erp5.org?rev=14424&view=rev
Log:
more API for Response - methods for checking what sort of status it is.

Modified:
    erp5/trunk/utils/oood/oood_common.py

Modified: erp5/trunk/utils/oood/oood_common.py
URL: http://svn.erp5.org/erp5/trunk/utils/oood/oood_common.py?rev=14424&r1=14423&r2=14424&view=diff
==============================================================================
--- erp5/trunk/utils/oood/oood_common.py (original)
+++ erp5/trunk/utils/oood/oood_common.py Tue May  8 13:06:56 2007
@@ -23,6 +23,7 @@
 
 
 class MalformedRequest(Exception): pass
+class InvalidStatusCode(Exception): pass
 
 malformed_request_message = 'oood accepts either 1-4 arguments: filename [, data [, meta [, extension]] or only one argument - a dictionary'
 
@@ -60,6 +61,8 @@
     Response data is a dictionary, which either has multiple keys (filename, data encoded in base64 etc),
     or only one key 'response_data'
   """
+
+  code = 0
 
   def __init__(self, code, data={}, message='', sending=True):
     self.code = code
@@ -86,6 +89,47 @@
     """
     return (self.code, self.data, self.message)
 
+  def isCodeValid(func):
+    """
+      Decorator for checking if response code of self is valid.
+      If it is not, an InvalidStatusCode exception is raised.
+    """
+    def wrappedFunc(self, *args, **kwargs):
+      if self.code not in code_map.keys():
+        raise InvalidStatusCode(str(self.code))
+      return func(self, *args, **kwargs)
+    return wrappedFunc
+
+  @isCodeValid
+  def isOk(self):
+    """
+      Check if the response is "OK".
+    """
+    return self.code == 200
+
+  @isCodeValid
+  def isError(self):
+    """
+      Check if the response signals an error.
+    """
+    return self.code != 200
+
+  @isCodeValid
+  def isClientError(self):
+    """
+      Check if the error was caused by client, or was it an
+      internal problem of the server.
+    """
+    return self.code > 400 and self.code < 500
+
+  @isCodeValid
+  def isServerError(self):
+    """
+      Check if the error was caused by client, or was it an
+      internal problem of the server.
+    """
+    return self.code > 500
+
 def responseFactory(response):
   """
     To be used by clients.




More information about the Erp5-report mailing list