|
22 | 22 | AuthError_validator,
|
23 | 23 | RateLimitError_validator,
|
24 | 24 | )
|
| 25 | +from .common import ( |
| 26 | + PathRoot, |
| 27 | + PathRoot_validator, |
| 28 | + PathRootError_validator |
| 29 | +) |
25 | 30 | from .base import DropboxBase
|
26 | 31 | from .base_team import DropboxTeamBase
|
27 | 32 | from .exceptions import (
|
28 | 33 | ApiError,
|
29 | 34 | AuthError,
|
30 | 35 | BadInputError,
|
31 | 36 | HttpError,
|
| 37 | + PathRootError, |
32 | 38 | InternalServerError,
|
33 | 39 | RateLimitError,
|
34 | 40 | )
|
|
42 | 48 | pinned_session,
|
43 | 49 | )
|
44 | 50 |
|
| 51 | +PATH_ROOT_HEADER = 'Dropbox-API-Path-Root' |
| 52 | +HTTP_STATUS_INVALID_PATH_ROOT = 422 |
| 53 | + |
45 | 54 | class RouteResult(object):
|
46 | 55 | """The successful result of a call to a route."""
|
47 | 56 |
|
@@ -182,6 +191,35 @@ def __init__(self,
|
182 | 191 |
|
183 | 192 | self._timeout = timeout
|
184 | 193 |
|
| 194 | + def clone( |
| 195 | + self, |
| 196 | + oauth2_access_token=None, |
| 197 | + max_retries_on_error=None, |
| 198 | + max_retries_on_rate_limit=None, |
| 199 | + user_agent=None, |
| 200 | + session=None, |
| 201 | + headers=None, |
| 202 | + timeout=None): |
| 203 | + """ |
| 204 | + Creates a new copy of the Dropbox client with the same defaults unless modified by |
| 205 | + arguments to clone() |
| 206 | +
|
| 207 | + See constructor for original parameter descriptions. |
| 208 | +
|
| 209 | + :return: New instance of Dropbox clent |
| 210 | + :rtype: Dropbox |
| 211 | + """ |
| 212 | + |
| 213 | + return self.__class__( |
| 214 | + oauth2_access_token or self._oauth2_access_token, |
| 215 | + max_retries_on_error or self._max_retries_on_error, |
| 216 | + max_retries_on_rate_limit or self._max_retries_on_rate_limit, |
| 217 | + user_agent or self._user_agent, |
| 218 | + session or self._session, |
| 219 | + headers or self._headers, |
| 220 | + timeout or self._timeout |
| 221 | + ) |
| 222 | + |
185 | 223 | def request(self,
|
186 | 224 | route,
|
187 | 225 | namespace,
|
@@ -421,6 +459,10 @@ def request_json_string(self,
|
421 | 459 | err = stone_serializers.json_compat_obj_decode(
|
422 | 460 | AuthError_validator, r.json()['error'])
|
423 | 461 | raise AuthError(request_id, err)
|
| 462 | + elif r.status_code == HTTP_STATUS_INVALID_PATH_ROOT: |
| 463 | + err = stone_serializers.json_compat_obj_decode( |
| 464 | + PathRootError_validator, r.json()['error']) |
| 465 | + raise PathRootError(request_id, err) |
424 | 466 | elif r.status_code == 429:
|
425 | 467 | err = None
|
426 | 468 | if r.headers.get('content-type') == 'application/json':
|
@@ -479,6 +521,28 @@ def _save_body_to_file(self, download_path, http_resp, chunksize=2**16):
|
479 | 521 | for c in http_resp.iter_content(chunksize):
|
480 | 522 | f.write(c)
|
481 | 523 |
|
| 524 | + def with_path_root(self, path_root): |
| 525 | + """ |
| 526 | + Creates a clone of the Dropbox instance with the Dropbox-API-Path-Root header |
| 527 | + as the appropriate serialized instance of PathRoot. |
| 528 | +
|
| 529 | + For more information, see |
| 530 | + https://www.dropbox.com/developers/reference/namespace-guide#pathrootmodes |
| 531 | +
|
| 532 | + :param PathRoot path_root: instance of PathRoot to serialize into the headers field |
| 533 | + :return: A :class: `Dropbox` |
| 534 | + :rtype: Dropbox |
| 535 | + """ |
| 536 | + |
| 537 | + if not isinstance(path_root, PathRoot): |
| 538 | + raise ValueError("path_root must be an instance of PathRoot") |
| 539 | + |
| 540 | + return self.clone( |
| 541 | + headers={ |
| 542 | + PATH_ROOT_HEADER: stone_serializers.json_encode(PathRoot_validator, path_root) |
| 543 | + } |
| 544 | + ) |
| 545 | + |
482 | 546 | class Dropbox(_DropboxTransport, DropboxBase):
|
483 | 547 | """
|
484 | 548 | Use this class to make requests to the Dropbox API using a user's access
|
@@ -532,12 +596,4 @@ def _get_dropbox_client_with_select_header(self, select_header_name, team_member
|
532 | 596 |
|
533 | 597 | new_headers = self._headers.copy() if self._headers else {}
|
534 | 598 | new_headers[select_header_name] = team_member_id
|
535 |
| - return Dropbox( |
536 |
| - self._oauth2_access_token, |
537 |
| - max_retries_on_error=self._max_retries_on_error, |
538 |
| - max_retries_on_rate_limit=self._max_retries_on_rate_limit, |
539 |
| - timeout=self._timeout, |
540 |
| - user_agent=self._raw_user_agent, |
541 |
| - session=self._session, |
542 |
| - headers=new_headers, |
543 |
| - ) |
| 599 | + return self.clone(headers=new_headers) |
0 commit comments