Skip to content

Commit

Permalink
Merge pull request #48 from KorvinSzanto/patch-1
Browse files Browse the repository at this point in the history
Fix case where invalid response is returned
  • Loading branch information
catduckgnaf authored Feb 26, 2024
2 parents 8d42389 + 3195990 commit a7ee730
Showing 1 changed file with 16 additions and 5 deletions.
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

0 comments on commit a7ee730

Please sign in to comment.