Skip to content

Commit bac9010

Browse files
committed
Add ability to use http/https proxies with iland-sdk
1 parent 474623a commit bac9010

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

docs/usage.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,14 @@ cloud::
2727
'username': USERNAME}
2828
>>> api.post('/user/' + USERNAME + '/alert-emails', user_alert_emails)
2929

30+
To use a proxy you can define a ``python-requests`` style proxies_ dictionary
31+
and set the ``_proxies`` attribute on the api object::
32+
33+
>>> api = iland.Api(client_id=CLIENT_ID,
34+
client_secret=CLIENT_SECRET,
35+
username=USERNAME,
36+
password=PASSWORD)
37+
>>> api._proxies = {'https': 'https://10.10.1.10:3128'}
38+
>>> user = api.get('/user/' + USERNAME)
39+
40+
.. _proxies: http://docs.python-requests.org/en/master/user/advanced/#proxies

iland/api.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Api(object):
2323
_base_url = BASE_URL
2424
_access_token_url = ACCESS_URL
2525
_refresh_token_url = REFRESH_URL
26+
_proxies = None
2627

2728
_token = None
2829
_token_expiration_time = None
@@ -128,18 +129,26 @@ def _do_request(self, rpath, verb='GET', form_data=None):
128129
headers = {
129130
'Authorization': 'Bearer %s' % self._get_access_token_string(),
130131
'content-type': 'application/json'}
132+
133+
request_params = {
134+
'headers': headers,
135+
'verify': self._verify_ssl
136+
}
137+
138+
if verb in ('PUT', 'POST'):
139+
request_params['data'] = data
140+
141+
if self._proxies and isinstance(self._proxies, dict):
142+
request_params['proxies'] = self._proxies
143+
131144
if verb == 'GET':
132-
r = self._session.get(url, headers=headers,
133-
verify=self._verify_ssl)
145+
r = self._session.get(url, **request_params)
134146
elif verb == 'PUT':
135-
r = self._session.put(url, data=data, headers=headers,
136-
verify=self._verify_ssl)
147+
r = self._session.put(url, **request_params)
137148
elif verb == 'POST':
138-
r = self._session.post(url, data=data, headers=headers,
139-
verify=self._verify_ssl)
149+
r = self._session.post(url, **request_params)
140150
elif verb == 'DELETE':
141-
r = self._session.delete(url, headers=headers,
142-
verify=self._verify_ssl)
151+
r = self._session.delete(url, **request_params)
143152
else:
144153
raise ApiException({'message': 'Unsupported HTTP verb %s' % verb})
145154

tests/apicreds.py.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ CLIENT_ID = ''
44
CLIENT_SECRET = ''
55
USERNAME = ''
66
PASSWORD = ''
7+
PROXIES = {}

tests/test_iland_int.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,21 @@
2020
from apicreds import (CLIENT_ID,
2121
CLIENT_SECRET,
2222
USERNAME,
23-
PASSWORD)
23+
PASSWORD,
24+
PROXIES)
2425
except ImportError:
2526
try:
2627
from .apicreds import (CLIENT_ID,
2728
CLIENT_SECRET,
2829
USERNAME,
29-
PASSWORD)
30+
PASSWORD,
31+
PROXIES)
3032
except ImportError:
3133
CLIENT_ID = None
3234
CLIENT_SECRET = None
3335
USERNAME = None
3436
PASSWORD = None
37+
PROXIES = {}
3538

3639
VDC_UUID = \
3740
'res01.ilandcloud.com:urn:vcloud:vdc:a066325d-6be0-4733-8d9f-7687c36f4536'
@@ -131,3 +134,10 @@ def test_unauthorized_errors(self):
131134
def test_api_errors(self):
132135
with self.assertRaises(ApiException):
133136
self._api.get('/doesnotexist')
137+
138+
@unittest.skipIf(not PROXIES, "No proxies defined")
139+
def test_get_with_proxy(self):
140+
self._api._proxies = PROXIES
141+
user = self._api.get('/user/' + USERNAME)
142+
self.assertEqual(USERNAME, user.get('name'))
143+
self.assertTrue(len(user.keys()) > 5)

0 commit comments

Comments
 (0)