|
|
|
@ -6,6 +6,7 @@ from typing import Optional, Dict, Any
|
|
|
|
|
|
|
|
|
|
import aiohttp
|
|
|
|
|
from typing_extensions import Self
|
|
|
|
|
from yarl import URL
|
|
|
|
|
|
|
|
|
|
from pyhon.connection.auth import HonAuth
|
|
|
|
|
from pyhon.connection.device import HonDevice
|
|
|
|
@ -54,16 +55,19 @@ class HonConnectionHandler(ConnectionHandler):
|
|
|
|
|
|
|
|
|
|
@asynccontextmanager
|
|
|
|
|
async def _intercept(
|
|
|
|
|
self, method: Callback, *args: Any, loop: int = 0, **kwargs: Dict[str, str]
|
|
|
|
|
self, method: Callback, url: str | URL, *args: Any, **kwargs: Any
|
|
|
|
|
) -> AsyncIterator[aiohttp.ClientResponse]:
|
|
|
|
|
loop: int = kwargs.get("loop", 0)
|
|
|
|
|
kwargs["headers"] = await self._check_headers(kwargs.get("headers", {}))
|
|
|
|
|
async with method(args[0], *args[1:], **kwargs) as response:
|
|
|
|
|
async with method(url, *args, **kwargs) as response:
|
|
|
|
|
if (
|
|
|
|
|
self.auth.token_expires_soon or response.status in [401, 403]
|
|
|
|
|
) and loop == 0:
|
|
|
|
|
_LOGGER.info("Try refreshing token...")
|
|
|
|
|
await self.auth.refresh()
|
|
|
|
|
async with self._intercept(method, loop=loop + 1, **kwargs) as result:
|
|
|
|
|
async with self._intercept(
|
|
|
|
|
method, url, *args, loop=loop + 1, **kwargs
|
|
|
|
|
) as result:
|
|
|
|
|
yield result
|
|
|
|
|
elif (
|
|
|
|
|
self.auth.token_is_expired or response.status in [401, 403]
|
|
|
|
@ -75,7 +79,9 @@ class HonConnectionHandler(ConnectionHandler):
|
|
|
|
|
await response.text(),
|
|
|
|
|
)
|
|
|
|
|
await self.create()
|
|
|
|
|
async with self._intercept(method, loop=loop + 1, **kwargs) as result:
|
|
|
|
|
async with self._intercept(
|
|
|
|
|
method, url, *args, loop=loop + 1, **kwargs
|
|
|
|
|
) as result:
|
|
|
|
|
yield result
|
|
|
|
|
elif loop >= 2:
|
|
|
|
|
_LOGGER.error(
|
|
|
|
|