Skip to content

Commit

Permalink
Support PathLike token type again (#750)
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentame authored Dec 19, 2024
1 parent c95d7f0 commit 16c94e2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/freebox_api/aiofreepybox.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import asyncio
import json
import logging
import os
from os import path
from os import PathLike
import socket
import ssl
from typing import Any
Expand Down Expand Up @@ -47,9 +48,9 @@
from freebox_api.exceptions import NotOpenError

# Token file default location
DEFAULT_TOKEN_FILENAME = "app_auth" # noqa S105
DEFAULT_TOKEN_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
DEFAULT_TOKEN_FILE = os.path.join(DEFAULT_TOKEN_DIRECTORY, DEFAULT_TOKEN_FILENAME)
DEFAULT_TOKEN_FILENAME: str = "app_auth" # noqa S105
DEFAULT_TOKEN_DIRECTORY = path.dirname(path.abspath(__file__))
DEFAULT_TOKEN_FILE: str = path.join(DEFAULT_TOKEN_DIRECTORY, DEFAULT_TOKEN_FILENAME)

# Default application descriptor
DEFAULT_APP_DESC: Dict[str, str] = {
Expand All @@ -61,19 +62,22 @@

DEFAULT_TIMEOUT = 10

StrOrPath = Union[str, "PathLike[str]"] # type TypeAlias but issues with <= py3.9

logger = logging.getLogger(__name__)


class Freepybox:

def __init__(
self,
app_desc: Dict[str, str] = DEFAULT_APP_DESC,
token_file: str = DEFAULT_TOKEN_FILE,
token_file: StrOrPath = DEFAULT_TOKEN_FILE,
api_version: str = "v3",
timeout: int = DEFAULT_TIMEOUT,
):
self.app_desc: Dict[str, str] = app_desc
self.token_file: str = token_file
self.token_file: StrOrPath = token_file
self.api_version: str = api_version
self.timeout: int = timeout
self._session: ClientSession
Expand Down Expand Up @@ -115,7 +119,7 @@ async def open(self, host: str, port: str) -> None:
if not self._is_app_desc_valid(self.app_desc):
raise InvalidTokenError("Invalid application descriptor")

cert_path = os.path.join(os.path.dirname(__file__), "freebox_certificates.pem")
cert_path = path.join(path.dirname(__file__), "freebox_certificates.pem")
ssl_ctx = ssl.create_default_context()
ssl_ctx.load_verify_locations(cafile=cert_path)
if ".fbxos.fr" in host or "mafreebox.freebox.fr" in host:
Expand Down Expand Up @@ -191,7 +195,7 @@ async def _get_freebox_access(
host: str,
port: str,
api_version: str,
token_file: str,
token_file: StrOrPath,
app_desc: Dict[str, str],
timeout: int = DEFAULT_TIMEOUT,
) -> Access:
Expand Down Expand Up @@ -296,7 +300,11 @@ async def _get_app_token(
return (app_token, track_id)

def _writefile_app_token(
self, app_token: str, track_id: int, app_desc: Dict[str, str], token_file: str
self,
app_token: str,
track_id: int,
app_desc: Dict[str, str],
token_file: StrOrPath,
) -> None:
"""
Store the application token in g_app_auth_file file
Expand All @@ -311,7 +319,7 @@ def _writefile_app_token(
json.dump(file_content, f)

def _readfile_app_token(
self, token_file: str
self, token_file: StrOrPath
) -> Union[Tuple[str, int, Dict[str, Any]], Tuple[None, None, None]]:
"""
Read the application token in the authentication file.
Expand Down
6 changes: 6 additions & 0 deletions tests/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@

# import m3u8

# from pathlib import Path

from freebox_api import Freepybox

# from freebox_api.aiofreepybox import DEFAULT_TOKEN_FILE


async def demo():
# Instantiate Freepybox class using default application descriptor
# and default token_file location
fbx = Freepybox(api_version="latest")
# fbx = Freepybox(token_file=DEFAULT_TOKEN_FILE, api_version="latest")
# fbx = Freepybox(token_file=Path(DEFAULT_TOKEN_FILE), api_version="latest")

# To find out the HTTPS host and port of your Freebox, go to
# http://mafreebox.freebox.fr/api_version
Expand Down

0 comments on commit 16c94e2

Please sign in to comment.