Skip to content

Commit

Permalink
Merge branch 'proxies'
Browse files Browse the repository at this point in the history
  • Loading branch information
paulosman committed Mar 6, 2012
2 parents a674949 + 8a9d17b commit 6a55e6f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 21 deletions.
15 changes: 15 additions & 0 deletions README.rst
Expand Up @@ -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
---------

Expand Down
28 changes: 20 additions & 8 deletions soundcloud/client.py
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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."""
Expand Down
31 changes: 19 additions & 12 deletions soundcloud/request.py
Expand Up @@ -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():
Expand All @@ -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)
Expand Down
27 changes: 26 additions & 1 deletion soundcloud/tests/test_client.py
Expand Up @@ -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)
Expand Down Expand Up @@ -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')

0 comments on commit 6a55e6f

Please sign in to comment.