From 3fb9f62fd30b583f1a421a8cd96a2a0dbe6b12a7 Mon Sep 17 00:00:00 2001 From: Quentin POLLET Date: Mon, 28 Oct 2024 15:17:55 +0100 Subject: [PATCH] fix: typing --- src/freebox_api/access.py | 4 +-- src/freebox_api/api/download.py | 44 +++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/freebox_api/access.py b/src/freebox_api/access.py index e23fad45..7f30e932 100644 --- a/src/freebox_api/access.py +++ b/src/freebox_api/access.py @@ -1,7 +1,7 @@ import hmac import json import logging -from typing import Any +from typing import Any, Mapping from typing import Dict from typing import Optional from urllib.parse import urljoin @@ -138,7 +138,7 @@ async def get( return await self._perform_request(self.session.get, end_url) async def post( - self, end_url: str, payload: Optional[Dict[str, Any]] = None + self, end_url: str, payload: Optional[Mapping[str, Any]] = None ) -> Dict[str, Any]: """ Send post request and return results diff --git a/src/freebox_api/api/download.py b/src/freebox_api/api/download.py index 9ec2f457..9a9a46b1 100644 --- a/src/freebox_api/api/download.py +++ b/src/freebox_api/api/download.py @@ -4,14 +4,14 @@ """ import base64 -from typing import Any, TypedDict +from typing import Any, Required, TypedDict from typing import Dict from typing import Optional from freebox_api.access import Access -class DownloadAddURL(TypedDict): +class _DownloadAddURL(TypedDict, total=False): """ Add download by URL parameters data structure. @@ -30,17 +30,27 @@ class DownloadAddURL(TypedDict): cookies : `str` – The http cookies (to be able to pass session cookies along with url) """ - download_url: str - download_url_list: str - download_dir: Optional[str] + download_dir: str recursive: bool - username: Optional[str] - password: Optional[str] + username: str + password: str archive_password: str cookies: str -class DownloadAddFile(TypedDict): +class DownloadAddURL(_DownloadAddURL): + """Add download by URL parameters data structure.""" + + download_url: Required[str] + + +class DownloadAddURLList(_DownloadAddURL): + """Add download by URL list parameters data structure.""" + + download_url_list: Required[str] + + +class DownloadAddFile(TypedDict, total=False): """ Add download by file upload parameters data structure. @@ -53,8 +63,8 @@ class DownloadAddFile(TypedDict): (only relevant for nzb) """ - download_file: str - download_dir: Optional[str] + download_file: Required[str] + download_dir: str archive_password: str @@ -66,8 +76,14 @@ class Download: def __init__(self, access: Access) -> None: self._access = access - download_url_schema = { + download_url_schema: DownloadAddURL = { "download_url": "", + "username": "", + "password": "", + "recursive": False, + "download_dir": "", + } + download_url_list_schema: DownloadAddURLList = { "download_url_list": "", # items separated by /n "username": "", "password": "", @@ -169,8 +185,9 @@ async def add_download_task_from_url( """ download_params: DownloadAddURL = { "download_url": download_url, - "download_dir": download_dir, } + if download_dir: + download_params["download_dir"] = download_dir return await self.add_download_task(download_params) async def add_download_task_from_file( @@ -183,8 +200,9 @@ async def add_download_task_from_file( """ download_params: DownloadAddFile = { "download_file": download_file, - "download_dir": download_dir, } + if download_dir: + download_params["download_dir"] = download_dir return await self.add_download_task(download_params) # Download Stats