Skip to content

Commit

Permalink
Add support for app/getDirectoryContent (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmartin16 authored May 1, 2024
1 parent a816d42 commit 49bad8a
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 9 deletions.
9 changes: 7 additions & 2 deletions docs/source/apidoc/app.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
44 changes: 43 additions & 1 deletion src/qbittorrentapi/app.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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]):
"""
Expand Down Expand Up @@ -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
9 changes: 3 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
32 changes: 32 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from qbittorrentapi import APINames
from qbittorrentapi._attrdict import AttrDict
from qbittorrentapi.app import (
DirectoryContentList,
NetworkInterface,
NetworkInterfaceAddressList,
NetworkInterfaceList,
Expand Down Expand Up @@ -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)("/")

0 comments on commit 49bad8a

Please sign in to comment.