Skip to content
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

Initial introduction of exceptions to requests. #48

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
6 changes: 5 additions & 1 deletion src/nextcloud/api_wrappers/apps.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
67 changes: 67 additions & 0 deletions src/nextcloud/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down