Skip to content

Commit

Permalink
Add a bunch of debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Woyken committed Feb 4, 2024
1 parent 6a1d973 commit 330526b
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 15 deletions.
160 changes: 150 additions & 10 deletions src/pyelectroluxocp/apiClient.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from base64 import b64decode
from datetime import datetime, timedelta
from json import loads
from json import dumps, loads
import logging
from types import TracebackType
from typing import Any, Dict, Optional, Type
from aiohttp import ClientSession
Expand Down Expand Up @@ -55,6 +56,9 @@ def decodeJwt(token: str):
return payload


_LOGGER: logging.Logger = logging.getLogger(__package__).getChild("OneAppApiClient")


class OneAppApiClient:
def __init__(self, client_session: Optional[ClientSession] = None) -> None:
self._client_session = client_session
Expand All @@ -77,6 +81,7 @@ def _api_headers_base(self, token: Optional[str]):

async def login_client_credentials(self, base_url: str):
"""Login using client credentials of the mobile application, used for fetching identity providers urls"""
_LOGGER.debug("login_client_credentials(), base_url: %s", base_url)
req_params = token_url(
base_url,
self._api_headers_base(None),
Expand All @@ -85,12 +90,24 @@ async def login_client_credentials(self, base_url: str):
)

async with await self._get_session().request(**req_params.__dict__) as response:
_LOGGER.debug(
"login_client_credentials(), response, requestUlr: %s, requestHeaders: %s, responseStatus: %i, responseHeaders: %s",
response.request_info.url,
response.request_info.headers,
response.status,
response.headers,
)
response_json = await response.json()
_LOGGER.debug(
"login_client_credentials(), response, json: %s", dumps(response_json)
)
response.raise_for_status()
token: ClientCredTokenResponse = await response.json()
token: ClientCredTokenResponse = response_json
return ClientToken(token)

async def exchange_login_user(self, base_url: str, id_token: str):
"""Exchange external id token to api token"""
_LOGGER.debug("exchange_login_user(), base_url: %s", base_url)
decodedToken = decodeJwt(id_token)
req_params = token_url(
base_url,
Expand All @@ -103,11 +120,23 @@ async def exchange_login_user(self, base_url: str, id_token: str):
)

async with await self._get_session().request(**req_params.__dict__) as response:
_LOGGER.debug(
"exchange_login_user(), response, requestUlr: %s, requestHeaders: %s, responseStatus: %i, responseHeaders: %s",
response.request_info.url,
response.request_info.headers,
response.status,
response.headers,
)
response_json = await response.json()
_LOGGER.debug(
"exchange_login_user(), response, json: %s", dumps(response_json)
)
response.raise_for_status()
token: UserTokenResponse = await response.json()
token: UserTokenResponse = response_json
return UserToken(token)

async def refresh_token_user(self, base_url: str, refresh_token: str):
_LOGGER.debug("refresh_token_user(), base_url: %s", base_url)
req_params = token_url(
base_url,
self._api_headers_base(None),
Expand All @@ -116,13 +145,27 @@ async def refresh_token_user(self, base_url: str, refresh_token: str):
)

async with await self._get_session().request(**req_params.__dict__) as response:
_LOGGER.debug(
"refresh_token_user(), response, requestUlr: %s, requestHeaders: %s, responseStatus: %i, responseHeaders: %s",
response.request_info.url,
response.request_info.headers,
response.status,
response.headers,
)
response_json = await response.json()
_LOGGER.debug(
"refresh_token_user(), response, json: %s", dumps(response_json)
)
response.raise_for_status()
newToken: UserTokenResponse = await response.json()
newToken: UserTokenResponse = response_json
return UserToken(newToken)

async def get_identity_providers(
self, base_url: str, client_cred_token: str, username: str
):
_LOGGER.debug(
"get_identity_providers(), base_url: %s, username: %s", base_url, username
)
req_params = identity_providers_url(
base_url,
{
Expand All @@ -134,33 +177,78 @@ async def get_identity_providers(
)

async with await self._get_session().request(**req_params.__dict__) as response:
_LOGGER.debug(
"get_identity_providers(), response, requestUlr: %s, requestHeaders: %s, responseStatus: %i, responseHeaders: %s",
response.request_info.url,
response.request_info.headers,
response.status,
response.headers,
)
response_json = await response.json()
_LOGGER.debug(
"get_identity_providers(), response, json: %s", dumps(response_json)
)
response.raise_for_status()
data: list[AuthResponse] = await response.json()
data: list[AuthResponse] = response_json
return data

async def get_user_metadata(self, base_url: str, token: str):
_LOGGER.debug("get_user_metadata(), base_url: %s", base_url)
req_params = current_user_metadata_url(base_url, self._api_headers_base(token))

async with await self._get_session().request(**req_params.__dict__) as response:
_LOGGER.debug(
"get_user_metadata(), response, requestUlr: %s, requestHeaders: %s, responseStatus: %i, responseHeaders: %s",
response.request_info.url,
response.request_info.headers,
response.status,
response.headers,
)
response_json = await response.json()
_LOGGER.debug(
"get_user_metadata(), response, json: %s", dumps(response_json)
)
response.raise_for_status()
data: UserMetadataResponse = await response.json()
data: UserMetadataResponse = response_json
return data

async def get_appliances_list(
self, base_url: str, token: str, include_metadata: bool
):
_LOGGER.debug(
"get_appliances_list(), base_url: %s, include_metadata: %s",
base_url,
include_metadata,
)
req_params = list_appliances_url(
base_url, self._api_headers_base(token), include_metadata
)

async with await self._get_session().request(**req_params.__dict__) as response:
_LOGGER.debug(
"get_appliances_list(), response, requestUlr: %s, requestHeaders: %s, responseStatus: %i, responseHeaders: %s",
response.request_info.url,
response.request_info.headers,
response.status,
response.headers,
)
response_json = await response.json()
_LOGGER.debug(
"get_appliances_list(), response, json: %s", dumps(response_json)
)
response.raise_for_status()
data: list[ApplienceStatusResponse] = await response.json()
data: list[ApplienceStatusResponse] = response_json
return data

async def get_appliance_status(
self, base_url: str, token: str, id: str, include_metadata: bool
):
_LOGGER.debug(
"get_appliance_status(), base_url: %s, id: %s, include_metadata: %s",
base_url,
id,
include_metadata,
)
req_params = get_appliance_by_id_url(
base_url,
self._api_headers_base(token),
Expand All @@ -169,35 +257,80 @@ async def get_appliance_status(
)

async with await self._get_session().request(**req_params.__dict__) as response:
_LOGGER.debug(
"get_appliance_status(), response, requestUlr: %s, requestHeaders: %s, responseStatus: %i, responseHeaders: %s",
response.request_info.url,
response.request_info.headers,
response.status,
response.headers,
)
response_json = await response.json()
_LOGGER.debug(
"get_appliance_status(), response, json: %s", dumps(response_json)
)
response.raise_for_status()
data: ApplienceStatusResponse = await response.json()
data: ApplienceStatusResponse = response_json
return data

async def get_appliance_capabilities(self, base_url: str, token: str, id: str):
_LOGGER.debug(
"get_appliance_capabilities(), base_url: %s, id: %s", base_url, id
)
req_params = get_appliance_capabilities_url(
base_url, self._api_headers_base(token), id
)

async with await self._get_session().request(**req_params.__dict__) as response:
_LOGGER.debug(
"get_appliance_capabilities(), response, requestUlr: %s, requestHeaders: %s, responseStatus: %i, responseHeaders: %s",
response.request_info.url,
response.request_info.headers,
response.status,
response.headers,
)
response_json = await response.json()
_LOGGER.debug(
"get_appliance_capabilities(), response, json: %s", dumps(response_json)
)
response.raise_for_status()
data: Dict[str, Any] = await response.json()
data: Dict[str, Any] = response_json
return data

async def get_appliances_info(self, base_url: str, token: str, ids: list[str]):
_LOGGER.debug(
"get_appliances_info(), base_url: %s, ids: %s", base_url, dumps(ids)
)
req_params = get_appliances_info_by_ids_url(
base_url,
self._api_headers_base(token),
ids,
)

async with await self._get_session().request(**req_params.__dict__) as response:
_LOGGER.debug(
"get_appliances_info(), response, requestUlr: %s, requestHeaders: %s, responseStatus: %i, responseHeaders: %s",
response.request_info.url,
response.request_info.headers,
response.status,
response.headers,
)
response_json = await response.json()
_LOGGER.debug(
"get_appliances_info(), response, json: %s", dumps(response_json)
)
response.raise_for_status()
data: list[ApplianceInfoResponse] = await response.json()
data: list[ApplianceInfoResponse] = response_json
return data

async def execute_appliance_command(
self, base_url: str, token: str, id: str, command_data: Dict[str, Any]
):
_LOGGER.debug(
"execute_appliance_command(), base_url: %s, id: %s, command_data: %s",
base_url,
id,
dumps(command_data),
)
req_params = appliance_command_url(
base_url,
self._api_headers_base(token),
Expand All @@ -206,6 +339,13 @@ async def execute_appliance_command(
)

async with await self._get_session().request(**req_params.__dict__) as response:
_LOGGER.debug(
"execute_appliance_command(), response, requestUlr: %s, requestHeaders: %s, responseStatus: %i, responseHeaders: %s",
response.request_info.url,
response.request_info.headers,
response.status,
response.headers,
)
response.raise_for_status()
await response.wait_for_close()
return
Expand Down
51 changes: 48 additions & 3 deletions src/pyelectroluxocp/gigyaClient.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import random
import time
from types import TracebackType
Expand Down Expand Up @@ -111,6 +112,9 @@ def getOAuth1Signature(
return calcSignature(base_string, secret_key)


_LOGGER: logging.Logger = logging.getLogger(__package__).getChild("GigyaClient")


class GigyaClient:
def __init__(
self, domain: str, api_key: str, client_session: Optional[ClientSession] = None
Expand All @@ -131,6 +135,7 @@ def _generate_nonce(self):

async def get_ids(self):
# https://socialize.eu1.gigya.com/socialize.getIDs
_LOGGER.debug("get_ids()")
url = f"https://socialize.{self._domain}/socialize.getIDs"
async with await self._get_session().get(
url,
Expand All @@ -143,12 +148,27 @@ async def get_ids(self):
"targetEnv": "mobile",
},
) as response:
_LOGGER.debug(
"get_ids(), response, requestUlr: %s, requestHeaders: %s, responseStatus: %i, responseHeaders: %s",
response.request_info.url,
response.request_info.headers,
response.status,
response.headers,
)
response_json = await response.json(content_type=None)
_LOGGER.debug(
"get_ids(), response, json: %s",
jsonstringify(response_json),
)
response.raise_for_status()
data: SocializeGetIdsResponse = await response.json(content_type=None)
data: SocializeGetIdsResponse = response_json
return data

async def login_session(self, username: str, password: str, gmid: str, ucid: str):
# https://accounts.eu1.gigya.com/accounts.login
_LOGGER.debug(
"login_session(), username: %s, gmid: %s, ucid: %s", username, gmid, ucid
)
url = f"https://accounts.{self._domain}/accounts.login"
async with await self._get_session().post(
url,
Expand All @@ -165,14 +185,27 @@ async def login_session(self, username: str, password: str, gmid: str, ucid: str
"ucid": ucid,
},
) as response:
_LOGGER.debug(
"login_session(), response, requestUlr: %s, requestHeaders: %s, responseStatus: %i, responseHeaders: %s",
response.request_info.url,
response.request_info.headers,
response.status,
response.headers,
)
response_json = await response.json(content_type=None)
_LOGGER.debug(
"login_session(), response, json: %s",
jsonstringify(response_json),
)
response.raise_for_status()
data: LoginResponse = await response.json(content_type=None)
data: LoginResponse = response_json
return data

async def get_JWT(
self, session_token: str, session_secret: str, gmid: str, ucid: str
):
# https://accounts.eu1.gigya.com/accounts.getJWT
_LOGGER.debug("get_JWT(), gmid: %s, ucid: %s", gmid, ucid)
url = f"https://accounts.{self._domain}/accounts.getJWT"

data_params = {
Expand All @@ -193,8 +226,20 @@ async def get_JWT(
)

async with await self._get_session().post(url, data=data_params) as response:
_LOGGER.debug(
"get_JWT(), response, requestUlr: %s, requestHeaders: %s, responseStatus: %i, responseHeaders: %s",
response.request_info.url,
response.request_info.headers,
response.status,
response.headers,
)
response_json = await response.json(content_type=None)
_LOGGER.debug(
"get_JWT(), response, json: %s",
jsonstringify(response_json),
)
response.raise_for_status()
data: GetJWTResponse = await response.json(content_type=None)
data: GetJWTResponse = response_json
return data

async def login_user(self, username: str, password: str):
Expand Down
Loading

0 comments on commit 330526b

Please sign in to comment.