From c6b87c2e6f7b469595ba638f079005a0985c362c Mon Sep 17 00:00:00 2001 From: Krisjanis Lejejs Date: Mon, 3 Jan 2022 22:26:33 +0200 Subject: [PATCH] Request header store changes --- ThermiaOnlineAPI/api/ThermiaAPI.py | 29 +++++++++++++++-------------- example.py | 4 +++- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/ThermiaOnlineAPI/api/ThermiaAPI.py b/ThermiaOnlineAPI/api/ThermiaAPI.py index 463ca6a..ca5a8b3 100644 --- a/ThermiaOnlineAPI/api/ThermiaAPI.py +++ b/ThermiaOnlineAPI/api/ThermiaAPI.py @@ -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/" @@ -25,6 +20,11 @@ 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() @@ -32,7 +32,7 @@ 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)) @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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 diff --git a/example.py b/example.py index 716f9f5..94b086f 100644 --- a/example.py +++ b/example.py @@ -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)) @@ -31,6 +31,8 @@ print("\n") +thermia.update_data() + heat_pump.set_temperature(19) heat_pump.set_operation_mode("COMPRESSOR")