Skip to content

Commit 2a057b0

Browse files
authored
preparing next version (#39)
Adjustments to the last AppEcosystem update --------- Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
1 parent eb23b68 commit 2a057b0

29 files changed

+254
-103
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,5 @@ share/python-wheels/
9292
*.egg
9393
MANIFEST
9494
converted/
95+
96+
geckodriver.log

.pre-commit-config.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ repos:
1818
files: >-
1919
(?x)^(
2020
nc_py_api/|
21-
benchmarks/
21+
benchmarks/|
22+
tests/
2223
)
2324
2425
- repo: https://github.com/psf/black
@@ -28,7 +29,8 @@ repos:
2829
files: >-
2930
(?x)^(
3031
nc_py_api/|
31-
benchmarks/
32+
benchmarks/|
33+
tests/
3234
)
3335
3436
- repo: https://github.com/tox-dev/pyproject-fmt

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22

33
All notable changes to this project will be documented in this file.
44

5-
## [0.0.24 - 2023-07-xx]
5+
## [0.0.25 - 2023-07-xx]
6+
7+
### Changed
8+
9+
- Updated documentation, description
10+
-
11+
12+
## [0.0.24 - 2023-07-18]
613

714
### Added
815

nc_py_api/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from ._version import __version__
66
from .constants import ApiScope, LogLvl
7-
from .exceptions import NextcloudException, check_error
7+
from .exceptions import NextcloudException, NextcloudExceptionNotFound, check_error
88
from .files import FsNode, FsNodeInfo
99
from .integration_fastapi import (
1010
enable_heartbeat,

nc_py_api/_session.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
from xxhash import xxh64
2020

2121
from . import options
22-
from .exceptions import NextcloudException, check_error
22+
from .constants import OCSRespond
23+
from .exceptions import NextcloudException, NextcloudExceptionNotFound, check_error
2324

2425

2526
class ServerVersion(TypedDict):
@@ -138,8 +139,6 @@ def _ocs(self, method: str, path_params: str, headers: dict, data: Optional[byte
138139
raise NextcloudException(408, info=info) from None
139140

140141
check_error(response.status_code, info)
141-
if not response.text:
142-
raise ValueError(f"Response is empty. Status code: {response.status_code}")
143142
response_data = loads(response.text)
144143
ocs_meta = response_data["ocs"]["meta"]
145144
if ocs_meta["status"] != "ok":
@@ -149,6 +148,8 @@ def _ocs(self, method: str, path_params: str, headers: dict, data: Optional[byte
149148
self.adapter.close()
150149
self.init_adapter(restart=True)
151150
return self._ocs(method, path_params, headers, data, **kwargs, nested_req=True)
151+
if ocs_meta["statuscode"] in (404, OCSRespond.RESPOND_NOT_FOUND):
152+
raise NextcloudExceptionNotFound(reason=ocs_meta["message"], info=info)
152153
raise NextcloudException(status_code=ocs_meta["statuscode"], reason=ocs_meta["message"], info=info)
153154
return response_data["ocs"]["data"]
154155

nc_py_api/appconfig_preferences_ex.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from ._session import NcSessionBasic
77
from .constants import APP_V2_BASIC_URL
8+
from .exceptions import NextcloudExceptionNotFound
89
from .misc import require_capabilities
910

1011

@@ -19,28 +20,50 @@ class BasicAppCfgPref:
1920
def __init__(self, session: NcSessionBasic):
2021
self._session = session
2122

22-
def get_value(self, key: str) -> Optional[str]:
23+
def get_value(self, key: str, default=None) -> Optional[str]:
24+
if not key:
25+
raise ValueError("`key` parameter can not be empty")
2326
require_capabilities("app_ecosystem_v2", self._session.capabilities)
24-
r = self.get_values([key])
25-
if r:
26-
return r[0]["configvalue"]
27-
return None
27+
try:
28+
r = self.get_values([key])
29+
if r:
30+
return r[0]["configvalue"]
31+
except NextcloudExceptionNotFound:
32+
pass
33+
return default
2834

2935
def get_values(self, keys: list[str]) -> list[AppCfgPrefRecord]:
36+
if not keys:
37+
return []
38+
if not all(keys):
39+
raise ValueError("`key` parameter can not be empty")
3040
require_capabilities("app_ecosystem_v2", self._session.capabilities)
3141
data = {"configKeys": keys}
32-
return self._session.ocs(method="POST", path=f"{APP_V2_BASIC_URL}/{self.url_suffix}/get-values", json=data)
42+
try:
43+
return self._session.ocs(method="POST", path=f"{APP_V2_BASIC_URL}/{self.url_suffix}/get-values", json=data)
44+
except NextcloudExceptionNotFound:
45+
return []
3346

3447
def set(self, key: str, value: str) -> None:
48+
if not key:
49+
raise ValueError("`key` parameter can not be empty")
3550
require_capabilities("app_ecosystem_v2", self._session.capabilities)
3651
params = {"configKey": key, "configValue": value}
3752
self._session.ocs(method="POST", path=f"{APP_V2_BASIC_URL}/{self.url_suffix}", json=params)
3853

39-
def delete(self, keys: Union[str, list[str]]) -> None:
40-
require_capabilities("app_ecosystem_v2", self._session.capabilities)
54+
def delete(self, keys: Union[str, list[str]], not_fail=True) -> None:
4155
if isinstance(keys, str):
4256
keys = [keys]
43-
self._session.ocs(method="DELETE", path=f"{APP_V2_BASIC_URL}/{self.url_suffix}", json={"configKeys": keys})
57+
if not keys:
58+
return
59+
if not all(keys):
60+
raise ValueError("`key` parameter can not be empty")
61+
require_capabilities("app_ecosystem_v2", self._session.capabilities)
62+
try:
63+
self._session.ocs(method="DELETE", path=f"{APP_V2_BASIC_URL}/{self.url_suffix}", json={"configKeys": keys})
64+
except NextcloudExceptionNotFound as e:
65+
if not not_fail:
66+
raise e from None
4467

4568

4669
class PreferencesExAPI(BasicAppCfgPref):

nc_py_api/constants.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class LogLvl(IntEnum):
1212
FATAL = 4
1313

1414

15-
APP_V2_BASIC_URL = "/ocs/v2.php/apps/app_ecosystem_v2/api/v1"
15+
APP_V2_BASIC_URL = "/ocs/v1.php/apps/app_ecosystem_v2/api/v1"
1616

1717

1818
class ApiScope(IntEnum):
@@ -27,3 +27,10 @@ class ApiScope(IntEnum):
2727
class ApiScopesStruct(TypedDict):
2828
required: list[int]
2929
optional: list[int]
30+
31+
32+
class OCSRespond(IntEnum):
33+
RESPOND_SERVER_ERROR = 996
34+
RESPOND_UNAUTHORISED = 997
35+
RESPOND_NOT_FOUND = 998
36+
RESPOND_UNKNOWN_ERROR = 999

nc_py_api/exceptions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ def __str__(self):
2020
return f"[{self.status_code}]{reason}{info}"
2121

2222

23+
class NextcloudExceptionNotFound(NextcloudException):
24+
def __init__(self, reason="Not found", info: str = ""):
25+
super().__init__(404, reason=reason, info=info)
26+
27+
2328
def check_error(code: int, info: str = ""):
2429
if 996 <= code <= 999:
2530
if code == 996:

nc_py_api/preferences.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from ._session import NcSessionBasic
66
from .misc import check_capabilities, require_capabilities
77

8-
ENDPOINT = "/ocs/v2.php/apps/provisioning_api/api/v1/config/users"
8+
ENDPOINT = "/ocs/v1.php/apps/provisioning_api/api/v1/config/users"
99

1010

1111
class PreferencesAPI:

nc_py_api/ui_files_actions_menu.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from ._session import NcSessionApp
88
from .constants import APP_V2_BASIC_URL
9+
from .exceptions import NextcloudExceptionNotFound
910
from .misc import require_capabilities
1011

1112

@@ -28,7 +29,7 @@ class UiFilesActionsAPI:
2829
def __init__(self, session: NcSessionApp):
2930
self._session = session
3031

31-
def register(self, name: str, display_name: str, callback_url: str, **kwargs):
32+
def register(self, name: str, display_name: str, callback_url: str, **kwargs) -> None:
3233
require_capabilities("app_ecosystem_v2", self._session.capabilities)
3334
params = {
3435
"fileActionMenuParams": {
@@ -42,9 +43,13 @@ def register(self, name: str, display_name: str, callback_url: str, **kwargs):
4243
"action_handler": callback_url,
4344
},
4445
}
45-
return self._session.ocs(method="POST", path=f"{APP_V2_BASIC_URL}/{ENDPOINT_SUFFIX}", json=params)
46+
self._session.ocs(method="POST", path=f"{APP_V2_BASIC_URL}/{ENDPOINT_SUFFIX}", json=params)
4647

47-
def unregister(self, name: str):
48+
def unregister(self, name: str, not_fail=True) -> None:
4849
require_capabilities("app_ecosystem_v2", self._session.capabilities)
4950
params = {"fileActionMenuName": name}
50-
return self._session.ocs(method="DELETE", path=f"{APP_V2_BASIC_URL}/{ENDPOINT_SUFFIX}", json=params)
51+
try:
52+
self._session.ocs(method="DELETE", path=f"{APP_V2_BASIC_URL}/{ENDPOINT_SUFFIX}", json=params)
53+
except NextcloudExceptionNotFound as e:
54+
if not not_fail:
55+
raise e from None

0 commit comments

Comments
 (0)