pyh0n/pyhon/connection/handler/hon.py

106 lines
3.9 KiB
Python
Raw Normal View History

2023-04-10 00:20:28 +05:30
import json
2023-04-15 17:52:04 +05:30
import logging
from collections.abc import AsyncIterator
2023-04-09 21:43:50 +05:30
from contextlib import asynccontextmanager
2023-06-28 22:32:11 +05:30
from typing import Optional, Dict, Any
2023-04-09 21:43:50 +05:30
import aiohttp
2023-04-15 17:52:04 +05:30
from typing_extensions import Self
2023-06-28 23:55:52 +05:30
from yarl import URL
2023-04-09 21:43:50 +05:30
2023-04-15 17:52:04 +05:30
from pyhon.connection.auth import HonAuth
2023-04-09 21:43:50 +05:30
from pyhon.connection.device import HonDevice
2023-04-15 17:52:04 +05:30
from pyhon.connection.handler.base import ConnectionHandler
2023-04-15 19:25:22 +05:30
from pyhon.exceptions import HonAuthenticationError, NoAuthenticationException
2023-06-28 22:32:11 +05:30
from pyhon.typedefs import Callback
2023-04-09 21:43:50 +05:30
2023-04-15 17:52:04 +05:30
_LOGGER = logging.getLogger(__name__)
2023-04-09 21:43:50 +05:30
2023-04-15 17:52:04 +05:30
class HonConnectionHandler(ConnectionHandler):
2023-04-14 02:55:49 +05:30
def __init__(
self, email: str, password: str, session: Optional[aiohttp.ClientSession] = None
) -> None:
2023-04-10 10:04:19 +05:30
super().__init__(session=session)
2023-04-14 02:55:49 +05:30
self._device: HonDevice = HonDevice()
self._email: str = email
self._password: str = password
2023-04-09 21:43:50 +05:30
if not self._email:
raise HonAuthenticationError("An email address must be specified")
2023-04-09 21:43:50 +05:30
if not self._password:
raise HonAuthenticationError("A password address must be specified")
2023-04-15 17:52:04 +05:30
self._auth: Optional[HonAuth] = None
@property
2023-04-15 19:25:22 +05:30
def auth(self) -> HonAuth:
if self._auth is None:
raise NoAuthenticationException()
2023-04-15 17:52:04 +05:30
return self._auth
2023-04-09 21:43:50 +05:30
@property
2023-04-14 02:55:49 +05:30
def device(self) -> HonDevice:
2023-04-09 21:43:50 +05:30
return self._device
2023-04-14 02:55:49 +05:30
async def create(self) -> Self:
2023-04-09 21:43:50 +05:30
await super().create()
2023-06-28 22:32:11 +05:30
self._auth = HonAuth(self.session, self._email, self._password, self._device)
2023-04-10 00:20:28 +05:30
return self
2023-04-09 21:43:50 +05:30
2023-06-28 22:32:11 +05:30
async def _check_headers(self, headers: Dict[str, str]) -> Dict[str, str]:
2023-04-15 19:25:22 +05:30
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 22:44:14 +05:30
return self._HEADERS | headers
2023-04-09 21:43:50 +05:30
@asynccontextmanager
2023-04-14 02:55:49 +05:30
async def _intercept(
2023-06-28 23:55:52 +05:30
self, method: Callback, url: str | URL, *args: Any, **kwargs: Any
2023-06-28 22:32:11 +05:30
) -> AsyncIterator[aiohttp.ClientResponse]:
2023-06-28 23:55:52 +05:30
loop: int = kwargs.get("loop", 0)
2023-04-09 21:43:50 +05:30
kwargs["headers"] = await self._check_headers(kwargs.get("headers", {}))
2023-06-28 23:55:52 +05:30
async with method(url, *args, **kwargs) as response:
if (
2023-04-15 19:25:22 +05:30
self.auth.token_expires_soon or response.status in [401, 403]
) and loop == 0:
2023-04-09 22:13:57 +05:30
_LOGGER.info("Try refreshing token...")
2023-04-15 19:25:22 +05:30
await self.auth.refresh()
2023-06-28 23:55:52 +05:30
async with self._intercept(
method, url, *args, loop=loop + 1, **kwargs
) as result:
2023-04-11 20:39:02 +05:30
yield result
elif (
2023-04-15 19:25:22 +05:30
self.auth.token_is_expired or response.status in [401, 403]
) and loop == 1:
2023-04-10 00:25:36 +05:30
_LOGGER.warning(
"%s - Error %s - %s",
response.request_info.url,
response.status,
await response.text(),
)
2023-04-09 21:43:50 +05:30
await self.create()
2023-06-28 23:55:52 +05:30
async with self._intercept(
method, url, *args, loop=loop + 1, **kwargs
) as result:
2023-04-11 20:39:02 +05:30
yield result
2023-04-09 21:43:50 +05:30
elif loop >= 2:
2023-04-10 00:25:36 +05:30
_LOGGER.error(
"%s - Error %s - %s",
response.request_info.url,
response.status,
await response.text(),
)
raise HonAuthenticationError("Login failure")
2023-04-09 21:43:50 +05:30
else:
2023-04-10 00:20:28 +05:30
try:
await response.json()
yield response
except json.JSONDecodeError:
2023-04-10 00:25:36 +05:30
_LOGGER.warning(
"%s - JsonDecodeError %s - %s",
response.request_info.url,
response.status,
await response.text(),
)
2023-04-14 02:55:49 +05:30
raise HonAuthenticationError("Decode Error")