pyhOn/pyhon/connection/handler/auth.py

38 lines
1.2 KiB
Python
Raw Normal View History

2023-04-15 12:22:04 +00:00
import logging
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
2023-06-28 17:02:11 +00:00
from typing import Optional, List, Tuple, Any
2023-04-15 12:22:04 +00:00
import aiohttp
from pyhon import const
from pyhon.connection.handler.base import ConnectionHandler
2023-06-28 17:02:11 +00:00
from pyhon.typedefs import Callback
2023-04-15 12:22:04 +00:00
_LOGGER = logging.getLogger(__name__)
class HonAuthConnectionHandler(ConnectionHandler):
_HEADERS = {"user-agent": const.USER_AGENT}
def __init__(self, session: Optional[aiohttp.ClientSession] = None) -> None:
super().__init__(session)
self._called_urls: List[Tuple[int, str]] = []
@property
def called_urls(self) -> List[Tuple[int, str]]:
return self._called_urls
@called_urls.setter
def called_urls(self, called_urls: List[Tuple[int, str]]) -> None:
self._called_urls = called_urls
@asynccontextmanager
async def _intercept(
2023-06-28 17:02:11 +00:00
self, method: Callback, *args: Any, **kwargs: Any
) -> AsyncIterator[aiohttp.ClientResponse]:
2023-04-15 12:22:04 +00:00
kwargs["headers"] = kwargs.pop("headers", {}) | self._HEADERS
async with method(*args, **kwargs) as response:
2023-06-28 17:02:11 +00:00
self._called_urls.append((response.status, str(response.request_info.url)))
2023-04-15 12:22:04 +00:00
yield response