Skip to content

Clean-up/Refactoring of old code #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions nc_py_api/_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Exceptions for the Nextcloud API."""

from httpx import codes
from httpx import Response, codes


class NextcloudException(Exception):
Expand Down Expand Up @@ -42,21 +42,24 @@ def __init__(self, reason="Missing capability", info: str = ""):
super().__init__(412, reason=reason, info=info)


def check_error(code: int, info: str = ""):
def check_error(response: Response, info: str = ""):
"""Checks HTTP code from Nextcloud, and raises exception in case of error.

For the OCS and DAV `code` be code returned by HTTP and not the status from ``ocs_meta``.
"""
if 996 <= code <= 999:
if code == 996:
status_code = response.status_code
if not info:
info = f"request: {response.request.method} {response.request.url}"
if 996 <= status_code <= 999:
if status_code == 996:
phrase = "Server error"
elif code == 997:
elif status_code == 997:
phrase = "Unauthorised"
elif code == 998:
elif status_code == 998:
phrase = "Not found"
else:
phrase = "Unknown error"
raise NextcloudException(code, reason=phrase, info=info)
if not codes.is_error(code):
raise NextcloudException(status_code, reason=phrase, info=info)
if not codes.is_error(status_code):
return
raise NextcloudException(code, reason=codes(code).phrase, info=info)
raise NextcloudException(status_code, reason=codes(status_code).phrase, info=info)
4 changes: 2 additions & 2 deletions nc_py_api/_preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def available(self) -> bool:
def set_value(self, app_name: str, key: str, value: str) -> None:
"""Sets the value for the key for the specific application."""
require_capabilities("provisioning_api", self._session.capabilities)
self._session.ocs(method="POST", path=f"{self._ep_base}/{app_name}/{key}", params={"configValue": value})
self._session.ocs("POST", f"{self._ep_base}/{app_name}/{key}", params={"configValue": value})

def delete(self, app_name: str, key: str) -> None:
"""Removes a key and its value for a specific application."""
require_capabilities("provisioning_api", self._session.capabilities)
self._session.ocs(method="DELETE", path=f"{self._ep_base}/{app_name}/{key}")
self._session.ocs("DELETE", f"{self._ep_base}/{app_name}/{key}")
12 changes: 4 additions & 8 deletions nc_py_api/_preferences_ex.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ def get_values(self, keys: list[str]) -> list[CfgRecord]:
raise ValueError("`key` parameter can not be empty")
require_capabilities("app_api", self._session.capabilities)
data = {"configKeys": keys}
results = self._session.ocs(
method="POST", path=f"{self._session.ae_url}/{self._url_suffix}/get-values", json=data
)
results = self._session.ocs("POST", f"{self._session.ae_url}/{self._url_suffix}/get-values", json=data)
return [CfgRecord(i) for i in results]

def delete(self, keys: typing.Union[str, list[str]], not_fail=True) -> None:
Expand All @@ -59,9 +57,7 @@ def delete(self, keys: typing.Union[str, list[str]], not_fail=True) -> None:
raise ValueError("`key` parameter can not be empty")
require_capabilities("app_api", self._session.capabilities)
try:
self._session.ocs(
method="DELETE", path=f"{self._session.ae_url}/{self._url_suffix}", json={"configKeys": keys}
)
self._session.ocs("DELETE", f"{self._session.ae_url}/{self._url_suffix}", json={"configKeys": keys})
except NextcloudExceptionNotFound as e:
if not not_fail:
raise e from None
Expand All @@ -78,7 +74,7 @@ def set_value(self, key: str, value: str) -> None:
raise ValueError("`key` parameter can not be empty")
require_capabilities("app_api", self._session.capabilities)
params = {"configKey": key, "configValue": value}
self._session.ocs(method="POST", path=f"{self._session.ae_url}/{self._url_suffix}", json=params)
self._session.ocs("POST", f"{self._session.ae_url}/{self._url_suffix}", json=params)


class AppConfigExAPI(_BasicAppCfgPref):
Expand All @@ -99,4 +95,4 @@ def set_value(self, key: str, value: str, sensitive: typing.Optional[bool] = Non
params: dict = {"configKey": key, "configValue": value}
if sensitive is not None:
params["sensitive"] = sensitive
self._session.ocs(method="POST", path=f"{self._session.ae_url}/{self._url_suffix}", json=params)
self._session.ocs("POST", f"{self._session.ae_url}/{self._url_suffix}", json=params)
Loading