|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
""" |
|
3
|
|
|
Define what is an api wrapper |
|
4
|
|
|
""" |
|
5
|
|
|
import six |
|
6
|
|
|
from nextcloud.requester import Requester, OCSRequester, WebDAVRequester |
|
7
|
|
|
from nextcloud.codes import ProvisioningCode, OCSCode, WebDAVCode |
|
8
|
|
|
|
|
9
|
|
|
API_WRAPPER_CLASSES = [] |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class MetaWrapper(type): |
|
13
|
|
|
""" Meta class to register wrappers """ |
|
14
|
|
|
def __new__(cls, name, bases, attrs): |
|
15
|
|
|
new_cls = type.__new__(cls, name, bases, attrs) |
|
16
|
|
|
if (new_cls.API_URL != NotImplementedError and new_cls.VERIFIED): |
|
17
|
|
|
API_WRAPPER_CLASSES.append(new_cls) |
|
18
|
|
|
return new_cls |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
class BaseApiWrapper(object, six.with_metaclass(MetaWrapper)): |
|
22
|
|
|
""" |
|
23
|
|
|
Define an API wrapper |
|
24
|
|
|
|
|
25
|
|
|
Example of an abstract API wrapper. |
|
26
|
|
|
>>> class ApiWrapper(BaseApiWrapper): |
|
27
|
|
|
>>> REQUESTER = WebDAVRequester |
|
28
|
|
|
>>> SUCCESS_CODE = 100 |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
If API_URL is provided (and if attribute ''VERIFIED = False'' is not in the new |
|
32
|
|
|
class),then public methods of the class are added to NextCloud object. |
|
33
|
|
|
Example of a concerete API wrapper. |
|
34
|
|
|
>>> class Info(ApiWrapper): |
|
35
|
|
|
>>> API_URL = 'remote.php/info' |
|
36
|
|
|
>>> |
|
37
|
|
|
>>> def get_info(self): |
|
38
|
|
|
>>> return self.requester.get() |
|
39
|
|
|
|
|
40
|
|
|
""" |
|
41
|
|
|
API_URL = NotImplementedError |
|
42
|
|
|
VERIFIED = True |
|
43
|
|
|
JSON_ABLE = True |
|
44
|
|
|
REQUIRE_CLIENT = False |
|
45
|
|
|
REQUIRE_USER = False |
|
46
|
|
|
REQUESTER = Requester |
|
47
|
|
|
|
|
48
|
|
|
def __init__(self, session, json_output=None, client=None, user=None): |
|
49
|
|
|
self.json_output = json_output |
|
50
|
|
|
self.client = client |
|
51
|
|
|
self.user = user |
|
52
|
|
|
self.requester = self.REQUESTER(session, json_output=json_output) |
|
53
|
|
|
|
|
54
|
|
|
for attr_name in ['API_URL', 'SUCCESS_CODE', 'METHODS_SUCCESS_CODES']: |
|
55
|
|
|
setattr(self.requester, attr_name, getattr(self, attr_name, None)) |
|
56
|
|
|
|
|
57
|
|
|
def _arguments_get(self, varnames, vals): |
|
58
|
|
|
""" |
|
59
|
|
|
allows to automatically fetch values of varnames |
|
60
|
|
|
using generic values computing '_default_get_VARNAME' |
|
61
|
|
|
|
|
62
|
|
|
Example |
|
63
|
|
|
>>> def get_file_id(self, **kwargs): |
|
64
|
|
|
>>> file_id, = self._arguments_get(['file_id'], locals()) |
|
65
|
|
|
>>> |
|
66
|
|
|
>>> def _default_get_file_id(self, vals): |
|
67
|
|
|
>>> return self.get_file_id_from_name(vals.get('name', None)) |
|
68
|
|
|
>>> |
|
69
|
|
|
>>> nxc.get_file_id(name='foo.bar') |
|
70
|
|
|
|
|
71
|
|
|
:param varmames: list of wanted python var names |
|
72
|
|
|
:param vals: a dict object containing already set variables |
|
73
|
|
|
:returns: list of wanted values |
|
74
|
|
|
""" |
|
75
|
|
|
if 'kwargs' in vals: |
|
76
|
|
|
vals.update(vals['kwargs']) |
|
77
|
|
|
ret = [] |
|
78
|
|
|
for varname in varnames: |
|
79
|
|
|
val = vals.get(varname, None) |
|
80
|
|
|
if val is None: |
|
81
|
|
|
getter_func_name = '_default_get_%s' % varname |
|
82
|
|
|
if hasattr(self, getter_func_name): |
|
83
|
|
|
val = getattr(self, getter_func_name)(vals) |
|
84
|
|
|
ret.append(val) |
|
85
|
|
|
|
|
86
|
|
|
return ret |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
class ProvisioningApiWrapper(BaseApiWrapper): |
|
92
|
|
|
""" Define "Provisioning API" wrapper classes """ |
|
93
|
|
|
REQUESTER = OCSRequester |
|
94
|
|
|
SUCCESS_CODE = ProvisioningCode.SUCCESS |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
class OCSv1ApiWrapper(BaseApiWrapper): |
|
98
|
|
|
""" Define OCS wrapper classes """ |
|
99
|
|
|
REQUESTER = OCSRequester |
|
100
|
|
|
SUCCESS_CODE = OCSCode.SUCCESS_V1 |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
class OCSv2ApiWrapper(BaseApiWrapper): |
|
104
|
|
|
""" Define OCS wrapper classes """ |
|
105
|
|
|
REQUESTER = OCSRequester |
|
106
|
|
|
SUCCESS_CODE = OCSCode.SUCCESS_V2 |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
class WebDAVApiWrapper(BaseApiWrapper): |
|
110
|
|
|
""" Define WebDav wrapper classes """ |
|
111
|
|
|
REQUESTER = WebDAVRequester |
|
112
|
|
|
|
|
113
|
|
|
SUCCESS_CODE = { |
|
114
|
|
|
'PROPFIND': [WebDAVCode.MULTISTATUS], |
|
115
|
|
|
'PROPPATCH': [WebDAVCode.MULTISTATUS], |
|
116
|
|
|
'REPORT': [WebDAVCode.MULTISTATUS], |
|
117
|
|
|
'MKCOL': [WebDAVCode.CREATED], |
|
118
|
|
|
'COPY': [WebDAVCode.CREATED, WebDAVCode.NO_CONTENT], |
|
119
|
|
|
'MOVE': [WebDAVCode.CREATED, WebDAVCode.NO_CONTENT], |
|
120
|
|
|
'PUT': [WebDAVCode.CREATED], |
|
121
|
|
|
'POST': [WebDAVCode.CREATED], |
|
122
|
|
|
'DELETE': [WebDAVCode.NO_CONTENT] |
|
123
|
|
|
} |
|
124
|
|
|
|