Skip to content

Commit 2d20a86

Browse files
committed
ex_app API rework [ci skip]
Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
1 parent eed6f11 commit 2d20a86

File tree

2 files changed

+55
-26
lines changed

2 files changed

+55
-26
lines changed

nc_py_api/apps.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
"""Nextcloud API for working with applications."""
22

3-
from typing import Optional, TypedDict
3+
from dataclasses import dataclass
4+
from typing import Optional
45

56
from ._session import NcSessionBasic
67
from .misc import require_capabilities
78

89
ENDPOINT = "/ocs/v1.php/cloud/apps"
910

1011

11-
class ExAppInfo(TypedDict):
12+
@dataclass
13+
class ExAppInfo:
1214
"""Information about the External Application."""
1315

14-
id: str
16+
app_id: str
1517
"""`ID` of the application"""
1618
name: str
1719
"""Display name"""
@@ -24,6 +26,14 @@ class ExAppInfo(TypedDict):
2426
system: bool
2527
"""Flag indicating if the application is a system application"""
2628

29+
def __init__(self, raw_data: dict):
30+
self.app_id = raw_data["id"]
31+
self.name = raw_data["name"]
32+
self.version = raw_data["version"]
33+
self.enabled = bool(raw_data["enabled"])
34+
self.last_check_time = raw_data["last_check_time"]
35+
self.system = raw_data["system"]
36+
2737

2838
class AppAPI:
2939
"""The class provides the application management API on the Nextcloud server."""
@@ -78,12 +88,25 @@ def is_disabled(self, app_id: str) -> bool:
7888
raise ValueError("`app_id` parameter can not be empty")
7989
return app_id in self.get_list(enabled=False)
8090

81-
def ex_app_get_list(self) -> list[str]:
82-
"""Gets the list of the external applications IDs installed on the server."""
83-
require_capabilities("app_ecosystem_v2", self._session.capabilities)
84-
return self._session.ocs(method="GET", path=f"{self._session.ae_url}/ex-app/all", params={"extended": 0})
91+
def ex_app_get_list(self, enabled: bool = False) -> list[ExAppInfo]:
92+
"""Gets information of the enabled external applications installed on the server.
8593
86-
def ex_app_get_info(self) -> list[ExAppInfo]:
87-
"""Gets information of the external applications installed on the server."""
94+
:param enabled: Flag indicating whether to return only enabled applications or all applications.
95+
Default = **False**.
96+
"""
8897
require_capabilities("app_ecosystem_v2", self._session.capabilities)
89-
return self._session.ocs(method="GET", path=f"{self._session.ae_url}/ex-app/all", params={"extended": 1})
98+
url_param = "enabled" if enabled else "all"
99+
r = self._session.ocs(method="GET", path=f"{self._session.ae_url}/ex-app/{url_param}")
100+
return [ExAppInfo(i) for i in r]
101+
102+
def ex_app_is_enabled(self, app_id: str) -> bool:
103+
"""Returns ``True`` if specified external application is enabled."""
104+
if not app_id:
105+
raise ValueError("`app_id` parameter can not be empty")
106+
return app_id in [i.app_id for i in self.ex_app_get_list(True)]
107+
108+
def ex_app_is_disabled(self, app_id: str) -> bool:
109+
"""Returns ``True`` if specified external application is disabled."""
110+
if not app_id:
111+
raise ValueError("`app_id` parameter can not be empty")
112+
return app_id in [i.app_id for i in self.ex_app_get_list(True) if not i.enabled]

tests/apps_test.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,33 @@ def test_invalid_param(nc):
5252
nc.apps.enable("")
5353
with pytest.raises(ValueError):
5454
nc.apps.disable("")
55+
with pytest.raises(ValueError):
56+
nc.apps.ex_app_is_enabled("")
57+
with pytest.raises(ValueError):
58+
nc.apps.ex_app_is_disabled("")
5559

5660

5761
@pytest.mark.parametrize("nc", NC_TO_TEST)
5862
def test_ex_app_get_list(nc):
5963
if "app_ecosystem_v2" not in nc.capabilities:
6064
pytest.skip("app_ecosystem_v2 is not installed.")
65+
enabled_ex_apps = nc.apps.ex_app_get_list(enabled=True)
66+
assert isinstance(enabled_ex_apps, list)
67+
for i in enabled_ex_apps:
68+
assert i.enabled is True
69+
assert nc.apps.ex_app_is_enabled(i.app_id) is True
70+
assert nc.apps.ex_app_is_disabled(i.app_id) is False
71+
assert "nc_py_api" in [i.app_id for i in enabled_ex_apps]
6172
ex_apps = nc.apps.ex_app_get_list()
6273
assert isinstance(ex_apps, list)
63-
assert isinstance(ex_apps[0], str)
64-
65-
66-
@pytest.mark.parametrize("nc", NC_TO_TEST)
67-
def test_ex_app_get_info(nc):
68-
if "app_ecosystem_v2" not in nc.capabilities:
69-
pytest.skip("app_ecosystem_v2 is not installed.")
70-
ex_apps = nc.apps.ex_app_get_info()
71-
assert isinstance(ex_apps, list)
72-
nc_py_api = [i for i in ex_apps if i["id"] == "nc_py_api"][0]
73-
assert nc_py_api["id"] == "nc_py_api"
74-
assert isinstance(nc_py_api["name"], str)
75-
assert isinstance(nc_py_api["version"], str)
76-
assert nc_py_api["enabled"]
77-
assert isinstance(nc_py_api["last_check_time"], int)
78-
assert nc_py_api["system"]
74+
assert "nc_py_api" in [i.app_id for i in ex_apps]
75+
assert len(ex_apps) >= len(enabled_ex_apps)
76+
for app in ex_apps:
77+
assert isinstance(app.app_id, str)
78+
assert isinstance(app.name, str)
79+
assert isinstance(app.version, str)
80+
assert isinstance(app.enabled, bool)
81+
assert isinstance(app.last_check_time, int)
82+
assert isinstance(app.system, bool)
83+
if app.app_id == "nc_py_api":
84+
assert app.system is True

0 commit comments

Comments
 (0)