pyhOn/pyhon/connection/handler/hon.py

119 lines
4.2 KiB
Python
Raw Normal View History

2023-04-09 20:50:28 +02:00
import json
2023-04-15 14:22:04 +02:00
import logging
from collections.abc import AsyncIterator
2023-04-09 18:13:50 +02:00
from contextlib import asynccontextmanager
2023-06-28 19:02:11 +02:00
from typing import Optional, Dict, Any
2023-04-09 18:13:50 +02:00
import aiohttp
2023-04-15 14:22:04 +02:00
from typing_extensions import Self
2023-06-28 20:25:52 +02:00
from yarl import URL
2023-04-09 18:13:50 +02:00
2023-04-15 14:22:04 +02:00
from pyhon.connection.auth import HonAuth
2023-04-09 18:13:50 +02:00
from pyhon.connection.device import HonDevice
2023-04-15 14:22:04 +02:00
from pyhon.connection.handler.base import ConnectionHandler
2023-04-15 15:55:22 +02:00
from pyhon.exceptions import HonAuthenticationError, NoAuthenticationException
2023-06-28 19:02:11 +02:00
from pyhon.typedefs import Callback
2023-04-09 18:13:50 +02:00
2023-04-15 14:22:04 +02:00
_LOGGER = logging.getLogger(__name__)
2023-04-09 18:13:50 +02:00
2023-04-15 14:22:04 +02:00
class HonConnectionHandler(ConnectionHandler):
2023-04-13 23:25:49 +02:00
def __init__(
2024-02-09 20:37:53 +01:00
self,
email: str,
password: str,
2024-02-11 02:02:47 +01:00
session: Optional[aiohttp.ClientSession] = None,
2024-02-09 20:37:53 +01:00
mobile_id: str = "",
2024-02-10 00:59:14 +01:00
refresh_token: str = "",
2023-04-13 23:25:49 +02:00
) -> None:
2023-04-10 06:34:19 +02:00
super().__init__(session=session)
2024-02-09 20:37:53 +01:00
self._device: HonDevice = HonDevice(mobile_id)
2023-04-13 23:25:49 +02:00
self._email: str = email
self._password: str = password
2024-02-10 00:59:14 +01:00
self._refresh_token: str = refresh_token
2023-04-09 18:13:50 +02:00
if not self._email:
raise HonAuthenticationError("An email address must be specified")
2023-04-09 18:13:50 +02:00
if not self._password:
raise HonAuthenticationError("A password address must be specified")
2023-04-15 14:22:04 +02:00
self._auth: Optional[HonAuth] = None
@property
2023-04-15 15:55:22 +02:00
def auth(self) -> HonAuth:
if self._auth is None:
raise NoAuthenticationException()
2023-04-15 14:22:04 +02:00
return self._auth
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()
2024-02-10 00:59:14 +01:00
self._auth = 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-06-28 19:02:11 +02:00
async def _check_headers(self, headers: Dict[str, str]) -> Dict[str, str]:
2024-02-11 02:02:47 +01:00
if self._refresh_token:
await self.auth.refresh(self._refresh_token)
2023-04-15 15:55:22 +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
2023-04-12 19:14:14 +02:00
return self._HEADERS | headers
2023-04-09 18:13:50 +02:00
@asynccontextmanager
2023-04-13 23:25:49 +02:00
async def _intercept(
2023-06-28 20:25:52 +02:00
self, method: Callback, url: str | URL, *args: Any, **kwargs: Any
2023-06-28 19:02:11 +02:00
) -> AsyncIterator[aiohttp.ClientResponse]:
2023-06-29 22:08:17 +02:00
loop: int = kwargs.pop("loop", 0)
2023-04-09 18:13:50 +02:00
kwargs["headers"] = await self._check_headers(kwargs.get("headers", {}))
2023-06-28 20:25:52 +02:00
async with method(url, *args, **kwargs) as response:
if (
2023-04-15 15:55:22 +02:00
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...")
2023-04-15 15:55:22 +02:00
await self.auth.refresh()
2023-06-28 20:25:52 +02:00
async with self._intercept(
method, url, *args, loop=loop + 1, **kwargs
) as result:
2023-04-11 17:09:02 +02:00
yield result
elif (
2023-04-15 15:55:22 +02:00
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-06-28 20:25:52 +02:00
async with self._intercept(
method, url, *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(),
)
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
2023-07-12 19:36:32 +02:00
except json.JSONDecodeError as exc:
2023-04-09 20:55:36 +02:00
_LOGGER.warning(
"%s - JsonDecodeError %s - %s",
response.request_info.url,
response.status,
await response.text(),
)
2023-07-12 19:36:32 +02:00
raise HonAuthenticationError("Decode Error") from exc