Coverage for hexonet/apiconnector/responsetemplate.py: 89%
47 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.responsetemplate
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 This module covers all basic functionality to
6 deal with Backend API responses.
7 :copyright: © 2018 by HEXONET GmbH.
8 :license: MIT, see LICENSE for more details.
9"""
11import hexonet.apiconnector.responseparser as RP
14class ResponseTemplate(object):
15 """
16 The ResponseTemplate class is the base class for the Response Class that
17 covers basic functionality to work with Backend API responses.
18 """
20 def __init__(self, response=""):
21 #: Holds the response as plain text / string
22 self._raw = response
23 if (response == "") or (response is None):
24 descr = "Empty API response. Probably unreachable API end point {CONNECTION_URL}"
25 self._raw = "[RESPONSE]\r\nCODE=423\r\nDESCRIPTION=%s\r\nEOF\r\n" % (descr)
27 # try/except to support old versions of python (python2.5)
28 try:
29 if isinstance(self._raw, bytes):
30 self._raw = self._raw.decode("utf-8")
31 except UnicodeError:
32 self._raw = self._raw.decode("latin1")
33 except BaseException:
34 self._raw = self._raw.decode("utf-8")
36 if isinstance(response, dict):
37 raise TypeError(
38 'Type "dict" is not allowed for parameter "response". Use type "string" instead.'
39 )
40 else:
41 #: Holds the response as hash
42 self.__hash = RP.parse(self._raw)
44 if ("CODE" not in self.__hash) or ("DESCRIPTION" not in self.__hash):
45 self._raw = "[RESPONSE]\r\nCODE=423\r\nDESCRIPTION=Invalid API response. Contact Support\r\nEOF\r\n"
46 self.__hash = RP.parse(self._raw)
48 def getCode(self):
49 """
50 Returns the API response code as integer
51 """
52 return int(self.__hash["CODE"])
54 def getDescription(self):
55 """
56 Returns the API response description
57 """
58 return self.__hash["DESCRIPTION"]
60 def getPlain(self):
61 """
62 Returns the plain API response
63 """
64 return self._raw
66 def getQueuetime(self):
67 """
68 Get Queuetime of API response as float value
69 """
70 if "QUEUETIME" in self.__hash:
71 return float(self.__hash["QUEUETIME"])
72 return 0.00
74 def getHash(self):
75 """
76 Get API response as Hash
77 """
78 return self.__hash
80 def getRuntime(self):
81 """
82 Get Runtime of API response as float value
83 """
84 if "RUNTIME" in self.__hash:
85 return float(self.__hash["RUNTIME"])
86 return 0.00
88 def isError(self):
89 """
90 Check if current API response represents an error case (5xx)
91 """
92 return self.__hash["CODE"][0] == "5"
94 def isSuccess(self):
95 """
96 Check if current API response represents a success case (2xx)
97 """
98 return self.__hash["CODE"][0] == "2"
100 def isTmpError(self):
101 """
102 Check if current API response represents a temporary error case (4xx)
103 """
104 return self.__hash["CODE"][0] == "4"
106 def isPending(self):
107 """
108 Check if current operation is returned as pending
109 """
110 if "PENDING" in self.__hash:
111 return self.__hash["PENDING"] == "1"
112 return False