|
3 | 3 | import requests |
4 | 4 | from .compat import encode_requests_password |
5 | 5 |
|
| 6 | +import logging |
| 7 | +_logger = logging.getLogger(__name__) |
| 8 | + |
6 | 9 |
|
7 | 10 | class NextCloudConnectionError(Exception): |
8 | 11 | """ A connection error occurred """ |
@@ -31,7 +34,7 @@ def __init__(self, url=None, user=None, password=None, auth=None, session_kwargs |
31 | 34 | self.auth = None |
32 | 35 | self.user = None |
33 | 36 | self._set_credentials(user, password, auth) |
34 | | - self.url = url |
| 37 | + self.url = url.rstrip('/') |
35 | 38 | self._session_kwargs = session_kwargs or {} |
36 | 39 |
|
37 | 40 | def _set_credentials(self, user, password, auth): |
@@ -75,18 +78,35 @@ def login(self, user=None, password=None, auth=None, client=None): |
75 | 78 | self._set_credentials(user, password, auth) |
76 | 79 | self.session.auth = self.auth |
77 | 80 | if client: |
78 | | - try: |
79 | | - resp = client.with_attr(json_output=True).get_user() |
80 | | - if not resp.is_ok: |
81 | | - raise NextCloudLoginError( |
82 | | - 'Failed to login to NextCloud', self.url, resp) |
83 | | - except requests.exceptions.SSLError as e: |
84 | | - self.logout() |
85 | | - raise e |
86 | | - except Exception as e: |
| 81 | + self._check_session(client.with_attr(json_output=True), retry=3) |
| 82 | + |
| 83 | + |
| 84 | + def _check_session(self, client=None, retry=None): |
| 85 | + def _clear(): |
| 86 | + if self.session: |
87 | 87 | self.logout() |
| 88 | + |
| 89 | + def _raise(e): |
| 90 | + if retry: |
| 91 | + _logger.warning('Retry session check (%s)', self.url) |
| 92 | + return self._check_session(client, retry=retry - 1) |
| 93 | + else: |
| 94 | + _clear() |
88 | 95 | raise e |
89 | 96 |
|
| 97 | + try: |
| 98 | + resp = client.get_user() |
| 99 | + if not resp.is_ok: |
| 100 | + _raise(NextCloudLoginError( |
| 101 | + 'Failed to login to NextCloud', self.url, resp)) |
| 102 | + except requests.exceptions.SSLError as e: |
| 103 | + _raise(e) |
| 104 | + except NextCloudConnectionError as e: |
| 105 | + _raise(e) |
| 106 | + except Exception as e: |
| 107 | + _clear() |
| 108 | + raise e |
| 109 | + |
90 | 110 | def logout(self): |
91 | 111 | """Log out the authenticated user and close the session. |
92 | 112 |
|
|
0 commit comments