|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
from .session import Session |
|
3
|
|
|
from .api_wrappers import API_WRAPPER_CLASSES |
|
4
|
|
|
|
|
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=None, |
|
35
|
|
|
user=None, password=None, json_output=True, auth=None, |
|
36
|
|
|
session_kwargs=None, |
|
37
|
|
|
session=None): |
|
38
|
|
|
self.session = session or Session( |
|
39
|
|
|
url=endpoint, user=user, password=password, auth=auth, |
|
40
|
|
|
session_kwargs=session_kwargs |
|
41
|
|
|
) |
|
42
|
|
|
self.json_output = json_output |
|
43
|
|
|
for functionality_class in API_WRAPPER_CLASSES: |
|
44
|
|
|
functionality_instance = functionality_class(self) |
|
45
|
|
|
for potential_method in dir(functionality_instance): |
|
46
|
|
|
if not potential_method.startswith('_'): |
|
47
|
|
|
if not callable(getattr(functionality_instance, potential_method)): |
|
48
|
|
|
pass |
|
49
|
|
|
else: |
|
50
|
|
|
setattr(self, potential_method, getattr( |
|
51
|
|
|
functionality_instance, potential_method)) |
|
52
|
|
|
|
|
53
|
|
|
@property |
|
54
|
|
|
def user(self): |
|
55
|
|
|
return self.session.user |
|
56
|
|
|
|
|
57
|
|
|
@property |
|
58
|
|
|
def url(self): |
|
59
|
|
|
return self.session.url |
|
60
|
|
|
|
|
61
|
|
|
def __enter__(self): |
|
62
|
|
|
self.login() |
|
63
|
|
|
return self |
|
64
|
|
|
|
|
65
|
|
|
def __exit__(self, *args): |
|
66
|
|
|
self.logout() |
|
67
|
|
|
|
|
68
|
|
|
def login(self, user=None, password=None, auth=None): |
|
69
|
|
|
self.logout() |
|
70
|
|
|
return self.session.login(user=user, password=password, auth=auth, |
|
71
|
|
|
client=self) |
|
72
|
|
|
|
|
73
|
|
|
def with_attr(self, **kwargs): |
|
74
|
|
|
if 'auth' in kwargs or 'endpoint' in kwargs or 'endpoint' in kwargs: |
|
75
|
|
|
return self.with_auth(**kwargs) |
|
76
|
|
|
if 'session_kwargs' in kwargs: |
|
77
|
|
|
return self.with_auth(auth=self.session.auth, **kwargs) |
|
78
|
|
|
return self.__class__(session=self.session, **kwargs) |
|
79
|
|
|
|
|
80
|
|
|
def with_auth(self, auth=None, **kwargs): |
|
81
|
|
|
init_kwargs = {'session_kwargs': self.session._session_kwargs, |
|
82
|
|
|
'json_output': self.json_output} |
|
83
|
|
|
init_kwargs.update(kwargs) |
|
84
|
|
|
if 'endpoint' in kwargs: |
|
85
|
|
|
return self.__class__(auth=auth, **init_kwargs) |
|
86
|
|
|
return self.__class__(endpoint=self.session.url, auth=auth, **init_kwargs) |
|
87
|
|
|
|
|
88
|
|
|
def logout(self): |
|
89
|
|
|
if self.session.session: |
|
90
|
|
|
self.session.logout() |
|
91
|
|
|
|