Skip to content

Commit bc93abf

Browse files
mbogosianbraincore
authored andcommitted
Fix *_HOST environment variable overrides
This ensures that environmental host overrides for both v1 and v2, which is necessary for testing.
1 parent c277238 commit bc93abf

File tree

2 files changed

+37
-31
lines changed

2 files changed

+37
-31
lines changed

dropbox/dropbox.py

+13-24
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import contextlib
1010
import json
1111
import logging
12-
import os
1312
import random
1413
import six
1514
import time
@@ -31,7 +30,15 @@
3130
InternalServerError,
3231
RateLimitError,
3332
)
34-
from .session import pinned_session
33+
from .session import (
34+
API_CONTENT_HOST,
35+
API_HOST,
36+
API_NOTIFICATION_HOST,
37+
HOST_API,
38+
HOST_CONTENT,
39+
HOST_NOTIFY,
40+
pinned_session,
41+
)
3542

3643

3744
class RouteResult(object):
@@ -98,17 +105,6 @@ class _DropboxTransport(object):
98105

99106
_API_VERSION = '2'
100107

101-
_DEFAULT_DOMAIN = '.dropboxapi.com'
102-
103-
# Host for RPC-style routes.
104-
_HOST_API = 'api'
105-
106-
# Host for upload and download-style routes.
107-
_HOST_CONTENT = 'content'
108-
109-
# Host for longpoll routes.
110-
_HOST_NOTIFY = 'notify'
111-
112108
# Download style means that the route argument goes in a Dropbox-API-Arg
113109
# header, and the result comes back in a Dropbox-API-Result header. The
114110
# HTTP response body contains a binary payload.
@@ -183,16 +179,9 @@ def __init__(self,
183179

184180
self._logger = logging.getLogger('dropbox')
185181

186-
self._domain = os.environ.get('DROPBOX_DOMAIN', Dropbox._DEFAULT_DOMAIN)
187-
self._api_hostname = os.environ.get(
188-
'DROPBOX_API_HOST', 'api' + self._domain)
189-
self._api_content_hostname = os.environ.get(
190-
'DROPBOX_API_CONTENT_HOST', 'content' + self._domain)
191-
self._api_notify_hostname = os.environ.get(
192-
'DROPBOX_API_NOTIFY_HOST', 'notify' + self._domain)
193-
self._host_map = {self._HOST_API: self._api_hostname,
194-
self._HOST_CONTENT: self._api_content_hostname,
195-
self._HOST_NOTIFY: self._api_notify_hostname}
182+
self._host_map = {HOST_API: API_HOST,
183+
HOST_CONTENT: API_CONTENT_HOST,
184+
HOST_NOTIFY: API_NOTIFICATION_HOST}
196185

197186
self._timeout = timeout
198187

@@ -389,7 +378,7 @@ def request_json_string(self,
389378
url = self._get_route_url(fq_hostname, func_name)
390379

391380
headers = {'User-Agent': self._user_agent}
392-
if host != self._HOST_NOTIFY:
381+
if host != HOST_NOTIFY:
393382
headers['Authorization'] = 'Bearer %s' % self._oauth2_access_token
394383
if self._headers:
395384
headers.update(self._headers)

dropbox/session.py

+24-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import pkg_resources
23
import ssl
34
import sys
@@ -52,6 +53,24 @@ def pinned_session(pool_maxsize=8):
5253
url_path_quote = urllib.parse.quote
5354
url_encode = urllib.parse.urlencode
5455

56+
DOMAIN = os.environ.get('DROPBOX_DOMAIN', '.dropboxapi.com')
57+
58+
# Default short hostname for RPC-style routes.
59+
HOST_API = 'api'
60+
61+
# Default short hostname for upload and download-style routes.
62+
HOST_CONTENT = 'content'
63+
64+
# Default short hostname for longpoll routes.
65+
HOST_NOTIFY = 'notify'
66+
67+
# Default short hostname for the Drobox website.
68+
HOST_WWW = 'www'
69+
70+
API_HOST = os.environ.get('DROPBOX_API_HOST', HOST_API + DOMAIN)
71+
API_CONTENT_HOST = os.environ.get('DROPBOX_API_CONTENT_HOST', HOST_CONTENT + DOMAIN)
72+
API_NOTIFICATION_HOST = os.environ.get('DROPBOX_API_NOTIFY_HOST', HOST_NOTIFY + DOMAIN)
73+
WEB_HOST = os.environ.get('DROPBOX_WEB_HOST', HOST_WWW + DOMAIN)
5574

5675
class OAuthToken(object):
5776
"""
@@ -65,11 +84,6 @@ def __init__(self, key, secret):
6584
class BaseSession(object):
6685
API_VERSION = 1
6786

68-
API_HOST = "api.dropbox.com"
69-
WEB_HOST = "www.dropbox.com"
70-
API_CONTENT_HOST = "api-content.dropbox.com"
71-
API_NOTIFICATION_HOST = "api-notify.dropbox.com"
72-
7387
def __init__(self, consumer_key, consumer_secret, access_type="auto", locale=None, rest_client=rest.RESTClient):
7488
"""Initialize a DropboxSession object.
7589
@@ -151,6 +165,11 @@ def build_url(self, host, target, params=None):
151165
"""
152166
return "https://%s%s" % (host, self.build_path(target, params))
153167

168+
BaseSession.API_HOST = API_HOST
169+
BaseSession.API_CONTENT_HOST = API_CONTENT_HOST
170+
BaseSession.API_NOTIFICATION_HOST = API_NOTIFICATION_HOST
171+
BaseSession.WEB_HOST = WEB_HOST
172+
154173
class DropboxSession(BaseSession):
155174

156175
def set_token(self, access_token, access_token_secret):
@@ -251,8 +270,6 @@ def build_access_headers(self, method, resource_url, params=None, request_token=
251270
"""Build OAuth access headers for a future request.
252271
253272
Args:
254-
- ``method``: The HTTP method being used (e.g. 'GET' or 'POST').
255-
- ``resource_url``: The full url the request will be made to.
256273
- ``params``: A dictionary of parameters to add to what's already on the url.
257274
Typically, this would consist of POST parameters.
258275

0 commit comments

Comments
 (0)