From b5d8a90d799a7c3f4f8022f2642cac88204112d4 Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Wed, 28 Jun 2023 20:25:52 +0200 Subject: [PATCH] Fix messed up parameters in request --- pyhon/connection/handler/anonym.py | 5 +++-- pyhon/connection/handler/auth.py | 7 ++++--- pyhon/connection/handler/base.py | 3 ++- pyhon/connection/handler/hon.py | 14 ++++++++++---- setup.py | 2 +- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/pyhon/connection/handler/anonym.py b/pyhon/connection/handler/anonym.py index 42cdc79..f2ef63f 100644 --- a/pyhon/connection/handler/anonym.py +++ b/pyhon/connection/handler/anonym.py @@ -4,6 +4,7 @@ from contextlib import asynccontextmanager from typing import Dict, Any import aiohttp +from yarl import URL from pyhon import const from pyhon.connection.handler.base import ConnectionHandler @@ -17,10 +18,10 @@ class HonAnonymousConnectionHandler(ConnectionHandler): @asynccontextmanager async def _intercept( - self, method: Callback, *args: Any, **kwargs: Any + self, method: Callback, url: str | URL, *args: Any, **kwargs: Dict[str, Any] ) -> AsyncIterator[aiohttp.ClientResponse]: kwargs["headers"] = kwargs.pop("headers", {}) | self._HEADERS - async with method(*args, **kwargs) as response: + async with method(url, *args, **kwargs) as response: if response.status == 403: _LOGGER.error("Can't authenticate anymore") yield response diff --git a/pyhon/connection/handler/auth.py b/pyhon/connection/handler/auth.py index 212395c..e0e327d 100644 --- a/pyhon/connection/handler/auth.py +++ b/pyhon/connection/handler/auth.py @@ -1,9 +1,10 @@ import logging from collections.abc import AsyncIterator from contextlib import asynccontextmanager -from typing import Optional, List, Tuple, Any +from typing import Optional, List, Tuple, Any, Dict import aiohttp +from yarl import URL from pyhon import const from pyhon.connection.handler.base import ConnectionHandler @@ -29,9 +30,9 @@ class HonAuthConnectionHandler(ConnectionHandler): @asynccontextmanager async def _intercept( - self, method: Callback, *args: Any, **kwargs: Any + self, method: Callback, url: str | URL, *args: Any, **kwargs: Dict[str, Any] ) -> AsyncIterator[aiohttp.ClientResponse]: kwargs["headers"] = kwargs.pop("headers", {}) | self._HEADERS - async with method(*args, **kwargs) as response: + async with method(url, *args, **kwargs) as response: self._called_urls.append((response.status, str(response.request_info.url))) yield response diff --git a/pyhon/connection/handler/base.py b/pyhon/connection/handler/base.py index eb254b5..efba820 100644 --- a/pyhon/connection/handler/base.py +++ b/pyhon/connection/handler/base.py @@ -6,6 +6,7 @@ from typing import Optional, Dict, Type, Any, Protocol import aiohttp from typing_extensions import Self +from yarl import URL from pyhon import const, exceptions from pyhon.typedefs import Callback @@ -47,7 +48,7 @@ class ConnectionHandler: @asynccontextmanager def _intercept( - self, method: Callback, *args: Any, loop: int = 0, **kwargs: Any + self, method: Callback, url: str | URL, *args: Any, **kwargs: Dict[str, Any] ) -> AsyncIterator[aiohttp.ClientResponse]: raise NotImplementedError diff --git a/pyhon/connection/handler/hon.py b/pyhon/connection/handler/hon.py index 15654de..f0f8574 100644 --- a/pyhon/connection/handler/hon.py +++ b/pyhon/connection/handler/hon.py @@ -6,6 +6,7 @@ from typing import Optional, Dict, Any import aiohttp from typing_extensions import Self +from yarl import URL from pyhon.connection.auth import HonAuth from pyhon.connection.device import HonDevice @@ -54,16 +55,19 @@ class HonConnectionHandler(ConnectionHandler): @asynccontextmanager async def _intercept( - self, method: Callback, *args: Any, loop: int = 0, **kwargs: Dict[str, str] + self, method: Callback, url: str | URL, *args: Any, **kwargs: Any ) -> AsyncIterator[aiohttp.ClientResponse]: + loop: int = kwargs.get("loop", 0) kwargs["headers"] = await self._check_headers(kwargs.get("headers", {})) - async with method(args[0], *args[1:], **kwargs) as response: + async with method(url, *args, **kwargs) as response: if ( self.auth.token_expires_soon or response.status in [401, 403] ) and loop == 0: _LOGGER.info("Try refreshing token...") await self.auth.refresh() - async with self._intercept(method, loop=loop + 1, **kwargs) as result: + async with self._intercept( + method, url, *args, loop=loop + 1, **kwargs + ) as result: yield result elif ( self.auth.token_is_expired or response.status in [401, 403] @@ -75,7 +79,9 @@ class HonConnectionHandler(ConnectionHandler): await response.text(), ) await self.create() - async with self._intercept(method, loop=loop + 1, **kwargs) as result: + async with self._intercept( + method, url, *args, loop=loop + 1, **kwargs + ) as result: yield result elif loop >= 2: _LOGGER.error( diff --git a/setup.py b/setup.py index d8bcf15..28a8d5d 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ with open("README.md", "r") as f: setup( name="pyhOn", - version="0.14.3", + version="0.14.4", author="Andre Basche", description="Control hOn devices with python", long_description=long_description,