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

feat: add warning in web requests if it fails to decode #215

Merged
merged 1 commit into from
Jun 7, 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
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
Loading