mirror of
https://github.com/Andre0512/pyhOn.git
synced 2025-05-30 14:35:19 -04:00
Add mypy checks
This commit is contained in:
parent
b6ca12ebff
commit
f54b7b2dbf
6 changed files with 73 additions and 63 deletions
|
@ -1,7 +1,7 @@
|
|||
import logging
|
||||
from collections.abc import AsyncIterator
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import Optional, Callable, Dict
|
||||
from typing import Optional, Callable, Dict, Any
|
||||
|
||||
import aiohttp
|
||||
from typing_extensions import Self
|
||||
|
@ -37,18 +37,18 @@ class ConnectionHandler:
|
|||
raise NotImplementedError
|
||||
|
||||
@asynccontextmanager
|
||||
async def get(self, *args, **kwargs) -> AsyncIterator[Callable]:
|
||||
async def get(self, *args, **kwargs) -> AsyncIterator[aiohttp.ClientResponse]:
|
||||
if self._session is None:
|
||||
raise exceptions.NoSessionException()
|
||||
response: Callable
|
||||
response: aiohttp.ClientResponse
|
||||
async with self._intercept(self._session.get, *args, **kwargs) as response:
|
||||
yield response
|
||||
|
||||
@asynccontextmanager
|
||||
async def post(self, *args, **kwargs) -> AsyncIterator[Callable]:
|
||||
async def post(self, *args, **kwargs) -> AsyncIterator[aiohttp.ClientResponse]:
|
||||
if self._session is None:
|
||||
raise exceptions.NoSessionException()
|
||||
response: Callable
|
||||
response: aiohttp.ClientResponse
|
||||
async with self._intercept(self._session.post, *args, **kwargs) as response:
|
||||
yield response
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ from typing_extensions import Self
|
|||
from pyhon.connection.auth import HonAuth
|
||||
from pyhon.connection.device import HonDevice
|
||||
from pyhon.connection.handler.base import ConnectionHandler
|
||||
from pyhon.exceptions import HonAuthenticationError
|
||||
from pyhon.exceptions import HonAuthenticationError, NoAuthenticationException
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -30,7 +30,9 @@ class HonConnectionHandler(ConnectionHandler):
|
|||
self._auth: Optional[HonAuth] = None
|
||||
|
||||
@property
|
||||
def auth(self) -> Optional[HonAuth]:
|
||||
def auth(self) -> HonAuth:
|
||||
if self._auth is None:
|
||||
raise NoAuthenticationException()
|
||||
return self._auth
|
||||
|
||||
@property
|
||||
|
@ -39,16 +41,14 @@ class HonConnectionHandler(ConnectionHandler):
|
|||
|
||||
async def create(self) -> Self:
|
||||
await super().create()
|
||||
self._auth: HonAuth = HonAuth(
|
||||
self._session, self._email, self._password, self._device
|
||||
)
|
||||
self._auth = HonAuth(self._session, self._email, self._password, self._device)
|
||||
return self
|
||||
|
||||
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
|
||||
headers["id-token"] = self._auth.id_token
|
||||
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
|
||||
|
||||
@asynccontextmanager
|
||||
|
@ -58,16 +58,16 @@ class HonConnectionHandler(ConnectionHandler):
|
|||
kwargs["headers"] = await self._check_headers(kwargs.get("headers", {}))
|
||||
async with method(*args, **kwargs) as response:
|
||||
if (
|
||||
self._auth.token_expires_soon or response.status in [401, 403]
|
||||
self.auth.token_expires_soon or response.status in [401, 403]
|
||||
) and loop == 0:
|
||||
_LOGGER.info("Try refreshing token...")
|
||||
await self._auth.refresh()
|
||||
await self.auth.refresh()
|
||||
async with self._intercept(
|
||||
method, *args, loop=loop + 1, **kwargs
|
||||
) as result:
|
||||
yield result
|
||||
elif (
|
||||
self._auth.token_is_expired or response.status in [401, 403]
|
||||
self.auth.token_is_expired or response.status in [401, 403]
|
||||
) and loop == 1:
|
||||
_LOGGER.warning(
|
||||
"%s - Error %s - %s",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue