Coverage for hexonet/apiconnector/responsetemplatemanager.py: 97%
38 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-09 09:07 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-09 09:07 +0000
1# -*- coding: utf-8 -*-
2"""
3 hexonet.apiconnector.responsetemplatemanager
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 This module covers all functionality to
6 manage response templates.
7 :copyright: © 2018 by HEXONET GmbH.
8 :license: MIT, see LICENSE for more details.
9"""
11import hexonet.apiconnector.responseparser as RP
12from hexonet.apiconnector.responsetemplate import ResponseTemplate as RT
15class ResponseTemplateManager(object):
16 """
17 The ResponseTemplateManager class covers all functionality required to manage
18 Response Templates.
19 """
21 # to keep the singleton instance
22 __instance = None
23 __templates = None
25 def __new__(cls):
26 if ResponseTemplateManager.__instance is None:
27 ResponseTemplateManager.__instance = object.__new__(cls)
28 rtm = ResponseTemplateManager.__instance
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("530", "Unauthorized"),
48 }
49 return rtm
51 @staticmethod
52 def getInstance():
53 """
54 Returns the singleton instance
55 """
56 return ResponseTemplateManager()
58 def generateTemplate(self, code, description):
59 """
60 Returns a response template string for the given code and description
61 """
62 return ("[RESPONSE]\r\nCODE={0}\r\nDESCRIPTION={1}\r\nEOF\r\n").format(
63 code, description
64 )
66 def addTemplate(self, id, plain):
67 """
68 Add response template to template container
69 """
70 self.__templates[id] = plain
71 return self.__instance
73 def getTemplate(self, id):
74 """
75 Get response template instance from template container
76 """
77 if self.hasTemplate(id):
78 return RT(self.__templates[id])
79 return RT(self.generateTemplate("500", "Response Template not found"))
81 def getTemplates(self):
82 """
83 Return all available response templates
84 """
85 tpls = {}
86 for key in list(self.__templates.keys()):
87 tpls[key] = RT(self.__templates[key])
88 return tpls
90 def hasTemplate(self, id):
91 """
92 Check if given template exists in template container
93 """
94 return id in self.__templates
96 def isTemplateMatchHash(self, tpl2, id):
97 """
98 Check if given API response hash matches a given template by code and description
99 """
100 h = self.getTemplate(id).getHash()
101 return (h["CODE"] == tpl2["CODE"]) and (h["DESCRIPTION"] == tpl2["DESCRIPTION"])
103 def isTemplateMatchPlain(self, plain, id):
104 """
105 Check if given API plain response matches a given template by code and description
106 """
107 h = self.getTemplate(id).getHash()
108 tpl2 = RP.parse(plain)
109 return (h["CODE"] == tpl2["CODE"]) and (h["DESCRIPTION"] == tpl2["DESCRIPTION"])