Coverage for centralnicreseller / apiconnector / responsetemplatemanager.py: 97%

37 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-21 15:25 +0000

1# -*- coding: utf-8 -*- 

2""" 

3 centralnicreseller.apiconnector.responsetemplatemanager 

4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

5 This module covers all functionality to 

6 manage response templates. 

7 :copyright: © 2024 Team Internet Group PLC. 

8 :license: MIT, see LICENSE for more details. 

9""" 

10 

11import centralnicreseller.apiconnector.responseparser as RP 

12from centralnicreseller.apiconnector.responsetemplate import ResponseTemplate as RT 

13 

14 

15class ResponseTemplateManager(object): 

16 """ 

17 The ResponseTemplateManager class covers all functionality required to manage 

18 Response Templates. 

19 """ 

20 

21 # to keep the singleton instance 

22 __instance = None 

23 __templates = None 

24 

25 def __new__(cls): 

26 if ResponseTemplateManager.__instance is None: 

27 ResponseTemplateManager.__instance = object.__new__(cls) 

28 rtm = ResponseTemplateManager.__instance 

29 

30 ResponseTemplateManager.__templates = { 

31 "404": rtm.generateTemplate("421", "Page not found"), 

32 "500": rtm.generateTemplate("500", "Internal server error"), 

33 "empty": rtm.generateTemplate( 

34 "423", 

35 "Empty API response. Probably unreachable API end point {CONNECTION_URL}", 

36 ), 

37 "error": rtm.generateTemplate( 

38 "421", "Command failed due to server error. Client should try again" 

39 ), 

40 "expired": rtm.generateTemplate("530", "SESSION NOT FOUND"), 

41 "httperror": rtm.generateTemplate( 

42 "421", "Command failed due to HTTP communication error" 

43 ), 

44 "invalid": rtm.generateTemplate( 

45 "423", "Invalid API response. Contact Support" 

46 ), 

47 "unauthorized": rtm.generateTemplate("500", "Unauthorized"), 

48 "notfound": rtm.generateTemplate("500", "Response Template not found"), 

49 } 

50 return rtm 

51 

52 @staticmethod 

53 def getInstance(): 

54 """ 

55 Returns the singleton instance 

56 """ 

57 return ResponseTemplateManager() 

58 

59 def generateTemplate(self, code, description): 

60 """ 

61 Returns a response template string for the given code and description 

62 """ 

63 return ("[RESPONSE]\r\nCODE={0}\r\nDESCRIPTION={1}\r\nEOF\r\n").format( 

64 code, description 

65 ) 

66 

67 def addTemplate(self, id, plain): 

68 """ 

69 Add response template to template container 

70 """ 

71 self.__templates[id] = plain 

72 return self.__instance 

73 

74 def getTemplate(self, id): 

75 """ 

76 Get response template instance from template container 

77 """ 

78 if self.hasTemplate(id): 

79 return RT(self.__templates[id]) 

80 return RT(self.__templates["notfound"]) 

81 

82 def getTemplates(self): 

83 """ 

84 Return all available response templates 

85 """ 

86 tpls = {} 

87 for key in list(self.__templates.keys()): 

88 tpls[key] = RT(self.__templates[key]) 

89 return tpls 

90 

91 def hasTemplate(self, id): 

92 """ 

93 Check if given template exists in template container 

94 """ 

95 return id in self.__templates 

96 

97 def isTemplateMatchHash(self, tpl2, id): 

98 """ 

99 Check if given API response hash matches a given template by code and description 

100 """ 

101 h = self.getTemplate(id).getHash() 

102 return (h["CODE"] == tpl2["CODE"]) and (h["DESCRIPTION"] == tpl2["DESCRIPTION"]) 

103 

104 def isTemplateMatchPlain(self, plain, id): 

105 """ 

106 Check if given API plain response matches a given template by code and description 

107 """ 

108 h = self.getTemplate(id).getHash() 

109 tpl2 = RP.parse(plain) 

110 return (h["CODE"] == tpl2["CODE"]) and (h["DESCRIPTION"] == tpl2["DESCRIPTION"])