2023-04-09 20:50:28 +02:00
|
|
|
import json
|
2023-04-13 23:25:49 +02:00
|
|
|
from collections.abc import Generator, AsyncIterator, Coroutine
|
2023-04-09 18:13:50 +02:00
|
|
|
from contextlib import asynccontextmanager
|
2023-04-13 23:25:49 +02:00
|
|
|
from typing import Optional, Callable, Dict
|
|
|
|
from typing_extensions import Self
|
2023-04-09 18:13:50 +02:00
|
|
|
|
|
|
|
import aiohttp
|
|
|
|
|
2023-04-13 23:25:49 +02:00
|
|
|
from pyhon import const, exceptions
|
2023-04-09 18:13:50 +02:00
|
|
|
from pyhon.connection.auth import HonAuth, _LOGGER
|
|
|
|
from pyhon.connection.device import HonDevice
|
2023-04-11 00:59:00 +02:00
|
|
|
from pyhon.exceptions import HonAuthenticationError
|
2023-04-09 18:13:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
class HonBaseConnectionHandler:
|
2023-04-13 23:25:49 +02:00
|
|
|
_HEADERS: Dict = {
|
|
|
|
"user-agent": const.USER_AGENT,
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
}
|
2023-04-09 18:13:50 +02:00
|
|
|
|
2023-04-13 23:25:49 +02:00
|
|
|
def __init__(self, session: Optional[aiohttp.ClientSession] = None) -> None:
|
|
|
|
self._create_session: bool = session is None
|
|
|
|
self._session: Optional[aiohttp.ClientSession] = session
|
|
|
|
self._auth: Optional[HonAuth] = None
|
2023-04-09 18:13:50 +02:00
|
|
|
|
2023-04-13 23:25:49 +02:00
|
|
|
async def __aenter__(self) -> Self:
|
2023-04-09 20:50:28 +02:00
|
|
|
return await self.create()
|
2023-04-09 18:13:50 +02:00
|
|
|
|
2023-04-13 23:25:49 +02:00
|
|
|
async def __aexit__(self, exc_type, exc_val, exc_tb) -> None:
|
2023-04-09 18:13:50 +02:00
|
|
|
await self.close()
|
|
|
|
|
2023-04-13 23:25:49 +02:00
|
|
|
@property
|
|
|
|
def auth(self) -> Optional[HonAuth]:
|
|
|
|
return self._auth
|
|
|
|
|
|
|
|
async def create(self) -> Self:
|
2023-04-12 02:09:41 +02:00
|
|
|
if self._create_session:
|
|
|
|
self._session = aiohttp.ClientSession()
|
2023-04-09 20:50:28 +02:00
|
|
|
return self
|
2023-04-09 18:13:50 +02:00
|
|
|
|
|
|
|
@asynccontextmanager
|
2023-04-13 23:25:49 +02:00
|
|
|
def _intercept(self, method: Callable, *args, loop: int = 0, **kwargs):
|
2023-04-09 23:47:33 +02:00
|
|
|
raise NotImplementedError
|
2023-04-09 18:13:50 +02:00
|
|
|
|
2023-04-10 06:34:19 +02:00
|
|
|
@asynccontextmanager
|
2023-04-13 23:25:49 +02:00
|
|
|
async def get(self, *args, **kwargs) -> AsyncIterator[Callable]:
|
|
|
|
if self._session is None:
|
|
|
|
raise exceptions.NoSessionException()
|
|
|
|
response: Callable
|
2023-04-10 06:34:19 +02:00
|
|
|
async with self._intercept(self._session.get, *args, **kwargs) as response:
|
|
|
|
yield response
|
|
|
|
|
2023-04-09 18:13:50 +02:00
|
|
|
@asynccontextmanager
|
2023-04-13 23:25:49 +02:00
|
|
|
async def post(self, *args, **kwargs) -> AsyncIterator[Callable]:
|
|
|
|
if self._session is None:
|
|
|
|
raise exceptions.NoSessionException()
|
|
|
|
response: Callable
|
2023-04-10 06:34:19 +02:00
|
|
|
async with self._intercept(self._session.post, *args, **kwargs) as response:
|
|
|
|
yield response
|
2023-04-09 18:13:50 +02:00
|
|
|
|
2023-04-13 23:25:49 +02:00
|
|
|
async def close(self) -> None:
|
|
|
|
if self._create_session and self._session is not None:
|
2023-04-12 02:09:41 +02:00
|
|
|
await self._session.close()
|
2023-04-09 18:13:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
class HonConnectionHandler(HonBaseConnectionHandler):
|
2023-04-13 23:25:49 +02:00
|
|
|
def __init__(
|
|
|
|
self, email: str, password: str, session: Optional[aiohttp.ClientSession] = None
|
|
|
|
) -> None:
|
2023-04-10 06:34:19 +02:00
|
|
|
super().__init__(session=session)
|
2023-04-13 23:25:49 +02:00
|
|
|
self._device: HonDevice = HonDevice()
|
|
|
|
self._email: str = email
|
|
|
|
self._password: str = password
|
2023-04-09 18:13:50 +02:00
|
|
|
if not self._email:
|
2023-04-11 00:59:00 +02:00
|
|
|
raise HonAuthenticationError("An email address must be specified")
|
2023-04-09 18:13:50 +02:00
|
|
|
if not self._password:
|
2023-04-11 00:59:00 +02:00
|
|
|
raise HonAuthenticationError("A password address must be specified")
|
2023-04-09 18:13:50 +02:00
|
|
|
|
|
|
|
@property
|
2023-04-13 23:25:49 +02:00
|
|
|
def device(self) -> HonDevice:
|
2023-04-09 18:13:50 +02:00
|
|
|
return self._device
|
|
|
|
|
2023-04-13 23:25:49 +02:00
|
|
|
async def create(self) -> Self:
|
2023-04-09 18:13:50 +02:00
|
|
|
await super().create()
|
2023-04-13 23:25:49 +02:00
|
|
|
self._auth: HonAuth = HonAuth(
|
|
|
|
self._session, self._email, self._password, self._device
|
|
|
|
)
|
2023-04-09 20:50:28 +02:00
|
|
|
return self
|
2023-04-09 18:13:50 +02:00
|
|
|
|
2023-04-13 23:25:49 +02:00
|
|
|
async def _check_headers(self, headers: Dict) -> Dict:
|
2023-04-12 19:14:14 +02:00
|
|
|
if not (self._auth.cognito_token and self._auth.id_token):
|
|
|
|
await self._auth.authenticate()
|
|
|
|
headers["cognito-token"] = self._auth.cognito_token
|
|
|
|
headers["id-token"] = self._auth.id_token
|
|
|
|
return self._HEADERS | headers
|
2023-04-09 18:13:50 +02:00
|
|
|
|
|
|
|
@asynccontextmanager
|
2023-04-13 23:25:49 +02:00
|
|
|
async def _intercept(
|
|
|
|
self, method: Callable, *args, loop: int = 0, **kwargs
|
|
|
|
) -> AsyncIterator:
|
2023-04-09 18:13:50 +02:00
|
|
|
kwargs["headers"] = await self._check_headers(kwargs.get("headers", {}))
|
2023-04-09 18:43:57 +02:00
|
|
|
async with method(*args, **kwargs) as response:
|
2023-04-14 23:15:07 +02:00
|
|
|
if (
|
|
|
|
self._auth.token_expires_soon or response.status in [401, 403]
|
|
|
|
) and loop == 0:
|
2023-04-09 18:43:57 +02:00
|
|
|
_LOGGER.info("Try refreshing token...")
|
|
|
|
await self._auth.refresh()
|
2023-04-11 22:14:36 +02:00
|
|
|
async with self._intercept(
|
|
|
|
method, *args, loop=loop + 1, **kwargs
|
|
|
|
) as result:
|
2023-04-11 17:09:02 +02:00
|
|
|
yield result
|
2023-04-14 23:15:07 +02:00
|
|
|
elif (
|
|
|
|
self._auth.token_is_expired or response.status in [401, 403]
|
|
|
|
) and loop == 1:
|
2023-04-09 20:55:36 +02:00
|
|
|
_LOGGER.warning(
|
|
|
|
"%s - Error %s - %s",
|
|
|
|
response.request_info.url,
|
|
|
|
response.status,
|
|
|
|
await response.text(),
|
|
|
|
)
|
2023-04-09 18:13:50 +02:00
|
|
|
await self.create()
|
2023-04-11 22:14:36 +02:00
|
|
|
async with self._intercept(
|
|
|
|
method, *args, loop=loop + 1, **kwargs
|
|
|
|
) as result:
|
2023-04-11 17:09:02 +02:00
|
|
|
yield result
|
2023-04-09 18:13:50 +02:00
|
|
|
elif loop >= 2:
|
2023-04-09 20:55:36 +02:00
|
|
|
_LOGGER.error(
|
|
|
|
"%s - Error %s - %s",
|
|
|
|
response.request_info.url,
|
|
|
|
response.status,
|
|
|
|
await response.text(),
|
|
|
|
)
|
2023-04-11 00:59:00 +02:00
|
|
|
raise HonAuthenticationError("Login failure")
|
2023-04-09 18:13:50 +02:00
|
|
|
else:
|
2023-04-09 20:50:28 +02:00
|
|
|
try:
|
|
|
|
await response.json()
|
|
|
|
yield response
|
|
|
|
except json.JSONDecodeError:
|
2023-04-09 20:55:36 +02:00
|
|
|
_LOGGER.warning(
|
|
|
|
"%s - JsonDecodeError %s - %s",
|
|
|
|
response.request_info.url,
|
|
|
|
response.status,
|
|
|
|
await response.text(),
|
|
|
|
)
|
2023-04-13 23:25:49 +02:00
|
|
|
raise HonAuthenticationError("Decode Error")
|
2023-04-09 18:13:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
class HonAnonymousConnectionHandler(HonBaseConnectionHandler):
|
2023-04-13 23:25:49 +02:00
|
|
|
_HEADERS: Dict = HonBaseConnectionHandler._HEADERS | {"x-api-key": const.API_KEY}
|
2023-04-09 18:13:50 +02:00
|
|
|
|
|
|
|
@asynccontextmanager
|
2023-04-13 23:25:49 +02:00
|
|
|
async def _intercept(
|
|
|
|
self, method: Callable, *args, loop: int = 0, **kwargs
|
|
|
|
) -> AsyncIterator:
|
2023-04-10 06:34:19 +02:00
|
|
|
kwargs["headers"] = kwargs.pop("headers", {}) | self._HEADERS
|
|
|
|
async with method(*args, **kwargs) as response:
|
|
|
|
if response.status == 403:
|
2023-04-11 00:59:00 +02:00
|
|
|
_LOGGER.error("Can't authenticate anymore")
|
2023-04-09 18:13:50 +02:00
|
|
|
yield response
|