From ff30bfff6286631e64419ace9c9e7b60cbf3ecf1 Mon Sep 17 00:00:00 2001 From: ozrex75 Date: Wed, 16 Feb 2022 10:52:19 +1000 Subject: [PATCH] Refresh token if expired Token expires after 8 hours. Refresh the token if the request receives an unauthorised response. --- src/iaqualink/systems/exo/system.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/iaqualink/systems/exo/system.py b/src/iaqualink/systems/exo/system.py index d0db405..6511452 100644 --- a/src/iaqualink/systems/exo/system.py +++ b/src/iaqualink/systems/exo/system.py @@ -8,6 +8,7 @@ from iaqualink.exception import ( AqualinkServiceException, AqualinkSystemOfflineException, + AqualinkServiceUnauthorizedException ) from iaqualink.system import AqualinkSystem from iaqualink.systems.exo.device import ExoDevice @@ -40,7 +41,19 @@ def __repr__(self) -> str: async def send_devices_request(self, **kwargs: Any) -> httpx.Response: url = f"{EXO_DEVICES_URL}/{self.serial}/shadow" headers = {"Authorization": self.aqualink.id_token} - return await self.aqualink.send_request(url, headers=headers, **kwargs) + + try: + r = await self.aqualink.send_request(url, headers=headers, **kwargs) + return r + except AqualinkServiceUnauthorizedException: + try: + # token expired so refresh the token and try again + await self.aqualink.login() + headers = {"Authorization": self.aqualink.id_token} + r = await self.aqualink.send_request(url, headers=headers, **kwargs) + return r + except: + raise async def send_reported_state_request(self) -> httpx.Response: return await self.send_devices_request()