Skip to content

Commit f1f4521

Browse files
committed
Refactoring in order to ease creation of new wrappers outside this lib (custom apps) + python2 compatibility + addition of a wrapper for SystemTags + some lints and documentation
1 parent 6869dd1 commit f1f4521

34 files changed

+1398
-585
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99

1010
Python wrapper for NextCloud api
1111

12-
This is Python wrapper for NextCloud's API. With it you can manage your NextCloud instances from Python scripts.
13-
Tested with python 3.7, NextCloud 14.
12+
This is Python wrapper for NextCloud's API.
13+
With it you can manage your NextCloud instances from Python scripts.
14+
15+
Tested with :
16+
* NextCloud 14, python 3.7
17+
* NextCloud 20, python 2.7
18+
* NextCloud 20, python 3.6
1419

1520

1621
## FAQ
@@ -23,7 +28,7 @@ Check out the corresponding [nextcloud API documentation](https://nextcloud-api.
2328

2429
#### How do I use it?
2530

26-
Check out [the simple example](example.py) and also check out the [unit tests directory](tests).
31+
Check out [examples](examples) and also check out the [unit tests directory](tests).
2732

2833

2934
#### What do I do if it doesn't work?

api_implementation.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
"implementation status": "OK",
2222
"date_last_checked": "2019-02-02"
2323
},
24+
{
25+
"name": "Tags API",
26+
"url": "https://doc.owncloud.com/server/developer_manual/webdav_api/tags.html",
27+
"implementation status": "OK",
28+
"date_last_checked": "2021-05-03"
29+
},
2430
{
2531
"name": "Activity app API",
2632
"url": "https://github.com/nextcloud/activity",

docs/source/examples.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ Examples
44
Users API methods
55
-----------------
66

7-
.. include:: ../../example.py
7+
.. include:: ../../examples/user_management.py
88
:literal:
File renamed without changes.

requirements.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
requests
2-
pytest
1+
requests>=2.0.1
2+
pytest

setup.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,28 @@
1010

1111
setuptools.setup(
1212
name='nextcloud',
13-
version='0.0.1',
13+
version='0.0.2',
1414
author='EnterpriseyIntranet',
1515
description="Python wrapper for NextCloud api",
1616
long_description=long_description,
1717
long_description_content_type="text/markdown",
1818
url="https://github.com/EnterpriseyIntranet/nextcloud-API",
1919
packages=setuptools.find_packages(PKGDIR),
2020
include_package_data=True,
21-
install_requires=['requests'],
21+
install_requires=[
22+
'requests >= 2.0.1',
23+
'six'
24+
],
2225
package_dir={'': 'src'},
2326
classifiers=[
24-
'Programming Language :: Python :: 3.6',
25-
'Programming Language :: Python :: 3.7',
27+
'Programming Language :: Python',
28+
'Programming Language :: Python :: 2',
29+
'Programming Language :: Python :: 3',
30+
'Development Status :: 4 - Beta',
31+
'Environment :: Web Environment',
32+
'Intended Audience :: Developers',
33+
'Topic :: Internet :: WWW/HTTP',
34+
'Topic :: Software Development :: Libraries :: Python Modules',
2635
'License :: OSI Approved :: GNU General Public License (GPL)',
2736
"Operating System :: OS Independent",
2837
],

src/nextcloud/NextCloud.py

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/nextcloud/__init__.py

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,84 @@
11
# -*- coding: utf-8 -*-
2-
from .NextCloud import NextCloud
2+
from .session import Session
3+
from .api_wrappers import API_WRAPPER_CLASSES
34

5+
6+
class NextCloud(object):
7+
"""
8+
A NextCloud/OwnCloud client.
9+
Provides cookie persistence, connection-pooling, and configuration.
10+
11+
Basic Usage::
12+
13+
>>> from nextcloud import nextcloud
14+
>>> s = Nextcloud('https://nextcloud.mysite.com', user='admin', password='admin')
15+
>>> # or using use another auth method
16+
>>> from requests.auth import HTTPBasicAuth
17+
>>> s = Nextcloud('https://nextcloud.mysite.com', auth=HTTPBasicAuth('admin', 'admin'))
18+
>>> #
19+
>>> s.list_folders('/')
20+
<Response [200] data={} is_ok=True>
21+
22+
For a persistent session::
23+
>>> s.login() # if no user, password, or auth in parameter use existing
24+
>>> # some actions #
25+
>>> s.logout()
26+
27+
Or as a context manager::
28+
29+
>>> with Nextcloud('https://nextcloud.mysite.com',
30+
... user='admin', password='admin') as nxc:
31+
... # some actions #
32+
"""
33+
34+
def __init__(self, endpoint, user=None, password=None, json_output=True, auth=None, session_kwargs=None):
35+
self.query_components = []
36+
self._session = Session(
37+
url=endpoint, user=user, password=password, auth=auth,
38+
session_kwargs=session_kwargs
39+
)
40+
self.json_output = json_output
41+
for functionality_class in API_WRAPPER_CLASSES:
42+
json_able = getattr(functionality_class, 'JSON_ABLE', False)
43+
require_client = getattr(
44+
functionality_class, 'REQUIRE_CLIENT', False)
45+
functionality_instance = functionality_class(
46+
self._session,
47+
json_output=(json_able and json_output),
48+
client=(require_client and self))
49+
for potential_method in dir(functionality_instance):
50+
if not potential_method.startswith('_'):
51+
if not callable(getattr(functionality_instance, potential_method)):
52+
pass
53+
else:
54+
setattr(self, potential_method, getattr(
55+
functionality_instance, potential_method))
56+
57+
@property
58+
def user(self):
59+
return self._session.user
60+
61+
@property
62+
def url(self):
63+
return self._session.url
64+
65+
def __enter__(self):
66+
self.login()
67+
return self
68+
69+
def __exit__(self, *args):
70+
self.logout()
71+
72+
def login(self, user=None, password=None, auth=None):
73+
self.logout()
74+
self._session.login(user=user, password=password, auth=auth)
75+
76+
def with_auth(self, auth=None, **kwargs):
77+
init_kwargs = {'session_kwargs': self._session._session_kwargs,
78+
'json_output': self.json_output}
79+
init_kwargs.update(kwargs)
80+
return (self.__class__)(self._session.url, auth=auth, **init_kwargs)
81+
82+
def logout(self):
83+
if self._session.session:
84+
self._session.logout()
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# -*- coding: utf-8 -*-
1+
from nextcloud.base import API_WRAPPER_CLASSES
2+
23
from .activity import Activity
34
from .apps import Apps
45
from .capabilities import Capabilities
@@ -10,8 +11,4 @@
1011
from .user import User
1112
from .user_ldap import UserLDAP
1213
from .webdav import WebDAV
13-
14-
OCS_API_CLASSES = [Activity, Apps, Capabilities, FederatedCloudShare, Group, GroupFolders,
15-
Notifications, Share, User, UserLDAP]
16-
17-
WEBDAV_CLASS = WebDAV
14+
from .systemtags import SystemTags, SystemTagsRelation

src/nextcloud/api_wrappers/activity.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
# -*- coding: utf-8 -*-
2-
from nextcloud.base import WithRequester
2+
"""
3+
Activity API wrapper
4+
See https://doc.owncloud.com/server/user_manual/apps/activity.html
5+
https://doc.owncloud.com/server/developer_manual/core/apis/
6+
"""
7+
from nextcloud import base
38

49

5-
class Activity(WithRequester):
10+
class Activity(base.OCSv2ApiWrapper):
11+
""" Activity API wrapper """
612
API_URL = "/ocs/v2.php/apps/activity/api/v2/activity"
7-
SUCCESS_CODE = 200
813

914
def get_activities(self, since=None, limit=None, object_type=None, object_id=None, sort=None):
1015
"""
@@ -24,7 +29,7 @@ def get_activities(self, since=None, limit=None, object_type=None, object_id=Non
2429
(Default: desc)
2530
2631
Returns:
27-
32+
requester response
2833
"""
2934
params = dict(
3035
since=since,
@@ -34,5 +39,5 @@ def get_activities(self, since=None, limit=None, object_type=None, object_id=Non
3439
sort=sort
3540
)
3641
if params['object_type'] and params['object_id']:
37-
return self.requester.get(url="filter", params=params)
42+
return self.requester.get(url="filter", params=params)
3843
return self.requester.get(params=params)

0 commit comments

Comments
 (0)