mirror of
https://github.com/Andre0512/pyhOn.git
synced 2025-05-22 02:45:10 -04:00
Refactor, add hon auth handler
This commit is contained in:
parent
d52d622785
commit
4a0ee8569b
7 changed files with 186 additions and 136 deletions
21
pyhon/connection/handler/anonym.py
Normal file
21
pyhon/connection/handler/anonym.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
import logging
|
||||
from collections.abc import AsyncIterator
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import Callable, Dict
|
||||
|
||||
from pyhon import const
|
||||
from pyhon.connection.handler.base import ConnectionHandler
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class HonAnonymousConnectionHandler(ConnectionHandler):
|
||||
_HEADERS: Dict = ConnectionHandler._HEADERS | {"x-api-key": const.API_KEY}
|
||||
|
||||
@asynccontextmanager
|
||||
async def _intercept(self, method: Callable, *args, **kwargs) -> AsyncIterator:
|
||||
kwargs["headers"] = kwargs.pop("headers", {}) | self._HEADERS
|
||||
async with method(*args, **kwargs) as response:
|
||||
if response.status == 403:
|
||||
_LOGGER.error("Can't authenticate anymore")
|
||||
yield response
|
Loading…
Add table
Add a link
Reference in a new issue