Skip to content

Commit

Permalink
Request header store changes
Browse files Browse the repository at this point in the history
  • Loading branch information
klejejs committed Jan 3, 2022
1 parent 450f0bf commit c6b87c2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
29 changes: 15 additions & 14 deletions ThermiaOnlineAPI/api/ThermiaAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@

LOGGER = logging.getLogger(__name__)

DEFAULT_REQUEST_HEADERS = {
"Authorization": "Bearer %s",
"Content-Type": "application/json",
}

THERMIA_API_CONFIG_URL = "https://online.thermia.se/api/configuration"
THERMIA_INSTALLATION_PATH = "/api/v1/Registers/Installations/"

Expand All @@ -25,14 +20,19 @@ def __init__(self, email, password):
self.__token = None
self.__token_valid_to = None

self.__default_request_headers = {
"Authorization": "Bearer ",
"Content-Type": "application/json",
}

self.configuration = self.__fetch_configuration()
self.authenticated = self.__authenticate()

def get_devices(self):
self.__check_token_validity()

url = self.configuration["apiBaseUrl"] + "/api/v1/InstallationsInfo/own"
request = requests.get(url, headers=DEFAULT_REQUEST_HEADERS)
request = requests.get(url, headers=self.__default_request_headers)
status = request.status_code
LOGGER.info("Fetching devices. " + str(status))

Expand All @@ -50,7 +50,7 @@ def get_device_info(self, device):
+ "/api/v1/installations/"
+ str(device["id"])
)
request = requests.get(url, headers=DEFAULT_REQUEST_HEADERS)
request = requests.get(url, headers=self.__default_request_headers)
status = request.status_code

if status != 200:
Expand All @@ -68,7 +68,7 @@ def get_device_status(self, device):
+ str(device["id"])
+ "/status"
)
request = requests.get(url, headers=DEFAULT_REQUEST_HEADERS)
request = requests.get(url, headers=self.__default_request_headers)
status = request.status_code

if status != 200:
Expand All @@ -86,7 +86,7 @@ def get_temperature_status(self, device: ThermiaHeatPump):
+ str(device.id)
+ "/Groups/REG_GROUP_TEMPERATURES"
)
request = requests.get(url, headers=DEFAULT_REQUEST_HEADERS)
request = requests.get(url, headers=self.__default_request_headers)
status = request.status_code

if status != 200:
Expand Down Expand Up @@ -127,7 +127,7 @@ def get_operation_mode(self, device: ThermiaHeatPump):
+ str(device.id)
+ "/Groups/REG_GROUP_OPERATIONAL_OPERATION"
)
request = requests.get(url, headers=DEFAULT_REQUEST_HEADERS)
request = requests.get(url, headers=self.__default_request_headers)
status = request.status_code

if status != 200:
Expand Down Expand Up @@ -208,7 +208,7 @@ def __set_register_value(
"clientUuid": "api-client-uuid",
}

request = requests.post(url, headers=DEFAULT_REQUEST_HEADERS, json=body)
request = requests.post(url, headers=self.__default_request_headers, json=body)

status = request.status_code
if status != 200:
Expand Down Expand Up @@ -259,9 +259,10 @@ def __authenticate(self):
self.__token = auth_data.get("token")
self.__token_valid_to = token_valid_to

auth = DEFAULT_REQUEST_HEADERS.get("Authorization")
auth = auth % self.__token
DEFAULT_REQUEST_HEADERS["Authorization"] = auth
self.__default_request_headers = {
"Authorization": "Bearer " + self.__token,
"Content-Type": "application/json",
}

LOGGER.info("Authentication was successful, token set.")
return True
Expand Down
4 changes: 3 additions & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

print("Connected: " + str(thermia.connected))

heat_pump = thermia.heat_pumps[0]
heat_pump = thermia.fetch_heat_pumps()[0]

print("Name: " + heat_pump.name)
print("Id: " + str(heat_pump.id))
Expand All @@ -31,6 +31,8 @@

print("\n")

thermia.update_data()

heat_pump.set_temperature(19)
heat_pump.set_operation_mode("COMPRESSOR")

Expand Down

0 comments on commit c6b87c2

Please sign in to comment.