Skip to content

Commit

Permalink
Add PathLike token type
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentame committed Dec 18, 2024
1 parent 93ac942 commit 7723ee3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ jobs:
name: Release
runs-on: ubuntu-latest
permissions:
id-token: write
attestations: write
contents: write
id-token: write
steps:
- name: Check out the repository
uses: actions/checkout@v4.2.2
Expand Down
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.1.0"
version = "1.2.0"
description = "Provides asynchronous authentication and access to Freebox servers"
authors = ["stilllman <luc_touraille@yahoo.fr>", "quentame <polletquentin74@me.com>", "HACF <contact@hacf.fr>"]
license = "GNU GPL v3"
Expand Down
20 changes: 11 additions & 9 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 @@ -48,8 +49,8 @@

# 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_DIRECTORY = path.dirname(path.abspath(__file__))
DEFAULT_TOKEN_FILE = path.join(DEFAULT_TOKEN_DIRECTORY, DEFAULT_TOKEN_FILENAME)

# Default application descriptor
DEFAULT_APP_DESC: Dict[str, str] = {
Expand All @@ -65,15 +66,16 @@


class Freepybox:

def __init__(
self,
app_desc: Dict[str, str] = DEFAULT_APP_DESC,
token_file: str = DEFAULT_TOKEN_FILE,
token_file: str | PathLike[str] = 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: str | PathLike[str] = token_file
self.api_version: str = api_version
self.timeout: int = timeout
self._session: ClientSession
Expand Down Expand Up @@ -115,7 +117,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 +193,7 @@ async def _get_freebox_access(
host: str,
port: str,
api_version: str,
token_file: str,
token_file: str | PathLike[str],
app_desc: Dict[str, str],
timeout: int = DEFAULT_TIMEOUT,
) -> Access:
Expand Down Expand Up @@ -296,7 +298,7 @@ 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: str | PathLike[str]
) -> None:
"""
Store the application token in g_app_auth_file file
Expand All @@ -311,7 +313,7 @@ def _writefile_app_token(
json.dump(file_content, f)

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

# 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 7723ee3

Please sign in to comment.