Skip to content

Commit 68da621

Browse files
authored
Updating request handling (#119)
1 parent 6432217 commit 68da621

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

src/linkplay/exceptions.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1+
"""This module defines custom exceptions for the LinkPlay integration."""
2+
3+
14
class LinkPlayException(Exception):
2-
pass
5+
"""Base exception for LinkPlay errors."""
36

47

58
class LinkPlayRequestException(LinkPlayException):
6-
pass
9+
"""Exception raised for errors in LinkPlay requests."""
10+
11+
12+
class LinkPlayRequestCancelledException(LinkPlayException):
13+
"""Exception raised when a LinkPlay request is cancelled."""
714

815

916
class LinkPlayInvalidDataException(LinkPlayException):
17+
"""Exception raised for invalid data received from LinkPlay endpoints."""
18+
1019
def __init__(self, message: str = "Invalid data received", data: str | None = None):
1120
super().__init__(message)
1221
self.data = data

src/linkplay/utils.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,25 @@
1010

1111
import aiofiles
1212
import async_timeout
13-
from aiohttp import ClientError, ClientSession, TCPConnector
13+
from aiohttp import ClientError, ClientSession, ClientTimeout, TCPConnector
1414
from appdirs import AppDirs
1515
from deprecated import deprecated
1616

1717
from linkplay.consts import (
1818
API_ENDPOINT,
1919
API_TIMEOUT,
20+
LOGGER,
2021
MTLS_CERTIFICATE_CONTENTS,
2122
TCP_MESSAGE_LENGTH,
2223
EqualizerMode,
2324
PlayerAttribute,
2425
PlayingStatus,
2526
)
26-
from linkplay.exceptions import LinkPlayInvalidDataException, LinkPlayRequestException
27+
from linkplay.exceptions import (
28+
LinkPlayInvalidDataException,
29+
LinkPlayRequestCancelledException,
30+
LinkPlayRequestException,
31+
)
2732

2833

2934
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)
4550
try:
4651
async with async_timeout.timeout(API_TIMEOUT):
4752
response = await session.get(url)
48-
49-
except (asyncio.TimeoutError, ClientError, asyncio.CancelledError) as error:
53+
if response.status != HTTPStatus.OK:
54+
raise LinkPlayRequestException(
55+
f"Unexpected HTTPStatus {response.status} received from '{url}'"
56+
)
57+
return await response.text()
58+
59+
except ClientError as error:
60+
LOGGER.warning("ClientError for %s: %s", url, error)
5061
raise LinkPlayRequestException(
5162
f"{error} error requesting data from '{url}'"
5263
) from error
5364

54-
if response.status != HTTPStatus.OK:
65+
except asyncio.TimeoutError as error:
66+
LOGGER.warning("Timeout for %s: %s", url, error)
5567
raise LinkPlayRequestException(
56-
f"Unexpected HTTPStatus {response.status} received from '{url}'"
57-
)
68+
f"{error} error requesting data from '{url}'"
69+
) from error
5870

59-
return await response.text()
71+
except asyncio.CancelledError as error:
72+
LOGGER.warning("Cancelled for %s: %s", url, error)
73+
raise LinkPlayRequestCancelledException(
74+
f"{error} error requesting data from '{url}'"
75+
) from error
6076

6177

6278
async def session_call_api_json(
@@ -81,6 +97,7 @@ async def session_call_api_json(
8197
return json.loads(result) # type: ignore
8298
except json.JSONDecodeError as jsonexc:
8399
url = API_ENDPOINT.format(endpoint, command)
100+
LOGGER.warning("Unexpected json for %s: %s", url, jsonexc)
84101
raise LinkPlayInvalidDataException(
85102
message=f"Unexpected JSON ({result}) received from '{url}'", data=result
86103
) from jsonexc

0 commit comments

Comments
 (0)