Module centralnicreseller.apiconnector.responsetemplate
centralnicreseller.apiconnector.responsetemplate ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This module covers all basic functionality to deal with Backend API responses. :copyright: © 2024 Team Internet Group PLC. :license: MIT, see LICENSE for more details.
Classes
class ResponseTemplate (response='')
-
Expand source code
class ResponseTemplate(object): """ The ResponseTemplate class is the base class for the Response Class that covers basic functionality to work with Backend API responses. """ def __init__(self, response=""): #: Holds the response as plain text / string self._raw = response if (response == "") or (response is None): descr = "Empty API response. Probably unreachable API end point {CONNECTION_URL}" self._raw = "[RESPONSE]\r\nCODE=423\r\nDESCRIPTION=%s\r\nEOF\r\n" % ( descr) # try/except to support old versions of python (python2.5) try: if isinstance(self._raw, bytes): self._raw = self._raw.decode("utf-8") except UnicodeError: self._raw = self._raw.decode("latin1") except BaseException: self._raw = self._raw.decode("utf-8") if isinstance(response, dict): raise TypeError( 'Type "dict" is not allowed for parameter "response". Use type "string" instead.' ) else: #: Holds the response as hash self.__hash = RP.parse(self._raw) if ("CODE" not in self.__hash) or ("DESCRIPTION" not in self.__hash): self._raw = "[RESPONSE]\r\nCODE=423\r\nDESCRIPTION=Invalid API response. Contact Support\r\nEOF\r\n" self.__hash = RP.parse(self._raw) def getCode(self): """ Returns the API response code as integer """ return int(self.__hash["CODE"]) def getDescription(self): """ Returns the API response description """ return self.__hash["DESCRIPTION"] def getPlain(self): """ Returns the plain API response """ return self._raw def getQueuetime(self): """ Get Queuetime of API response as float value """ if "QUEUETIME" in self.__hash: return float(self.__hash["QUEUETIME"]) return 0.00 def getHash(self): """ Get API response as Hash """ return self.__hash def getRuntime(self): """ Get Runtime of API response as float value """ if "RUNTIME" in self.__hash: return float(self.__hash["RUNTIME"]) return 0.00 def isError(self): """ Check if current API response represents an error case (5xx) """ return self.__hash["CODE"][0] == "5" def isSuccess(self): """ Check if current API response represents a success case (2xx) """ return self.__hash["CODE"][0] == "2" def isTmpError(self): """ Check if current API response represents a temporary error case (4xx) """ return self.__hash["CODE"][0] == "4"
The ResponseTemplate class is the base class for the Response Class that covers basic functionality to work with Backend API responses.
Subclasses
Methods
def getCode(self)
-
Expand source code
def getCode(self): """ Returns the API response code as integer """ return int(self.__hash["CODE"])
Returns the API response code as integer
def getDescription(self)
-
Expand source code
def getDescription(self): """ Returns the API response description """ return self.__hash["DESCRIPTION"]
Returns the API response description
def getHash(self)
-
Expand source code
def getHash(self): """ Get API response as Hash """ return self.__hash
Get API response as Hash
def getPlain(self)
-
Expand source code
def getPlain(self): """ Returns the plain API response """ return self._raw
Returns the plain API response
def getQueuetime(self)
-
Expand source code
def getQueuetime(self): """ Get Queuetime of API response as float value """ if "QUEUETIME" in self.__hash: return float(self.__hash["QUEUETIME"]) return 0.00
Get Queuetime of API response as float value
def getRuntime(self)
-
Expand source code
def getRuntime(self): """ Get Runtime of API response as float value """ if "RUNTIME" in self.__hash: return float(self.__hash["RUNTIME"]) return 0.00
Get Runtime of API response as float value
def isError(self)
-
Expand source code
def isError(self): """ Check if current API response represents an error case (5xx) """ return self.__hash["CODE"][0] == "5"
Check if current API response represents an error case (5xx)
def isSuccess(self)
-
Expand source code
def isSuccess(self): """ Check if current API response represents a success case (2xx) """ return self.__hash["CODE"][0] == "2"
Check if current API response represents a success case (2xx)
def isTmpError(self)
-
Expand source code
def isTmpError(self): """ Check if current API response represents a temporary error case (4xx) """ return self.__hash["CODE"][0] == "4"
Check if current API response represents a temporary error case (4xx)