diff --git a/src/nextcloud/api_wrappers/apps.py b/src/nextcloud/api_wrappers/apps.py index 2a904c2..a1f1391 100644 --- a/src/nextcloud/api_wrappers/apps.py +++ b/src/nextcloud/api_wrappers/apps.py @@ -1,11 +1,12 @@ # -*- coding: utf-8 -*- -from nextcloud.base import WithRequester +from nextcloud.base import WithRequester, OCSMRD class Apps(WithRequester): API_URL = "/ocs/v1.php/cloud/apps" SUCCESS_CODE = 100 + @OCSMRD("apps", "apps") def get_apps(self, filter=None): """ Get a list of apps installed on the Nextcloud server @@ -18,6 +19,7 @@ def get_apps(self, filter=None): } return self.requester.get(params=params) + @OCSMRD("app") def get_app(self, app_id): """ Provide information on a specific application @@ -27,6 +29,7 @@ def get_app(self, app_id): """ return self.requester.get(app_id) + @OCSMRD() def enable_app(self, app_id): """ Enable an app @@ -36,6 +39,7 @@ def enable_app(self, app_id): """ return self.requester.post(app_id) + @OCSMRD() def disable_app(self, app_id): """ Disable the specified app diff --git a/src/nextcloud/base.py b/src/nextcloud/base.py index 3e5122e..9d45a87 100644 --- a/src/nextcloud/base.py +++ b/src/nextcloud/base.py @@ -18,6 +18,73 @@ def requester(self): return self._requester +class NextcloudError(RuntimeError): + def __init__(self, msg, code=None): + super().__init__(msg) + self.code = code + + +class MapResultData: + def __init__(self, where=None, what=None): + """ + Args: + where: To which attribute to map the result + what: How to get the result + """ + self.where = where + self.what = what + + def _get_error(self, result): + raise NotImplementedError() + + def _map_result(self, result): + raise NotImplementedError() + + def __call__(self, wrapped): + def wrapper(* args, ** kwargs): + res = wrapped(* args, ** kwargs) + if res.is_ok: + if self.where: + self._map_result(res) + else: + raise self._get_error(res) + return res + wrapped.__doc__ = self._update_doc(wrapped.__doc__) + return wrapper + + def _update_doc(self, original_doc): + return original_doc + + +class WebDavMRD(MapResultData): + def _get_error(self, result): + exc = NextcloudError(result.raw.reason, result.raw.status_code) + return exc + + +class OCSMRD(MapResultData): + def _map_result(self, result): + if self.what: + setattr(result, self.where, result.data[self.what]) + else: + setattr(result, self.where, result.data) + + def _result_doc_string(self): + ret = "" + if self.what: + ret = "The result is available as .data[XXX] key, or the .YYY attribute" + return ret + + def _update_doc(self, original_doc): + if ":return:" in original_doc: + doc = original_doc + "\n" + self._result_doc_string() + return doc + + def _get_error(self, result): + exc = NextcloudError(result.meta['message'], result.meta['statuscode']) + return exc + + class OCSCode(enum.IntEnum): OK = 100 SERVER_ERROR = 996