Skip to content

Commit

Permalink
Fix request retries, add status text logging to requests
Browse files Browse the repository at this point in the history
  • Loading branch information
klejejs committed May 7, 2023
1 parent 2ba9384 commit f56ebf3
Showing 1 changed file with 61 additions and 15 deletions.
76 changes: 61 additions & 15 deletions ThermiaOnlineAPI/api/ThermiaAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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

Expand All @@ -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()
Expand Down Expand Up @@ -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 []

Expand Down Expand Up @@ -452,16 +491,23 @@ def __set_register_value(
_LOGGER.error(
"Error setting register "
+ str(register_index)
+ " value. "
+ " value. Status: "
+ str(status)
+ ", Response: "
+ request.text
)

def __fetch_configuration(self):
request = self.__session.get(self.__api_config_url)
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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.")

Expand Down

0 comments on commit f56ebf3

Please sign in to comment.