From d435d9eb2d711f759380676bb5f3fc07cbab437b Mon Sep 17 00:00:00 2001 From: Simon Lamon <32477463+silamon@users.noreply.github.com> Date: Sat, 19 Jul 2025 08:08:35 +0000 Subject: [PATCH] Updating request handling --- src/linkplay/exceptions.py | 13 +++++++++++-- src/linkplay/utils.py | 33 +++++++++++++++++++++++++-------- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/linkplay/exceptions.py b/src/linkplay/exceptions.py index db29433..badd2db 100644 --- a/src/linkplay/exceptions.py +++ b/src/linkplay/exceptions.py @@ -1,12 +1,21 @@ +"""This module defines custom exceptions for the LinkPlay integration.""" + + class LinkPlayException(Exception): - pass + """Base exception for LinkPlay errors.""" class LinkPlayRequestException(LinkPlayException): - pass + """Exception raised for errors in LinkPlay requests.""" + + +class LinkPlayRequestCancelledException(LinkPlayException): + """Exception raised when a LinkPlay request is cancelled.""" class LinkPlayInvalidDataException(LinkPlayException): + """Exception raised for invalid data received from LinkPlay endpoints.""" + def __init__(self, message: str = "Invalid data received", data: str | None = None): super().__init__(message) self.data = data diff --git a/src/linkplay/utils.py b/src/linkplay/utils.py index bb45573..8a22c91 100644 --- a/src/linkplay/utils.py +++ b/src/linkplay/utils.py @@ -10,20 +10,25 @@ import aiofiles import async_timeout -from aiohttp import ClientError, ClientSession, TCPConnector +from aiohttp import ClientError, ClientSession, ClientTimeout, TCPConnector from appdirs import AppDirs from deprecated import deprecated from linkplay.consts import ( API_ENDPOINT, API_TIMEOUT, + LOGGER, MTLS_CERTIFICATE_CONTENTS, TCP_MESSAGE_LENGTH, EqualizerMode, PlayerAttribute, PlayingStatus, ) -from linkplay.exceptions import LinkPlayInvalidDataException, LinkPlayRequestException +from linkplay.exceptions import ( + LinkPlayInvalidDataException, + LinkPlayRequestCancelledException, + LinkPlayRequestException, +) async def session_call_api(endpoint: str, session: ClientSession, command: str) -> str: @@ -45,18 +50,29 @@ async def session_call_api(endpoint: str, session: ClientSession, command: str) try: async with async_timeout.timeout(API_TIMEOUT): response = await session.get(url) - - except (asyncio.TimeoutError, ClientError, asyncio.CancelledError) as error: + if response.status != HTTPStatus.OK: + raise LinkPlayRequestException( + f"Unexpected HTTPStatus {response.status} received from '{url}'" + ) + return await response.text() + + except ClientError as error: + LOGGER.warning("ClientError for %s: %s", url, error) raise LinkPlayRequestException( f"{error} error requesting data from '{url}'" ) from error - if response.status != HTTPStatus.OK: + except asyncio.TimeoutError as error: + LOGGER.warning("Timeout for %s: %s", url, error) raise LinkPlayRequestException( - f"Unexpected HTTPStatus {response.status} received from '{url}'" - ) + f"{error} error requesting data from '{url}'" + ) from error - return await response.text() + except asyncio.CancelledError as error: + LOGGER.warning("Cancelled for %s: %s", url, error) + raise LinkPlayRequestCancelledException( + f"{error} error requesting data from '{url}'" + ) from error async def session_call_api_json( @@ -81,6 +97,7 @@ async def session_call_api_json( return json.loads(result) # type: ignore except json.JSONDecodeError as jsonexc: url = API_ENDPOINT.format(endpoint, command) + LOGGER.warning("Unexpected json for %s: %s", url, jsonexc) raise LinkPlayInvalidDataException( message=f"Unexpected JSON ({result}) received from '{url}'", data=result ) from jsonexc