Skip to content

Commit f56ebf3

Browse files
committed
Fix request retries, add status text logging to requests
1 parent 2ba9384 commit f56ebf3

1 file changed

Lines changed: 61 additions & 15 deletions

File tree

ThermiaOnlineAPI/api/ThermiaAPI.py

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,14 @@ def __init__(self, email, password, api_type):
5858
self.__default_request_headers = {
5959
"Authorization": "Bearer ",
6060
"Content-Type": "application/json",
61+
"cache-control": "no-cache",
62+
"Access-Control-Allow-Origin": "*",
6163
}
6264

6365
self.__session = requests.Session()
64-
retry = Retry(total=20, connect=10, backoff_factor=0.1)
66+
retry = Retry(
67+
total=20, backoff_factor=0.1, status_forcelist=[500, 502, 503, 504]
68+
)
6569
adapter = HTTPAdapter(max_retries=retry)
6670
self.__session.mount("https://", adapter)
6771

@@ -81,7 +85,12 @@ def get_devices(self):
8185
status = request.status_code
8286

8387
if status != 200:
84-
_LOGGER.error("Error fetching devices. " + str(status))
88+
_LOGGER.error(
89+
"Error fetching devices. Status: "
90+
+ str(status)
91+
+ ", Response: "
92+
+ request.text
93+
)
8594
return []
8695

8796
return request.json()
@@ -107,7 +116,12 @@ def get_device_info(self, device_id: str):
107116
status = request.status_code
108117

109118
if status != 200:
110-
_LOGGER.error("Error fetching device info. " + str(status))
119+
_LOGGER.error(
120+
"Error fetching device info. Status: "
121+
+ str(status)
122+
+ ", Response: "
123+
+ str(request.text)
124+
)
111125
return None
112126

113127
return request.json()
@@ -125,7 +139,12 @@ def get_device_status(self, device_id: str):
125139
status = request.status_code
126140

127141
if status != 200:
128-
_LOGGER.error("Error fetching device status. " + str(status))
142+
_LOGGER.error(
143+
"Error fetching device status. Status :"
144+
+ str(status)
145+
+ ", Response: "
146+
+ request.text
147+
)
129148
return None
130149

131150
return request.json()
@@ -143,7 +162,12 @@ def get_all_alarms(self, device_id: str):
143162
status = request.status_code
144163

145164
if status != 200:
146-
_LOGGER.error("Error in getting device's alarms. " + str(status))
165+
_LOGGER.error(
166+
"Error in getting device's alarms. Status: "
167+
+ str(status)
168+
+ ", Response: "
169+
+ request.text
170+
)
147171
return None
148172

149173
return request.json()
@@ -160,7 +184,12 @@ def get_historical_data_registers(self, device_id: str):
160184
status = request.status_code
161185

162186
if status != 200:
163-
_LOGGER.error("Error in historical data registers. " + str(status))
187+
_LOGGER.error(
188+
"Error in historical data registers. Status: "
189+
+ str(status)
190+
+ ", Response: "
191+
+ request.text
192+
)
164193
return None
165194

166195
return request.json()
@@ -186,7 +215,10 @@ def get_historical_data(
186215

187216
if status != 200:
188217
_LOGGER.error(
189-
"Error in historical data for specific register. " + str(status)
218+
"Error in historical data for specific register. Status: "
219+
+ str(status)
220+
+ ", Response: "
221+
+ request.text
190222
)
191223
return None
192224

@@ -206,7 +238,12 @@ def get_all_available_groups(self, installation_profile_id: int):
206238
status = request.status_code
207239

208240
if status != 200:
209-
_LOGGER.error("Error in getting available groups. " + str(status))
241+
_LOGGER.error(
242+
"Error in getting available groups. Status: "
243+
+ str(status)
244+
+ ", Response: "
245+
+ request.text
246+
)
210247
return None
211248

212249
return request.json()
@@ -421,6 +458,8 @@ def __get_register_group(self, device_id: str, register_group: str) -> list:
421458
+ register_group
422459
+ ", Status: "
423460
+ str(status)
461+
+ ", Response: "
462+
+ request.text
424463
)
425464
return []
426465

@@ -452,16 +491,23 @@ def __set_register_value(
452491
_LOGGER.error(
453492
"Error setting register "
454493
+ str(register_index)
455-
+ " value. "
494+
+ " value. Status: "
456495
+ str(status)
496+
+ ", Response: "
497+
+ request.text
457498
)
458499

459500
def __fetch_configuration(self):
460501
request = self.__session.get(self.__api_config_url)
461502
status = request.status_code
462503

463504
if status != 200:
464-
_LOGGER.error("Error fetching API configuration. " + str(status))
505+
_LOGGER.error(
506+
"Error fetching API configuration. Status: "
507+
+ str(status)
508+
+ ", Response: "
509+
+ request.text
510+
)
465511
raise NetworkException("Error fetching API configuration.", status)
466512

467513
return request.json()
@@ -547,7 +593,10 @@ def __authenticate(self) -> bool:
547593
csrf_token = settings["csrf"]
548594
else:
549595
_LOGGER.error(
550-
"Error fetching authorization API. " + str(request_auth.reason)
596+
"Error fetching authorization API. Status: "
597+
+ str(request_auth.status_code)
598+
+ ", Response: "
599+
+ request_auth.text
551600
)
552601
raise NetworkException(
553602
"Error fetching authorization API.", request_auth.reason
@@ -641,10 +690,7 @@ def __authenticate(self) -> bool:
641690
).timestamp()
642691
self.__refresh_token = token_data.get("refresh_token")
643692

644-
self.__default_request_headers = {
645-
"Authorization": "Bearer " + self.__token,
646-
"Content-Type": "application/json",
647-
}
693+
self.__default_request_headers["Authorization"] = "Bearer " + self.__token
648694

649695
_LOGGER.info("Authentication was successful, token set.")
650696

0 commit comments

Comments
 (0)