Skip to content

Commit

Permalink
Improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindwe committed May 16, 2024
1 parent 1a1f740 commit 25f2086
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions connectlife/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async def login(self) -> None:
async def get_appliances(self) -> Any:
"""Make a request."""
appliances = await self.get_appliances_json()
self.appliances = [ConnectLifeAppliance(self, a) for a in appliances]
self.appliances = [ConnectLifeAppliance(self, a) for a in appliances if "deviceId" in a]
return self.appliances


Expand All @@ -76,6 +76,11 @@ async def get_appliances_json(self) -> Any:
"User-Agent": "connectlife-api-connector 2.1.4",
"X-Token": self._access_token
}) as response:
if response.status != 200:
_LOGGER.debug(f"Response status code: {response.status}")
_LOGGER.debug(response.headers)
_LOGGER.debug(await response.text())
raise LifeConnectError(f"Unexpected response: status={response.status}")
return await response.json()


Expand All @@ -93,7 +98,11 @@ async def _initial_access_token(self):
"password": self._password,
"APIKey": API_KEY,
}) as response:
_LOGGER.debug(response.status)
if response.status != 200:
_LOGGER.debug(f"Response status code: {response.status}")
_LOGGER.debug(response.headers)
_LOGGER.debug(await response.text())
raise LifeConnectAuthError(f"Unexpected response from login: status={response.status}")
body = await self._json(response)
uid = body["UID"]
login_token = body["sessionInfo"]["cookieValue"]
Expand All @@ -102,6 +111,11 @@ async def _initial_access_token(self):
"APIKey": API_KEY,
"login_token": login_token
}) as response:
if response.status != 200:
_LOGGER.debug(f"Response status code: {response.status}")
_LOGGER.debug(response.headers)
_LOGGER.debug(await response.text())
raise LifeConnectAuthError(f"Unexpected response from getJWT: status={response.status}")
body = await self._json(response)
id_token = body["id_token"]

Expand All @@ -113,6 +127,11 @@ async def _initial_access_token(self):
"thirdType":"CDC",
"thirdClientId": uid,
}) as response:
if response.status != 200:
_LOGGER.debug(f"Response status code: {response.status}")
_LOGGER.debug(response.headers)
_LOGGER.debug(await response.text())
raise LifeConnectAuthError(f"Unexpected response from authorize: status={response.status}")
body = await response.json()
code = body["code"]

Expand All @@ -123,6 +142,11 @@ async def _initial_access_token(self):
"grant_type": "authorization_code",
"code": code,
}) as response:
if response.status != 200:
_LOGGER.debug(f"Response status code: {response.status}")
_LOGGER.debug(response.headers)
_LOGGER.debug(await response.text())
raise LifeConnectAuthError(f"Unexpected response from initial access token: status={response.status}")
body = await self._json(response)
self._access_token = body["access_token"]
# Renew 90 seconds before expiration
Expand All @@ -139,6 +163,11 @@ async def _refresh_access_token(self) -> None:
"grant_type": "refresh_token",
"refresh_token": self._refresh_token,
}) as response:
if response.status != 200:
_LOGGER.debug(f"Response status code: {response.status}")
_LOGGER.debug(response.headers)
_LOGGER.debug(await response.text())
raise LifeConnectAuthError(f"Unexpected response from refreshing access token: status={response.status}")
body = await response.json()
self._access_token = body["access_token"]
# Renew 90 seconds before expiration
Expand Down

0 comments on commit 25f2086

Please sign in to comment.