Skip to content

Commit

Permalink
Add additional error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
klejejs committed Jun 15, 2024
1 parent 1aef523 commit d48f9a6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ debug.txt

# Visual Studio related files
/.vs/*

.DS_Store
65 changes: 52 additions & 13 deletions ThermiaOnlineAPI/api/ThermiaAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ def get_devices(self):
)
return []

return request.json()
return utils.get_response_json_or_log_and_raise_exception(
request, "Error getting devices."
)

def get_device_by_id(self, device_id: str):
self.__check_token_validity()
Expand Down Expand Up @@ -124,7 +126,9 @@ def get_device_info(self, device_id: str):
)
return None

return request.json()
return utils.get_response_json_or_log_and_raise_exception(
request, "Error getting device info."
)

def get_device_status(self, device_id: str):
self.__check_token_validity()
Expand All @@ -147,7 +151,9 @@ def get_device_status(self, device_id: str):
)
return None

return request.json()
return utils.get_response_json_or_log_and_raise_exception(
request, "Error fetching device status."
)

def get_all_alarms(self, device_id: str):
self.__check_token_validity()
Expand All @@ -170,7 +176,9 @@ def get_all_alarms(self, device_id: str):
)
return None

return request.json()
return utils.get_response_json_or_log_and_raise_exception(
request, "Error in getting device's alarms."
)

def get_historical_data_registers(self, device_id: str):
self.__check_token_validity()
Expand All @@ -192,7 +200,9 @@ def get_historical_data_registers(self, device_id: str):
)
return None

return request.json()
return utils.get_response_json_or_log_and_raise_exception(
request, "Error in historical data registers."
)

def get_historical_data(
self, device_id: str, register_id, start_date_str, end_date_str
Expand Down Expand Up @@ -222,7 +232,9 @@ def get_historical_data(
)
return None

return request.json()
return utils.get_response_json_or_log_and_raise_exception(
request, "Error in historical data for specific register."
)

def get_all_available_groups(self, installation_profile_id: int):
self.__check_token_validity()
Expand All @@ -246,7 +258,9 @@ def get_all_available_groups(self, installation_profile_id: int):
)
return None

return request.json()
return utils.get_response_json_or_log_and_raise_exception(
request, "Error in getting available groups."
)

def get__group_temperatures(self, device_id: str):
return self.__get_register_group(device_id, REG_GROUP_TEMPERATURES)
Expand Down Expand Up @@ -463,7 +477,9 @@ def __get_register_group(self, device_id: str, register_group: str) -> list:
)
return []

return request.json()
return utils.get_response_json_or_log_and_raise_exception(
request, "Error in getting device's register group: " + register_group
)

def __set_register_value(
self, device: ThermiaHeatPump, register_index: int, register_value: int
Expand Down Expand Up @@ -510,7 +526,9 @@ def __fetch_configuration(self):
)
raise NetworkException("Error fetching API configuration.", status)

return request.json()
return utils.get_response_json_or_log_and_raise_exception(
request, "Error fetching API configuration."
)

def __authenticate_refresh_token(self) -> Optional[str]:
request_token__data = {
Expand Down Expand Up @@ -584,9 +602,21 @@ def __authenticate(self) -> bool:
settings_string = request_auth.text.split("var SETTINGS = ")
settings_string = settings_string[1].split("};")[0] + "}"
if len(settings_string) > 0:
settings = json.loads(settings_string)
state_code = str(settings["transId"]).split("=")[1]
csrf_token = settings["csrf"]
try:
settings = json.loads(settings_string)
state_code = str(settings["transId"]).split("=")[1]
csrf_token = settings["csrf"]
except Exception as e:
_LOGGER.error(
"Error parsing authorization API settings. "
+ str(request_auth.text),
e,
)
raise NetworkException(
"Error parsing authorization API settings. "
+ request_auth.text,
e,
)
else:
_LOGGER.error(
"Error fetching authorization API. Status: "
Expand Down Expand Up @@ -675,7 +705,16 @@ def __authenticate(self) -> bool:

request_token_text = request_token.text

token_data = json.loads(request_token_text)
try:
token_data = json.loads(request_token_text)
except Exception as e:
_LOGGER.error(
"Error parsing authentication token data. " + str(request_token_text),
e,
)
raise NetworkException(
"Error parsing authentication token data. " + request_token_text, e
)

self.__token = token_data["access_token"]
self.__token_valid_to = token_data["expires_on"]
Expand Down
12 changes: 12 additions & 0 deletions ThermiaOnlineAPI/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import json
from base64 import urlsafe_b64encode
import logging
import random
import string
from typing import Any, TypeVar

T = TypeVar("T")


_LOGGER = logging.getLogger(__name__)


def get_dict_value_or_none(dictionary, key) -> Any:
if dictionary is None or key not in dictionary:
return None
Expand Down Expand Up @@ -43,3 +47,11 @@ def generate_challenge(length):
characters = string.ascii_letters + string.digits
challenge = "".join(random.choice(characters) for _ in range(length))
return challenge


def get_response_json_or_log_and_raise_exception(response, message: str):
try:
return response.json()
except Exception as e:
_LOGGER.error(f"{message} {response.status_code} {response.text}")
raise Exception(f"{message} {response.status_code} {response.text}") from e

0 comments on commit d48f9a6

Please sign in to comment.