Module centralnicreseller.apiconnector.responsetemplatemanager
centralnicreseller.apiconnector.responsetemplatemanager ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This module covers all functionality to manage response templates. :copyright: © 2024 Team Internet Group PLC. :license: MIT, see LICENSE for more details.
Classes
class ResponseTemplateManager
-
Expand source code
class ResponseTemplateManager(object): """ The ResponseTemplateManager class covers all functionality required to manage Response Templates. """ # to keep the singleton instance __instance = None __templates = None def __new__(cls): if ResponseTemplateManager.__instance is None: ResponseTemplateManager.__instance = object.__new__(cls) rtm = ResponseTemplateManager.__instance ResponseTemplateManager.__templates = { "404": rtm.generateTemplate("421", "Page not found"), "500": rtm.generateTemplate("500", "Internal server error"), "empty": rtm.generateTemplate( "423", "Empty API response. Probably unreachable API end point {CONNECTION_URL}", ), "error": rtm.generateTemplate( "421", "Command failed due to server error. Client should try again" ), "expired": rtm.generateTemplate("530", "SESSION NOT FOUND"), "httperror": rtm.generateTemplate( "421", "Command failed due to HTTP communication error" ), "invalid": rtm.generateTemplate( "423", "Invalid API response. Contact Support" ), "unauthorized": rtm.generateTemplate("500", "Unauthorized"), "notfound": rtm.generateTemplate("500", "Response Template not found"), } return rtm @staticmethod def getInstance(): """ Returns the singleton instance """ return ResponseTemplateManager() def generateTemplate(self, code, description): """ Returns a response template string for the given code and description """ return ("[RESPONSE]\r\nCODE={0}\r\nDESCRIPTION={1}\r\nEOF\r\n").format( code, description ) def addTemplate(self, id, plain): """ Add response template to template container """ self.__templates[id] = plain return self.__instance def getTemplate(self, id): """ Get response template instance from template container """ if self.hasTemplate(id): return RT(self.__templates[id]) return RT(self.__templates["notfound"]) def getTemplates(self): """ Return all available response templates """ tpls = {} for key in list(self.__templates.keys()): tpls[key] = RT(self.__templates[key]) return tpls def hasTemplate(self, id): """ Check if given template exists in template container """ return id in self.__templates def isTemplateMatchHash(self, tpl2, id): """ Check if given API response hash matches a given template by code and description """ h = self.getTemplate(id).getHash() return (h["CODE"] == tpl2["CODE"]) and (h["DESCRIPTION"] == tpl2["DESCRIPTION"]) def isTemplateMatchPlain(self, plain, id): """ Check if given API plain response matches a given template by code and description """ h = self.getTemplate(id).getHash() tpl2 = RP.parse(plain) return (h["CODE"] == tpl2["CODE"]) and (h["DESCRIPTION"] == tpl2["DESCRIPTION"])
The ResponseTemplateManager class covers all functionality required to manage Response Templates.
Static methods
def getInstance()
-
Expand source code
@staticmethod def getInstance(): """ Returns the singleton instance """ return ResponseTemplateManager()
Returns the singleton instance
Methods
def addTemplate(self, id, plain)
-
Expand source code
def addTemplate(self, id, plain): """ Add response template to template container """ self.__templates[id] = plain return self.__instance
Add response template to template container
def generateTemplate(self, code, description)
-
Expand source code
def generateTemplate(self, code, description): """ Returns a response template string for the given code and description """ return ("[RESPONSE]\r\nCODE={0}\r\nDESCRIPTION={1}\r\nEOF\r\n").format( code, description )
Returns a response template string for the given code and description
def getTemplate(self, id)
-
Expand source code
def getTemplate(self, id): """ Get response template instance from template container """ if self.hasTemplate(id): return RT(self.__templates[id]) return RT(self.__templates["notfound"])
Get response template instance from template container
def getTemplates(self)
-
Expand source code
def getTemplates(self): """ Return all available response templates """ tpls = {} for key in list(self.__templates.keys()): tpls[key] = RT(self.__templates[key]) return tpls
Return all available response templates
def hasTemplate(self, id)
-
Expand source code
def hasTemplate(self, id): """ Check if given template exists in template container """ return id in self.__templates
Check if given template exists in template container
def isTemplateMatchHash(self, tpl2, id)
-
Expand source code
def isTemplateMatchHash(self, tpl2, id): """ Check if given API response hash matches a given template by code and description """ h = self.getTemplate(id).getHash() return (h["CODE"] == tpl2["CODE"]) and (h["DESCRIPTION"] == tpl2["DESCRIPTION"])
Check if given API response hash matches a given template by code and description
def isTemplateMatchPlain(self, plain, id)
-
Expand source code
def isTemplateMatchPlain(self, plain, id): """ Check if given API plain response matches a given template by code and description """ h = self.getTemplate(id).getHash() tpl2 = RP.parse(plain) return (h["CODE"] == tpl2["CODE"]) and (h["DESCRIPTION"] == tpl2["DESCRIPTION"])
Check if given API plain response matches a given template by code and description