You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
check self._token_expires_at before requesting new token (but this might not be a good idea if goalType, goalWeight, and initialWeight change frequently)
# TODO: check self._token_expires_at before requesting new token (but this might not be a good idea if goalType, goalWeight, and initialWeight change frequently)
"""weight_gurus API."""importasyncioimportsocketimportaiohttpimportasync_timeoutfromhomeassistant.constimportCONF_EMAIL, CONF_PASSWORDfrom .constimportLOGGERTIMEOUT=10classApiClientException(Exception):
"""Api Client Exception."""classWeightGurusApiClient:
# TODO: refactor this class so functions live in appropriate method subnodesdef__init__(
self, email: str, password: str, session: aiohttp.ClientSession
) ->None:
"""Initialize the API client."""self._email=emailself._password=passwordself._session=sessionself._account_login_dict: dict= {}
self._token_expires_at: str=""asyncdefasync_get_data(self) ->dict:
"""Get data from the API."""url="https://api.weightgurus.com/v3/operation/"headers=awaitself.async_build_headers()
data=awaitself.api_wrapper("get", url, {}, headers)
returnawaitself.get_last_entry_and_merge_dicts(data)
asyncdefasync_build_headers(self) ->dict:
"""Build headers for the API."""account_access_token=awaitself.async_get_token_and_save_account_dict()
headers= {
"Authorization": f"Bearer {account_access_token}",
"Accept": "application/json, text/plain, */*",
}
returnheadersasyncdefasync_get_token_and_save_account_dict(self) ->dict:
"""Get account access token and save account dict."""# TODO: check self._token_expires_at before requesting new token (but this might not be a good idea if goalType, goalWeight, and initialWeight change frequently)account_credentials= {CONF_EMAIL: self._email, CONF_PASSWORD: self._password}
account_login_response=awaitself._session.post(
f"https://api.weightgurus.com/v3/account/login", data=account_credentials
)
account_login_json=awaitaccount_login_response.json()
self._token_expires_at=account_login_json["expiresAt"]
self._account_login_dict=account_login_json["account"]
account_access_token=account_login_json["accessToken"]
returnaccount_access_tokenasyncdefget_last_entry_and_merge_dicts(self, data: dict) ->dict:
"""Get last entry and merge dicts."""sorted_data=sorted(
data["operations"],
key=lambdax: x["entryTimestamp"],
)
last_entry=sorted_data[-1:][0]
merged_dicts= {**self._account_login_dict, **last_entry}
returnmerged_dictsasyncdefapi_wrapper(
self, method: str, url: str, data: dict= {}, headers: dict= {}
) ->dict:
"""API wrapper."""try:
asyncwithasync_timeout.timeout(TIMEOUT):
ifmethod=="get":
response=awaitself._session.get(url, headers=headers)
returnawaitresponse.json()
elifmethod=="put":
awaitself._session.put(url, headers=headers, json=data)
elifmethod=="patch":
awaitself._session.patch(url, headers=headers, json=data)
elifmethod=="post":
awaitself._session.post(url, headers=headers, json=data)
exceptasyncio.TimeoutErrorasexception:
LOGGER.error(
"Timeout error fetching information from %s - %s", url, exception
)
except (KeyError, TypeError) asexception:
LOGGER.error("Error parsing information from %s - %s", url, exception)
except (aiohttp.ClientError, socket.gaierror) asexception:
LOGGER.error("Error fetching information from %s - %s", url, exception)
exceptExceptionasexception: # pylint: disable=broad-exceptLOGGER.error("Something really wrong happened! - %s", exception)
e7d2a62e796b26ce4f2d0c814b51da8034aa8757
The text was updated successfully, but these errors were encountered:
check self._token_expires_at before requesting new token (but this might not be a good idea if goalType, goalWeight, and initialWeight change frequently)
weight_gurus_homeassistant/custom_components/weight_gurus/api.py
Line 48 in eb49238
e7d2a62e796b26ce4f2d0c814b51da8034aa8757
The text was updated successfully, but these errors were encountered: