Skip to content

Commit 7cd33b4

Browse files
fix(messaging): return a specific error template if API response' code or description are missing
1 parent d66d495 commit 7cd33b4

File tree

8 files changed

+16
-12
lines changed

8 files changed

+16
-12
lines changed

hexonet/apiconnector/responseparser.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ def parse(raw):
3939
r['PROPERTY'][prop].append(value)
4040
else:
4141
r[attr.upper()] = value
42-
43-
if 'DESCRIPTION' not in r:
44-
r['DESCRIPTION'] = ""
4542
return r
4643

4744

hexonet/apiconnector/responsetemplate.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ def __init__(self, response=""):
3939
#: Holds the response as hash
4040
self.__hash = RP.parse(self.__raw)
4141

42+
if ('CODE' not in self.__hash) or ('DESCRIPTION' not in self.__hash):
43+
self.__raw = '[RESPONSE]\r\nCODE=423\r\nDESCRIPTION=Invalid API response. Contact Support\r\nEOF\r\n'
44+
self.__hash = RP.parse(self.__raw)
45+
4246
def getCode(self):
4347
"""
4448
Returns the API response code as integer

hexonet/apiconnector/responsetemplatemanager.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def __new__(self):
3333
"error": rtm.generateTemplate("421", "Command failed due to server error. Client should try again"),
3434
"expired": rtm.generateTemplate("530", "SESSION NOT FOUND"),
3535
"httperror": rtm.generateTemplate("421", "Command failed due to HTTP communication error"),
36+
"invalid": rtm.generateTemplate("423", "Invalid API response. Contact Support"),
3637
"unauthorized": rtm.generateTemplate("530", "Unauthorized"),
3738
}
3839
return rtm

tests/test_apiclient.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
import pytest
55
import platform
66

7+
rtm = RTM()
8+
79

810
def test_apiclientmethods():
911
cl = AC()
10-
rtm = RTM.getInstance()
1112
rtm.addTemplate(
1213
'login200',
1314
'[RESPONSE]\r\nPROPERTY[SESSION][0]=h8JLZZHdF2WgWWXlwbKWzEG3XrzoW4y' +

tests/test_response.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
def test_responsemethods():
7-
rtm = RTM.getInstance()
7+
rtm = RTM()
88
rtm.addTemplate(
99
'listP0',
1010
'[RESPONSE]\r\nPROPERTY[TOTAL][0]=2701\r\nPROPERTY[FIRST][0]=0\r\nP' +

tests/test_responseparser.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@
44

55

66
def test_rpmethods():
7-
rtm = RTM.getInstance()
7+
rtm = RTM()
88
rtm.addTemplate(
99
'OK',
1010
rtm.generateTemplate('200', 'Command completed successfully')
1111
)
1212

13-
# #.parse()
14-
plain = re.sub(r'/\r\nDESCRIPTION=/', '', rtm.generateTemplate('421', ''))
15-
assert RP.parse(plain)["DESCRIPTION"] is ""
16-
1713
# #.serialize()
1814
# [w/ PROPERTY]
1915
r = rtm.getTemplate('OK').getHash()

tests/test_responsetemplate.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33

44
def test_responsetemplatemethods():
5+
# check invalid api response
6+
tpl = ResponseTemplate("[RESPONSE]\r\nqueuetime=0\r\nEOF\r\n")
7+
assert tpl.getCode() == 423
8+
assert tpl.getDescription() == "Invalid API response. Contact Support"
9+
510
descr = "Empty API response. Probably unreachable API end point"
611
# check instance [raw empty string]
712
tpl = ResponseTemplate()

tests/test_responsetemplatemanager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
def test_rtmmethods():
66
# #.getTemplate()
7-
rtm = RTM.getInstance()
7+
rtm = RTM()
88
tpl = rtm.getTemplate('IwontExist')
99
assert tpl.getCode() == 500
1010
assert tpl.getDescription() == 'Response Template not found'
1111

1212
# #.getTemplates()
13-
defaultones = sorted(['404', '500', 'error', 'httperror', 'empty', 'unauthorized', 'expired'])
13+
defaultones = sorted(['404', '500', 'error', 'httperror', 'invalid', 'empty', 'unauthorized', 'expired'])
1414
availableones = sorted(rtm.getTemplates().keys())
1515
assert defaultones == availableones
1616

0 commit comments

Comments
 (0)