Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix case where invalid response is returned #48

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions custom_components/ryobi_gdo/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,26 @@ def __init__(self, username: str, password: str, device_id: str | None = None):

async def _process_request(
self, url: str, method: str, data: dict[str, str]
) -> Any:
) -> Optional[dict]:
"""Process HTTP requests."""
async with aiohttp.ClientSession() as session:
http_hethod = getattr(session, method)
LOGGER.debug("Connecting to %s using %s", url, method)
reply = None
try:
async with http_hethod(url, data=data) as response:
reply = await response.text()
rawReply = await response.text()
try:
reply = json.loads(reply)
reply = json.loads(rawReply)
if not isinstance(reply, dict):
reply = None
except ValueError:
LOGGER.warning(
"Reply was not in JSON format: %s", response.text()
"Reply was not in JSON format: %s", rawReply
)

if response.status in [404, 405, 500]:
LOGGER.warning("HTTP Error: %s", response.text())
LOGGER.warning("HTTP Error: %s", rawReply)
except (TimeoutError, ServerTimeoutError):
LOGGER.error("Timeout connecting to %s", url)
except ServerConnectionError:
Expand All @@ -112,6 +115,8 @@ async def get_api_key(self) -> bool:
data = {"username": self.username, "password": self.password}
method = "post"
request = await self._process_request(url, method, data)
if request is None:
return auth_ok
try:
resp_meta = request["result"]["metaData"]
self.api_key = resp_meta["wskAuthAttempts"][0]["apiKey"]
Expand All @@ -127,6 +132,8 @@ async def check_device_id(self) -> bool:
data = {"username": self.username, "password": self.password}
method = "get"
request = await self._process_request(url, method, data)
if request is None:
return device_found
try:
result = request["result"]
except KeyError:
Expand All @@ -146,6 +153,8 @@ async def get_devices(self) -> list:
data = {"username": self.username, "password": self.password}
method = "get"
request = await self._process_request(url, method, data)
if request is None:
return devices
try:
result = request["result"]
except KeyError:
Expand All @@ -169,6 +178,8 @@ async def update(self) -> bool:
data = {"username": self.username, "password": self.password}
method = "get"
request = await self._process_request(url, method, data)
if request is None:
return update_ok
try:
dtm = request["result"][0]["deviceTypeMap"]
# Parse the modules
Expand Down