Skip to content

Commit

Permalink
Refresh token if expired
Browse files Browse the repository at this point in the history
Token expires after 8 hours.  Refresh the token if the request receives an unauthorised response.
  • Loading branch information
ozrex75 authored and flz committed Jul 7, 2024
1 parent 99de7a1 commit ff30bff
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/iaqualink/systems/exo/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from iaqualink.exception import (
AqualinkServiceException,
AqualinkSystemOfflineException,
AqualinkServiceUnauthorizedException
)
from iaqualink.system import AqualinkSystem
from iaqualink.systems.exo.device import ExoDevice
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit ff30bff

Please sign in to comment.