diff --git a/nc_py_api/appconfig_preferences_ex.py b/nc_py_api/appconfig_preferences_ex.py index 81b9926e..7c801d72 100644 --- a/nc_py_api/appconfig_preferences_ex.py +++ b/nc_py_api/appconfig_preferences_ex.py @@ -1,5 +1,6 @@ """Nextcloud API for working with apps V2's storage w/wo user context(table oc_appconfig_ex/oc_preferences_ex).""" -from typing import Optional, TypedDict, Union +from dataclasses import dataclass +from typing import Optional, Union from ._session import NcSessionBasic from .constants import APP_V2_BASIC_URL @@ -7,9 +8,14 @@ from .misc import require_capabilities -class AppCfgPrefRecord(TypedDict): - configkey: str - configvalue: str +@dataclass +class CfgRecord: + key: str + value: str + + def __init__(self, raw_data: dict): + self.key = raw_data["configkey"] + self.value = raw_data["configvalue"] class BasicAppCfgPref: @@ -24,24 +30,18 @@ def get_value(self, key: str, default=None) -> Optional[str]: require_capabilities("app_ecosystem_v2", self._session.capabilities) r = self.get_values([key]) if r: - return r[0]["configvalue"] + return r[0].value return default - def get_values(self, keys: list[str]) -> list[AppCfgPrefRecord]: + def get_values(self, keys: list[str]) -> list[CfgRecord]: if not keys: return [] if not all(keys): raise ValueError("`key` parameter can not be empty") require_capabilities("app_ecosystem_v2", self._session.capabilities) data = {"configKeys": keys} - return self._session.ocs(method="POST", path=f"{APP_V2_BASIC_URL}/{self.url_suffix}/get-values", json=data) - - def set_value(self, key: str, value: str) -> None: - if not key: - raise ValueError("`key` parameter can not be empty") - require_capabilities("app_ecosystem_v2", self._session.capabilities) - params = {"configKey": key, "configValue": value} - self._session.ocs(method="POST", path=f"{APP_V2_BASIC_URL}/{self.url_suffix}", json=params) + results = self._session.ocs(method="POST", path=f"{APP_V2_BASIC_URL}/{self.url_suffix}/get-values", json=data) + return [CfgRecord(i) for i in results] def delete(self, keys: Union[str, list[str]], not_fail=True) -> None: if isinstance(keys, str): @@ -61,6 +61,22 @@ def delete(self, keys: Union[str, list[str]], not_fail=True) -> None: class PreferencesExAPI(BasicAppCfgPref): url_suffix = "ex-app/preference" + def set_value(self, key: str, value: str) -> None: + if not key: + raise ValueError("`key` parameter can not be empty") + require_capabilities("app_ecosystem_v2", self._session.capabilities) + params = {"configKey": key, "configValue": value} + self._session.ocs(method="POST", path=f"{APP_V2_BASIC_URL}/{self.url_suffix}", json=params) + class AppConfigExAPI(BasicAppCfgPref): url_suffix = "ex-app/config" + + def set_value(self, key: str, value: str, sensitive: bool = False) -> None: + if not key: + raise ValueError("`key` parameter can not be empty") + require_capabilities("app_ecosystem_v2", self._session.capabilities) + params: dict = {"configKey": key, "configValue": value} + if sensitive: + params["sensitive"] = True + self._session.ocs(method="POST", path=f"{APP_V2_BASIC_URL}/{self.url_suffix}", json=params) diff --git a/tests/appconfig_preferences_ex_test.py b/tests/appconfig_preferences_ex_test.py index 76c36516..91797e4f 100644 --- a/tests/appconfig_preferences_ex_test.py +++ b/tests/appconfig_preferences_ex_test.py @@ -1,7 +1,7 @@ import pytest from gfixture import NC_APP -from nc_py_api import NextcloudExceptionNotFound +from nc_py_api import NextcloudExceptionNotFound, constants if NC_APP is None or "app_ecosystem_v2" not in NC_APP.capabilities: pytest.skip("app_ecosystem_v2 is not installed.", allow_module_level=True) @@ -108,7 +108,27 @@ def test_cfg_ex_get_typing(class_to_test): class_to_test.set_value("test key2", "321") r = class_to_test.get_values(["test key", "test key2"]) assert isinstance(r, list) - assert r[0]["configkey"] == "test key" - assert r[1]["configkey"] == "test key2" - assert r[0]["configvalue"] == "123" - assert r[1]["configvalue"] == "321" + assert r[0].key == "test key" + assert r[1].key == "test key2" + assert r[0].value == "123" + assert r[1].value == "321" + + +def test_appcfg_sensitive(): + appcfg = NC_APP.appconfig_ex_api + appcfg.delete("test_key") + appcfg.set_value("test_key", "123", sensitive=True) + assert appcfg.get_value("test_key") == "123" + assert appcfg.get_values(["test_key"])[0].value == "123" + appcfg.delete("test_key") + # next code tests `sensitive` value from the `AppEcosystem` + params = {"configKey": "test_key", "configValue": "123", "sensitive": True} + result = NC_APP._session.ocs(method="POST", path=f"{constants.APP_V2_BASIC_URL}/{appcfg.url_suffix}", json=params) + assert result["configkey"] == "test_key" + assert result["configvalue"] == "123" + assert bool(result["sensitive"]) is True + params["sensitive"] = False + result = NC_APP._session.ocs(method="POST", path=f"{constants.APP_V2_BASIC_URL}/{appcfg.url_suffix}", json=params) + assert result["configkey"] == "test_key" + assert result["configvalue"] == "123" + assert bool(result["sensitive"]) is False