mirror of
https://github.com/Andre0512/pyhOn.git
synced 2025-05-14 15:14:28 -04:00
Add some type hints
This commit is contained in:
parent
970b94bfa7
commit
e82c14ec99
6 changed files with 177 additions and 97 deletions
|
@ -1,73 +1,93 @@
|
|||
import json
|
||||
from collections.abc import Generator, AsyncIterator, Coroutine
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import Optional, Callable, Dict
|
||||
from typing_extensions import Self
|
||||
|
||||
import aiohttp
|
||||
|
||||
from pyhon import const
|
||||
from pyhon import const, exceptions
|
||||
from pyhon.connection.auth import HonAuth, _LOGGER
|
||||
from pyhon.connection.device import HonDevice
|
||||
from pyhon.exceptions import HonAuthenticationError
|
||||
|
||||
|
||||
class HonBaseConnectionHandler:
|
||||
_HEADERS = {"user-agent": const.USER_AGENT, "Content-Type": "application/json"}
|
||||
_HEADERS: Dict = {
|
||||
"user-agent": const.USER_AGENT,
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
def __init__(self, session=None):
|
||||
self._create_session = session is None
|
||||
self._session = session
|
||||
self._auth = None
|
||||
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
|
||||
|
||||
async def __aenter__(self):
|
||||
async def __aenter__(self) -> Self:
|
||||
return await self.create()
|
||||
|
||||
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
||||
async def __aexit__(self, exc_type, exc_val, exc_tb) -> None:
|
||||
await self.close()
|
||||
|
||||
async def create(self):
|
||||
@property
|
||||
def auth(self) -> Optional[HonAuth]:
|
||||
return self._auth
|
||||
|
||||
async def create(self) -> Self:
|
||||
if self._create_session:
|
||||
self._session = aiohttp.ClientSession()
|
||||
return self
|
||||
|
||||
@asynccontextmanager
|
||||
async def _intercept(self, method, *args, loop=0, **kwargs):
|
||||
def _intercept(self, method: Callable, *args, loop: int = 0, **kwargs):
|
||||
raise NotImplementedError
|
||||
|
||||
@asynccontextmanager
|
||||
async def get(self, *args, **kwargs):
|
||||
async def get(self, *args, **kwargs) -> AsyncIterator[Callable]:
|
||||
if self._session is None:
|
||||
raise exceptions.NoSessionException()
|
||||
response: Callable
|
||||
async with self._intercept(self._session.get, *args, **kwargs) as response:
|
||||
yield response
|
||||
|
||||
@asynccontextmanager
|
||||
async def post(self, *args, **kwargs):
|
||||
async def post(self, *args, **kwargs) -> AsyncIterator[Callable]:
|
||||
if self._session is None:
|
||||
raise exceptions.NoSessionException()
|
||||
response: Callable
|
||||
async with self._intercept(self._session.post, *args, **kwargs) as response:
|
||||
yield response
|
||||
|
||||
async def close(self):
|
||||
if self._create_session:
|
||||
async def close(self) -> None:
|
||||
if self._create_session and self._session is not None:
|
||||
await self._session.close()
|
||||
|
||||
|
||||
class HonConnectionHandler(HonBaseConnectionHandler):
|
||||
def __init__(self, email, password, session=None):
|
||||
def __init__(
|
||||
self, email: str, password: str, session: Optional[aiohttp.ClientSession] = None
|
||||
) -> None:
|
||||
super().__init__(session=session)
|
||||
self._device = HonDevice()
|
||||
self._email = email
|
||||
self._password = password
|
||||
self._device: HonDevice = HonDevice()
|
||||
self._email: str = email
|
||||
self._password: str = password
|
||||
if not self._email:
|
||||
raise HonAuthenticationError("An email address must be specified")
|
||||
if not self._password:
|
||||
raise HonAuthenticationError("A password address must be specified")
|
||||
|
||||
@property
|
||||
def device(self):
|
||||
def device(self) -> HonDevice:
|
||||
return self._device
|
||||
|
||||
async def create(self):
|
||||
async def create(self) -> Self:
|
||||
await super().create()
|
||||
self._auth = HonAuth(self._session, self._email, self._password, self._device)
|
||||
self._auth: HonAuth = HonAuth(
|
||||
self._session, self._email, self._password, self._device
|
||||
)
|
||||
return self
|
||||
|
||||
async def _check_headers(self, headers):
|
||||
async def _check_headers(self, headers: Dict) -> Dict:
|
||||
if not (self._auth.cognito_token and self._auth.id_token):
|
||||
await self._auth.authenticate()
|
||||
headers["cognito-token"] = self._auth.cognito_token
|
||||
|
@ -75,7 +95,9 @@ class HonConnectionHandler(HonBaseConnectionHandler):
|
|||
return self._HEADERS | headers
|
||||
|
||||
@asynccontextmanager
|
||||
async def _intercept(self, method, *args, loop=0, **kwargs):
|
||||
async def _intercept(
|
||||
self, method: Callable, *args, loop: int = 0, **kwargs
|
||||
) -> AsyncIterator:
|
||||
kwargs["headers"] = await self._check_headers(kwargs.get("headers", {}))
|
||||
async with method(*args, **kwargs) as response:
|
||||
if response.status in [401, 403] and loop == 0:
|
||||
|
@ -116,14 +138,16 @@ class HonConnectionHandler(HonBaseConnectionHandler):
|
|||
response.status,
|
||||
await response.text(),
|
||||
)
|
||||
yield {}
|
||||
raise HonAuthenticationError("Decode Error")
|
||||
|
||||
|
||||
class HonAnonymousConnectionHandler(HonBaseConnectionHandler):
|
||||
_HEADERS = HonBaseConnectionHandler._HEADERS | {"x-api-key": const.API_KEY}
|
||||
_HEADERS: Dict = HonBaseConnectionHandler._HEADERS | {"x-api-key": const.API_KEY}
|
||||
|
||||
@asynccontextmanager
|
||||
async def _intercept(self, method, *args, loop=0, **kwargs):
|
||||
async def _intercept(
|
||||
self, method: Callable, *args, loop: int = 0, **kwargs
|
||||
) -> AsyncIterator:
|
||||
kwargs["headers"] = kwargs.pop("headers", {}) | self._HEADERS
|
||||
async with method(*args, **kwargs) as response:
|
||||
if response.status == 403:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue