Skip to content

Commit

Permalink
try to get token before get 200 unauthorized
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuen Lee committed Dec 24, 2023
1 parent cd92551 commit 3fc1076
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions custom_components/polestar_api/pypolestar/polestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ async def get_ev_data(self, vin: str):
if self.updating is True:
return
self.updating = True

# check if the token is still valid
try:
if self.auth.token_expiry < datetime.now():
await self.auth.get_token()
except PolestarAuthException as e:
_LOGGER.exception("Auth Exception: %s", str(e))
self.updating = False
return

try:
await self._get_odometer_data(vin)
except PolestarNotAuthorizedException:
Expand Down Expand Up @@ -157,6 +167,9 @@ async def get_graph_ql(self, params: dict):
raise PolestarApiException(
f"Get GraphQL error: {result.text}")
resultData = result.json()
# if we get result with errors and with message "user is not authorized" then we throw an exception
if resultData.get('errors') and resultData['errors'][0]['message'] == "User is not authorized":
raise PolestarNotAuthorizedException("Unauthorized Exception")

_LOGGER.debug(resultData)
return resultData

0 comments on commit 3fc1076

Please sign in to comment.