pyhOn/pyhon/connection/handler.py

134 lines
4.5 KiB
Python
Raw Normal View History

2023-04-09 12:50:28 -06:00
import json
2023-04-09 10:13:50 -06:00
from contextlib import asynccontextmanager
import aiohttp
from pyhon import const
from pyhon.connection.auth import HonAuth, _LOGGER
from pyhon.connection.device import HonDevice
class HonBaseConnectionHandler:
_HEADERS = {"user-agent": const.USER_AGENT, "Content-Type": "application/json"}
def __init__(self):
self._session = None
self._auth = None
async def __aenter__(self):
2023-04-09 12:50:28 -06:00
return await self.create()
2023-04-09 10:13:50 -06:00
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.close()
async def create(self):
self._session = aiohttp.ClientSession(headers=self._HEADERS)
2023-04-09 12:50:28 -06:00
return self
2023-04-09 10:13:50 -06:00
@asynccontextmanager
async def get(self, *args, **kwargs):
2023-04-09 15:47:33 -06:00
raise NotImplementedError
2023-04-09 10:13:50 -06:00
@asynccontextmanager
async def post(self, *args, **kwargs):
2023-04-09 15:47:33 -06:00
raise NotImplementedError
2023-04-09 10:13:50 -06:00
async def close(self):
await self._session.close()
class HonConnectionHandler(HonBaseConnectionHandler):
def __init__(self, email, password):
super().__init__()
self._device = HonDevice()
self._email = email
self._password = password
if not self._email:
raise PermissionError("Login-Error - An email address must be specified")
if not self._password:
raise PermissionError("Login-Error - A password address must be specified")
self._request_headers = {}
@property
def device(self):
return self._device
async def create(self):
await super().create()
self._auth = HonAuth(self._session, self._email, self._password, self._device)
2023-04-09 12:50:28 -06:00
return self
2023-04-09 10:13:50 -06:00
async def _check_headers(self, headers):
2023-04-09 12:55:36 -06:00
if (
"cognito-token" not in self._request_headers
or "id-token" not in self._request_headers
):
2023-04-09 10:13:50 -06:00
if await self._auth.authorize():
self._request_headers["cognito-token"] = self._auth.cognito_token
self._request_headers["id-token"] = self._auth.id_token
else:
raise PermissionError("Can't Login")
return {h: v for h, v in self._request_headers.items() if h not in headers}
@asynccontextmanager
2023-04-09 10:43:57 -06:00
async def _intercept(self, method, *args, loop=0, **kwargs):
2023-04-09 10:13:50 -06:00
kwargs["headers"] = await self._check_headers(kwargs.get("headers", {}))
2023-04-09 10:43:57 -06:00
async with method(*args, **kwargs) as response:
2023-04-09 10:13:50 -06:00
if response.status == 403 and not loop:
2023-04-09 10:43:57 -06:00
_LOGGER.info("Try refreshing token...")
await self._auth.refresh()
yield await self._intercept(method, *args, loop=loop + 1, **kwargs)
elif response.status == 403 and loop < 2:
2023-04-09 12:55:36 -06:00
_LOGGER.warning(
"%s - Error %s - %s",
response.request_info.url,
response.status,
await response.text(),
)
2023-04-09 10:13:50 -06:00
await self.create()
2023-04-09 10:43:57 -06:00
yield await self._intercept(method, *args, loop=loop + 1, **kwargs)
2023-04-09 10:13:50 -06:00
elif loop >= 2:
2023-04-09 12:55:36 -06:00
_LOGGER.error(
"%s - Error %s - %s",
response.request_info.url,
response.status,
await response.text(),
)
2023-04-09 10:43:57 -06:00
raise PermissionError("Login failure")
2023-04-09 10:13:50 -06:00
else:
2023-04-09 12:50:28 -06:00
try:
await response.json()
yield response
except json.JSONDecodeError:
2023-04-09 12:55:36 -06:00
_LOGGER.warning(
"%s - JsonDecodeError %s - %s",
response.request_info.url,
response.status,
await response.text(),
)
2023-04-09 12:50:28 -06:00
yield {}
2023-04-09 10:13:50 -06:00
2023-04-09 10:43:57 -06:00
@asynccontextmanager
async def get(self, *args, **kwargs):
async with self._intercept(self._session.get, *args, **kwargs) as response:
yield response
2023-04-09 10:13:50 -06:00
@asynccontextmanager
async def post(self, *args, **kwargs):
2023-04-09 10:43:57 -06:00
async with self._intercept(self._session.post, *args, **kwargs) as response:
2023-04-09 10:13:50 -06:00
yield response
class HonAnonymousConnectionHandler(HonBaseConnectionHandler):
_HEADERS = HonBaseConnectionHandler._HEADERS | {"x-api-key": const.API_KEY}
@asynccontextmanager
async def get(self, *args, **kwargs):
async with self._session.post(*args, **kwargs) as response:
yield response
@asynccontextmanager
async def post(self, *args, **kwargs):
async with self._session.post(*args, **kwargs) as response:
yield response