Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'proxies'

  • Loading branch information...
commit 6a55e6ffd337a7d90c2ab17c9e387a459849c3e2 2 parents a674949 + 8a9d17b
@paulosman paulosman authored
View
15 README.rst
@@ -145,6 +145,21 @@ Update your profile description: ::
'description': "a new description"
})
+Proxy Support
+-------------
+
+If you're behind a proxy, you can specify it when creating a client: ::
+
+ import soundcloud
+
+ proxies = {
+ 'http': 'example.com:8000'
+ }
+ client = soundcloud.Client(access_token="a valid access token",
+ proxies=proxies)
+
+The proxies kwarg is a dictionary with protocols as keys and host:port as values.
+
Redirects
---------
View
28 soundcloud/client.py
@@ -52,9 +52,12 @@ def exchange_token(self, code):
'client_secret': self.options.get('client_secret'),
'code': code,
}
- verify_ssl = self.options.get('verify_ssl', True)
+ options.update({
+ 'verify_ssl': self.options.get('verify_ssl', True),
+ 'proxies': self.options.get('proxies', None)
+ })
self.token = wrapped_resource(
- make_request('post', url, options, verify_ssl))
+ make_request('post', url, options))
self.access_token = self.token.access_token
return self.token
@@ -82,9 +85,12 @@ def _refresh_token_flow(self):
'client_secret': self.options.get('client_secret'),
'refresh_token': self.options.get('refresh_token')
}
- verify_ssl = self.options.get('verify_ssl', True)
+ options.update({
+ 'verify_ssl': self.options.get('verify_ssl', True),
+ 'proxies': self.options.get('proxies', None)
+ })
self.token = wrapped_resource(
- make_request('post', url, options, verify_ssl))
+ make_request('post', url, options))
self.access_token = self.token.access_token
def _credentials_flow(self):
@@ -97,9 +103,12 @@ def _credentials_flow(self):
'password': self.options.get('password'),
'grant_type': 'password'
}
- verify_ssl = self.options.get('verify_ssl', True)
+ options.update({
+ 'verify_ssl': self.options.get('verify_ssl', True),
+ 'proxies': self.options.get('proxies', None)
+ })
self.token = wrapped_resource(
- make_request('post', url, options, verify_ssl))
+ make_request('post', url, options))
self.access_token = self.token.access_token
def _request(self, method, resource, **kwargs):
@@ -113,8 +122,11 @@ def _request(self, method, resource, **kwargs):
if hasattr(self, 'client_id'):
kwargs.update(dict(client_id=self.client_id))
- verify_ssl = self.options.get('verify_ssl', True)
- return wrapped_resource(make_request(method, url, kwargs, verify_ssl))
+ kwargs.update({
+ 'verify_ssl': self.options.get('verify_ssl', True),
+ 'proxies': self.options.get('proxies', None)
+ })
+ return wrapped_resource(make_request(method, url, kwargs))
def __getattr__(self, name):
"""Translate an HTTP verb into a request method."""
View
31 soundcloud/request.py
@@ -127,7 +127,7 @@ def namespaced_query_string(d, prefix=""):
return qs
-def make_request(method, url, params, verify_ssl=False):
+def make_request(method, url, params):
"""Make an HTTP request, formatting params as required."""
empty = []
for key, value in params.iteritems():
@@ -136,25 +136,32 @@ def make_request(method, url, params, verify_ssl=False):
for key in empty:
del params[key]
- files = namespaced_query_string(extract_files_from_dict(params))
- data = namespaced_query_string(remove_files_from_dict(params))
-
- request_func = getattr(requests, method, None)
- if request_func is None:
- raise TypeError('Unknown method: %s' % (method,))
-
# allow caller to disable automatic following of redirects
allow_redirects = params.get('allow_redirects', True)
+
kwargs = {
'allow_redirects': allow_redirects,
'headers': {
'User-Agent': soundcloud.USER_AGENT
}
}
- if 'allow_redirects' in data:
- del data['allow_redirects']
- if not verify_ssl:
- kwargs['verify_ssl'] = False
+ # options, not params
+ if 'verify_ssl' in params:
+ if params['verify_ssl'] is False:
+ kwargs['verify'] = params['verify_ssl']
+ del params['verify_ssl']
+ if 'proxies' in params:
+ kwargs['proxies'] = params['proxies']
+ del params['proxies']
+ if 'allow_redirects' in params:
+ del params['allow_redirects']
+
+ files = namespaced_query_string(extract_files_from_dict(params))
+ data = namespaced_query_string(remove_files_from_dict(params))
+
+ request_func = getattr(requests, method, None)
+ if request_func is None:
+ raise TypeError('Unknown method: %s' % (method,))
if method == 'get':
qs = urllib.urlencode(data)
View
27 soundcloud/tests/test_client.py
@@ -79,7 +79,7 @@ def test_disabling_ssl_verification(fake_get):
(fake_get.expects_call()
.with_args(expected_url,
headers=headers,
- verify_ssl=False,
+ verify=False,
allow_redirects=True)
.returns(MockResponse("{}")))
client.get('tracks', order='hotness', limit=5)
@@ -137,3 +137,28 @@ def test_method_dispatching_post_request(fake_post):
allow_redirects=True)
.returns(MockResponse("{}")))
client.post('tracks')
+
+
+@patch('requests.get')
+def test_proxy_servers(fake_request):
+ """Test that providing a dictionary of proxy servers works."""
+ proxies = {
+ 'http': 'myproxyserver:1234'
+ }
+ client = soundcloud.Client(client_id='foo', proxies=proxies)
+ expected_url = "%s?%s" % (
+ client._resolve_resource_name('me'),
+ urlencode({
+ 'client_id': 'foo'
+ })
+ )
+ headers = {
+ 'User-Agent': soundcloud.USER_AGENT
+ }
+ (fake_request.expects_call()
+ .with_args(expected_url,
+ headers=headers,
+ proxies=proxies,
+ allow_redirects=True)
+ .returns(MockResponse("{}")))
+ client.get('/me')
Please sign in to comment.
Something went wrong with that request. Please try again.