Skip to content

Commit

Permalink
fix aiohttp.ClientSession typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentame authored Oct 20, 2024
1 parent 01d6a60 commit 330dc66
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
16 changes: 10 additions & 6 deletions src/freebox_api/aiofreepybox.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from typing import Union
from urllib.parse import urljoin

from aiohttp import ClientSession
from aiohttp import ClientSession, ClientTimeout
from aiohttp import TCPConnector

import freebox_api
Expand Down Expand Up @@ -59,6 +59,8 @@
"device_name": socket.gethostname(),
}

DEFAULT_TIMEOUT = 10

logger = logging.getLogger(__name__)


Expand All @@ -68,7 +70,7 @@ def __init__(
app_desc: Dict[str, str] = DEFAULT_APP_DESC,
token_file: str = DEFAULT_TOKEN_FILE,
api_version: str = "v3",
timeout: int = 10,
timeout: int = DEFAULT_TIMEOUT,
):
self.app_desc: Dict[str, str] = app_desc
self.token_file: str = token_file
Expand Down Expand Up @@ -159,7 +161,7 @@ async def _get_freebox_access(
api_version: str,
token_file: str,
app_desc: Dict[str, str],
timeout: int = 10,
timeout: int = DEFAULT_TIMEOUT,
) -> Access:
"""
Returns an access object used for HTTP requests.
Expand Down Expand Up @@ -231,12 +233,12 @@ async def _get_authorization_status(
denied: the user denied the authorization request
"""
url = urljoin(base_url, f"login/authorize/{track_id}")
resp = await self._session.get(url, timeout=timeout)
resp = await self._session.get(url, timeout=ClientTimeout(total=timeout))
resp_data = await resp.json()
return str(resp_data["result"]["status"])

async def _get_app_token(
self, base_url: str, app_desc: Dict[str, str], timeout: int = 10
self, base_url: str, app_desc: Dict[str, str], timeout: int = DEFAULT_TIMEOUT
) -> Tuple[str, int]:
"""
Get the application token from the freebox
Expand All @@ -245,7 +247,9 @@ async def _get_app_token(
# Get authentification token
url = urljoin(base_url, "login/authorize/")
data = json.dumps(app_desc)
resp = await self._session.post(url, data=data, timeout=timeout)
resp = await self._session.post(
url, data=data, timeout=ClientTimeout(total=timeout)
)
resp_data = await resp.json()

# raise exception if resp.success != True
Expand Down
4 changes: 2 additions & 2 deletions src/freebox_api/api/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from typing import List
from typing import Optional

from aiohttp import ServerDisconnectedError
from aiohttp import ClientTimeout, ServerDisconnectedError

from freebox_api.access import Access

Expand Down Expand Up @@ -181,7 +181,7 @@ async def set_key(
long_press=key_data.get("long"), # type: ignore
repeat=key_data.get("repeat"), # type: ignore
),
timeout=_DEFAULT_TIMEOUT,
timeout=ClientTimeout(total=_DEFAULT_TIMEOUT),
skip_auto_headers=[
"Accept",
"Accept-Encoding",
Expand Down

0 comments on commit 330dc66

Please sign in to comment.