Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from hacf-fr:master #60

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "freebox-api"
version = "1.2.0"
version = "1.2.1"
description = "Provides asynchronous authentication and access to Freebox servers"
authors = ["stilllman <[email protected]>", "quentame <[email protected]>", "HACF <[email protected]>"]
license = "GNU GPL v3"
Expand Down
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
Loading