1010
1111import aiofiles
1212import async_timeout
13- from aiohttp import ClientError , ClientSession , TCPConnector
13+ from aiohttp import ClientError , ClientSession , ClientTimeout , TCPConnector
1414from appdirs import AppDirs
1515from deprecated import deprecated
1616
1717from 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
2934async 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
6278async 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