Skip to content

Commit 4bb5b53

Browse files
author
Vincent Petry
committed
Merge pull request #144 from owncloud/adding_capabilities_followup_PR53
Adapted PR 53 to 2015
2 parents e61d631 + 94f8b3c commit 4bb5b53

File tree

2 files changed

+75
-15
lines changed

2 files changed

+75
-15
lines changed

owncloud/owncloud.py

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ def __repr__(self):
288288
class Client():
289289
"""ownCloud client"""
290290

291+
OCS_BASEPATH = 'ocs/v1.php/'
291292
OCS_SERVICE_SHARE = 'apps/files_sharing/api/v1'
292293
OCS_SERVICE_PRIVATEDATA = 'privatedata'
293294
OCS_SERVICE_CLOUD = 'cloud'
@@ -323,6 +324,9 @@ def __init__(self, url, **kwargs):
323324
self.__verify_certs = kwargs.get('verify_certs', True)
324325
self.__single_session = kwargs.get('single_session', True)
325326

327+
self.__capabilities = None
328+
self.__version = None
329+
326330
url_components = urlparse.urlparse(url)
327331
self.__davpath = url_components.path + 'remote.php/webdav'
328332
self.__webdav_url = url + 'remote.php/webdav'
@@ -339,20 +343,13 @@ def login(self, user_id, password):
339343
self.__session = requests.session()
340344
self.__session.verify = self.__verify_certs
341345
self.__session.auth = (user_id, password)
342-
# TODO: use another path to prevent that the server renders the file list page
343-
res = self.__session.get(self.url + 'status.php')
344-
if res.status_code == 200:
345-
res = self.__make_ocs_request(
346-
'GET',
347-
self.OCS_SERVICE_CLOUD,
348-
'capabilities'
349-
)
350-
if res.status_code == 200:
351-
return
352-
raise HTTPResponseError(res)
353-
self.__session.close()
354-
self.__session = None
355-
raise HTTPResponseError(res)
346+
347+
try:
348+
self.__update_capabilities()
349+
except HTTPResponseError as e:
350+
self.__session.close()
351+
self.__session = None
352+
raise e
356353

357354
def logout(self):
358355
"""Log out the authenticated user and close the session.
@@ -1493,6 +1490,25 @@ def get_apps(self):
14931490

14941491
return ena_apps
14951492

1493+
def get_version(self):
1494+
"""Gets the ownCloud version of the connected server
1495+
1496+
:returns: ownCloud version as string
1497+
"""
1498+
if self.__version is None:
1499+
self.__update_capabilities()
1500+
return self.__version
1501+
1502+
def get_capabilities(self):
1503+
"""Gets the ownCloud app capabilities
1504+
1505+
:returns: capabilities dictionary that maps from
1506+
app name to another dictionary containing the capabilities
1507+
"""
1508+
if self.__capabilities is None:
1509+
self.__update_capabilities()
1510+
return self.__capabilities
1511+
14961512
def enable_app(self, appname):
14971513
"""Enable an app through provisioning_api
14981514
@@ -1596,7 +1612,7 @@ def __make_ocs_request(self, method, service, action, **kwargs):
15961612
slash = ''
15971613
if service:
15981614
slash = '/'
1599-
path = 'ocs/v1.php/' + service + slash + action
1615+
path = self.OCS_BASEPATH + service + slash + action
16001616

16011617
attributes = kwargs.copy()
16021618

@@ -1743,3 +1759,32 @@ def __get_shareinfo(self, data_el):
17431759
if (data_el is None) or not (isinstance(data_el, ET.Element)):
17441760
return None
17451761
return ShareInfo(self.__xml_to_dict(data_el))
1762+
1763+
def __update_capabilities(self):
1764+
res = self.__make_ocs_request(
1765+
'GET',
1766+
self.OCS_SERVICE_CLOUD,
1767+
'capabilities'
1768+
)
1769+
if res.status_code == 200:
1770+
tree = ET.fromstring(res.content)
1771+
self.__check_ocs_status(tree)
1772+
1773+
data_el = tree.find('data')
1774+
apps = {}
1775+
for app_el in data_el.find('capabilities'):
1776+
app_caps = {}
1777+
for cap_el in app_el:
1778+
app_caps[cap_el.tag] = cap_el.text
1779+
apps[app_el.tag] = app_caps
1780+
1781+
self.__capabilities = apps
1782+
1783+
version_el = data_el.find('version/string')
1784+
edition_el = data_el.find('version/edition')
1785+
self.__version = version_el.text
1786+
if edition_el.text is not None:
1787+
self.__version += '-' + edition_el.text
1788+
1789+
return self.__capabilities
1790+
raise HTTPResponseError(res)

owncloud/test/test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,21 @@ def test_get_config(self):
10641064
"""Test get_config() function"""
10651065
self.assertIsNotNone(self.client.get_config())
10661066

1067+
def test_get_version(self):
1068+
"""Test get_version() function"""
1069+
version = self.client.get_version()
1070+
self.assertIsNotNone(version)
1071+
version_parts = version.split('.')
1072+
self.assertGreaterEqual(int(version_parts[0]), 5)
1073+
1074+
def test_get_capabilities(self):
1075+
"""Test get_capabilities() function"""
1076+
caps = self.client.get_capabilities()
1077+
# files app is always enabled
1078+
self.assertIsNotNone(caps['files'])
1079+
# and always has big file chunking enabled
1080+
self.assertEquals(caps['files']['bigfilechunking'], '1')
1081+
10671082
def tearDown(self):
10681083
self.client.logout()
10691084

0 commit comments

Comments
 (0)