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,26 @@ 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
+ return self .__class__ (
205
+ oauth2_access_token or self ._oauth2_access_token ,
206
+ max_retries_on_error or self ._max_retries_on_error ,
207
+ max_retries_on_rate_limit or self ._max_retries_on_rate_limit ,
208
+ user_agent or self ._user_agent ,
209
+ session or self ._session ,
210
+ headers or self ._headers ,
211
+ timeout or self ._timeout
212
+ )
213
+
185
214
def request (self ,
186
215
route ,
187
216
namespace ,
@@ -421,6 +450,10 @@ def request_json_string(self,
421
450
err = stone_serializers .json_compat_obj_decode (
422
451
AuthError_validator , r .json ()['error' ])
423
452
raise AuthError (request_id , err )
453
+ elif r .status_code == HTTP_STATUS_INVALID_PATH_ROOT :
454
+ err = stone_serializers .json_compat_obj_decode (
455
+ PathRootError_validator , r .json ()['error' ])
456
+ raise PathRootError (request_id , err )
424
457
elif r .status_code == 429 :
425
458
err = None
426
459
if r .headers .get ('content-type' ) == 'application/json' :
@@ -479,13 +512,44 @@ def _save_body_to_file(self, download_path, http_resp, chunksize=2**16):
479
512
for c in http_resp .iter_content (chunksize ):
480
513
f .write (c )
481
514
515
+ def with_path_root (self , mode , namespace_id = None ):
516
+ """
517
+ Creates a clone of the Dropbox instance with the Dropbox-API-Path-Root header
518
+ as the appropriate serialized instance of PathRoot.
519
+
520
+ For more information, see
521
+ https://www.dropbox.com/developers/reference/namespace-guide#pathrootmodes
522
+
523
+ :param str mode: path root mode to use. Must be one of "home", "root", or "namespace_id"
524
+ :param str namespace_id: A namespace ID used in conjuntion with "root" or "namespace_id"
525
+ modes
526
+ :return: A :class: `Dropbox`
527
+ :rtype: Dropbox
528
+ """
529
+
530
+ if mode == 'root' :
531
+ path_root = PathRoot .root (namespace_id )
532
+ elif mode == 'namespace_id' :
533
+ path_root = PathRoot .namespace_id (namespace_id )
534
+ elif mode == 'home' :
535
+ path_root = PathRoot .home
536
+ else :
537
+ raise ValueError ("Invalid value for 'mode'. Must be one of 'home'" +
538
+ ", 'root' or 'namespace_id'" )
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
485
549
token. Methods of this class are meant to act on the corresponding user's
486
550
Dropbox.
487
551
"""
488
- pass
552
+
489
553
490
554
class DropboxTeam (_DropboxTransport , DropboxTeamBase ):
491
555
"""
@@ -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