diff --git a/docs/source/apidoc/app.rst b/docs/source/apidoc/app.rst index fd1217d68..410e30139 100644 --- a/docs/source/apidoc/app.rst +++ b/docs/source/apidoc/app.rst @@ -4,13 +4,13 @@ Application .. autoclass:: qbittorrentapi.app.AppAPIMixIn :members: :undoc-members: - :exclude-members: app, application, app_webapiVersion, app_buildInfo, app_setPreferences, app_defaultSavePath, app_networkInterfaceAddressList, app_networkInterfaceList, app_sendTestEmail + :exclude-members: app, application, app_webapiVersion, app_buildInfo, app_setPreferences, app_defaultSavePath, app_networkInterfaceAddressList, app_networkInterfaceList, app_sendTestEmail, app_getDirectoryContent :show-inheritance: .. autoclass:: qbittorrentapi.app.Application :members: :undoc-members: - :exclude-members: app, application, webapiVersion, buildInfo, setPreferences, defaultSavePath, networkInterfaceAddressList, networkInterfaceList, sendTestEmail + :exclude-members: app, application, webapiVersion, buildInfo, setPreferences, defaultSavePath, networkInterfaceAddressList, networkInterfaceList, sendTestEmail, getDirectoryContent .. autoclass:: qbittorrentapi.app.ApplicationPreferencesDictionary :members: @@ -22,6 +22,11 @@ Application :undoc-members: :show-inheritance: +.. autoclass:: qbittorrentapi.app.DirectoryContentList + :members: + :undoc-members: + :show-inheritance: + .. autoclass:: qbittorrentapi.app.NetworkInterfaceList :members: :undoc-members: diff --git a/src/qbittorrentapi/app.py b/src/qbittorrentapi/app.py index 98dcb7136..991322dad 100644 --- a/src/qbittorrentapi/app.py +++ b/src/qbittorrentapi/app.py @@ -1,9 +1,10 @@ from __future__ import annotations +import os from functools import wraps from json import dumps from logging import Logger, getLogger -from typing import Any, Iterable, Mapping, Union +from typing import Any, AnyStr, Iterable, Mapping, Union from qbittorrentapi.auth import AuthAPIMixIn from qbittorrentapi.definitions import ( @@ -55,6 +56,13 @@ def __init__(self, list_entries: Iterable[str], client: AppAPIMixIn | None = Non super().__init__(list_entries) # type: ignore +class DirectoryContentList(List[str]): # type: ignore + """Response for :meth:`~AppAPIMixIn.app_get_directory_content`""" + + def __init__(self, list_entries: Iterable[str], client: AppAPIMixIn | None = None): + super().__init__(list_entries) # type: ignore + + class AppAPIMixIn(AuthAPIMixIn): """ Implementation of all ``Application`` API methods. @@ -209,6 +217,31 @@ def app_send_test_email(self) -> None: app_sendTestEmail = app_send_test_email + def app_get_directory_content( + self, + directory_path: str | os.PathLike[AnyStr] | None = None, + ) -> DirectoryContentList: + """ + The contents of a directory file path. + + :raises NotFound404Error: file path not found or not a directory + :param directory_path: file system path to directory + """ + data = { + "dirPath": ( + os.fsdecode(directory_path) if directory_path is not None else None + ) + } + return self._post_cast( + _name=APINames.Application, + _method="getDirectoryContent", + data=data, + response_class=DirectoryContentList, + version_introduced="2.11", + ) + + app_getDirectoryContent = app_get_directory_content + class Application(ClientCache[AppAPIMixIn]): """ @@ -308,3 +341,12 @@ def send_test_email(self) -> None: self._client.app_send_test_email() sendTestEmail = send_test_email + + @wraps(AppAPIMixIn.app_get_directory_content) + def get_directory_content( + self, + directory_path: str | os.PathLike[AnyStr] | None = None, + ) -> DirectoryContentList: + return self._client.app_get_directory_content(directory_path=directory_path) + + getDirectoryContent = get_directory_content diff --git a/tests/conftest.py b/tests/conftest.py index 383a0949d..8559e0979 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -207,12 +207,9 @@ def app_version(client): @pytest.fixture def api_version(client): """qBittorrent Web API Version being used for testing.""" - try: - return api_version_map[QBT_VERSION] - except KeyError as exp: - if IS_QBT_DEV: - return client.app.web_api_version - raise exp + if IS_QBT_DEV: + return client.app.web_api_version + return api_version_map[QBT_VERSION] def pytest_sessionfinish(session, exitstatus): diff --git a/tests/test_app.py b/tests/test_app.py index c83792b62..e70748dba 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -4,6 +4,7 @@ from qbittorrentapi import APINames from qbittorrentapi._attrdict import AttrDict from qbittorrentapi.app import ( + DirectoryContentList, NetworkInterface, NetworkInterfaceAddressList, NetworkInterfaceList, @@ -125,3 +126,34 @@ def test_send_test_email(client_mock, send_test_email_func): _method="sendTestEmail", version_introduced="2.10.4", ) + + +@pytest.mark.skipif_before_api_version("2.11") +@pytest.mark.parametrize( + "get_directory_content_func", + [ + "app_get_directory_content", + "app.get_directory_content", + "app_getDirectoryContent", + "app.getDirectoryContent", + ], +) +def test_get_directory_content(client, get_directory_content_func): + dir_contents = client.func(get_directory_content_func)("/") + assert isinstance(dir_contents, DirectoryContentList) + assert all(isinstance(f, str) for f in dir_contents) + + +@pytest.mark.skipif_after_api_version("2.11") +@pytest.mark.parametrize( + "get_directory_content_func", + [ + "app_get_directory_content", + "app.get_directory_content", + "app_getDirectoryContent", + "app.getDirectoryContent", + ], +) +def test_get_directory_content_not_implemented(client, get_directory_content_func): + with pytest.raises(NotImplementedError): + client.func(get_directory_content_func)("/")