Skip to content

Commit

Permalink
feat: add warning in web requests if it fails to decode (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lash-L authored Jun 7, 2024
1 parent af6f107 commit 6ae69e9
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions roborock/web_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import base64
import hashlib
import hmac
import logging
import math
import secrets
import time

import aiohttp
from aiohttp import ContentTypeError

from roborock.containers import HomeData, HomeDataRoom, ProductResponse, RRiot, UserData
from roborock.exceptions import (
Expand All @@ -23,6 +25,8 @@
RoborockUrlException,
)

_LOGGER = logging.getLogger(__name__)


class RoborockApiClient:
def __init__(self, username: str, base_url=None) -> None:
Expand Down Expand Up @@ -294,11 +298,23 @@ async def request(self, method: str, url: str, params=None, data=None, headers=N
_url = "/".join(s.strip("/") for s in [self.base_url, url])
_headers = {**self.base_headers, **(headers or {})}
async with aiohttp.ClientSession() as session:
async with session.request(
method,
_url,
params=params,
data=data,
headers=_headers,
) as resp:
return await resp.json()
try:
async with session.request(
method,
_url,
params=params,
data=data,
headers=_headers,
) as resp:
return await resp.json()
except ContentTypeError as err:
"""If we get an error, lets log everything for debugging."""
try:
resp_json = await resp.json(content_type=None)
_LOGGER.info("Resp: %s", resp_json)
except ContentTypeError as err_2:
_LOGGER.info(err_2)
resp_raw = await resp.read()
_LOGGER.info("Resp raw: %s", resp_raw)
# Still raise the err so that it's clear it failed.
raise err

0 comments on commit 6ae69e9

Please sign in to comment.