diff --git a/ThermiaOnlineAPI/api/ThermiaAPI.py b/ThermiaOnlineAPI/api/ThermiaAPI.py index 793ca93..bc90a7f 100644 --- a/ThermiaOnlineAPI/api/ThermiaAPI.py +++ b/ThermiaOnlineAPI/api/ThermiaAPI.py @@ -58,10 +58,14 @@ def __init__(self, email, password, api_type): self.__default_request_headers = { "Authorization": "Bearer ", "Content-Type": "application/json", + "cache-control": "no-cache", + "Access-Control-Allow-Origin": "*", } self.__session = requests.Session() - retry = Retry(total=20, connect=10, backoff_factor=0.1) + retry = Retry( + total=20, backoff_factor=0.1, status_forcelist=[500, 502, 503, 504] + ) adapter = HTTPAdapter(max_retries=retry) self.__session.mount("https://", adapter) @@ -81,7 +85,12 @@ def get_devices(self): status = request.status_code if status != 200: - _LOGGER.error("Error fetching devices. " + str(status)) + _LOGGER.error( + "Error fetching devices. Status: " + + str(status) + + ", Response: " + + request.text + ) return [] return request.json() @@ -107,7 +116,12 @@ def get_device_info(self, device_id: str): status = request.status_code if status != 200: - _LOGGER.error("Error fetching device info. " + str(status)) + _LOGGER.error( + "Error fetching device info. Status: " + + str(status) + + ", Response: " + + str(request.text) + ) return None return request.json() @@ -125,7 +139,12 @@ def get_device_status(self, device_id: str): status = request.status_code if status != 200: - _LOGGER.error("Error fetching device status. " + str(status)) + _LOGGER.error( + "Error fetching device status. Status :" + + str(status) + + ", Response: " + + request.text + ) return None return request.json() @@ -143,7 +162,12 @@ def get_all_alarms(self, device_id: str): status = request.status_code if status != 200: - _LOGGER.error("Error in getting device's alarms. " + str(status)) + _LOGGER.error( + "Error in getting device's alarms. Status: " + + str(status) + + ", Response: " + + request.text + ) return None return request.json() @@ -160,7 +184,12 @@ def get_historical_data_registers(self, device_id: str): status = request.status_code if status != 200: - _LOGGER.error("Error in historical data registers. " + str(status)) + _LOGGER.error( + "Error in historical data registers. Status: " + + str(status) + + ", Response: " + + request.text + ) return None return request.json() @@ -186,7 +215,10 @@ def get_historical_data( if status != 200: _LOGGER.error( - "Error in historical data for specific register. " + str(status) + "Error in historical data for specific register. Status: " + + str(status) + + ", Response: " + + request.text ) return None @@ -206,7 +238,12 @@ def get_all_available_groups(self, installation_profile_id: int): status = request.status_code if status != 200: - _LOGGER.error("Error in getting available groups. " + str(status)) + _LOGGER.error( + "Error in getting available groups. Status: " + + str(status) + + ", Response: " + + request.text + ) return None return request.json() @@ -421,6 +458,8 @@ def __get_register_group(self, device_id: str, register_group: str) -> list: + register_group + ", Status: " + str(status) + + ", Response: " + + request.text ) return [] @@ -452,8 +491,10 @@ def __set_register_value( _LOGGER.error( "Error setting register " + str(register_index) - + " value. " + + " value. Status: " + str(status) + + ", Response: " + + request.text ) def __fetch_configuration(self): @@ -461,7 +502,12 @@ def __fetch_configuration(self): status = request.status_code if status != 200: - _LOGGER.error("Error fetching API configuration. " + str(status)) + _LOGGER.error( + "Error fetching API configuration. Status: " + + str(status) + + ", Response: " + + request.text + ) raise NetworkException("Error fetching API configuration.", status) return request.json() @@ -547,7 +593,10 @@ def __authenticate(self) -> bool: csrf_token = settings["csrf"] else: _LOGGER.error( - "Error fetching authorization API. " + str(request_auth.reason) + "Error fetching authorization API. Status: " + + str(request_auth.status_code) + + ", Response: " + + request_auth.text ) raise NetworkException( "Error fetching authorization API.", request_auth.reason @@ -641,10 +690,7 @@ def __authenticate(self) -> bool: ).timestamp() self.__refresh_token = token_data.get("refresh_token") - self.__default_request_headers = { - "Authorization": "Bearer " + self.__token, - "Content-Type": "application/json", - } + self.__default_request_headers["Authorization"] = "Bearer " + self.__token _LOGGER.info("Authentication was successful, token set.")