|
1 | 1 | # -*- coding: utf-8 -*- |
2 | | -from .NextCloud import NextCloud |
| 2 | +from .session import Session |
| 3 | +from .api_wrappers import API_WRAPPER_CLASSES |
3 | 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, 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() |
0 commit comments