Skip to content

Commit 768b72a

Browse files
Merge pull request #916 from allmightyspiff/ZuluPro-requests
requests sessions and retry
2 parents 84c4db6 + 8a9d858 commit 768b72a

File tree

8 files changed

+156
-49
lines changed

8 files changed

+156
-49
lines changed

CONTRIBUTORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ Swapnil Khanapurkar <swapnil_khanapurkar@persistent.co.in>
2929
The SoftLayer Developer Network <sldn@softlayer.com>
3030
Tim Ariyeh <tim.ariyeh@gmail.com>
3131
Wissam Elriachy <wissam.elriachy@gmail.com>
32+
Anthony Monthe (ZuluPro) <anthony.monthe@gmail.com>

SoftLayer/fixtures/SoftLayer_Account.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@
266266
"statusId": 4,
267267
"accountId": 1234
268268
}
269-
]
269+
],
270+
'accountId': 1234
270271
}
271272

272273
getRwhoisData = {

SoftLayer/transports.py

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import time
1212

1313
import requests
14+
from requests.adapters import HTTPAdapter
15+
from urllib3.util.retry import Retry
1416

1517
from SoftLayer import consts
1618
from SoftLayer import exceptions
@@ -38,6 +40,20 @@
3840
}
3941

4042

43+
def get_session(user_agent):
44+
"""Sets up urllib sessions"""
45+
46+
client = requests.Session()
47+
client.headers.update({
48+
'Content-Type': 'application/json',
49+
'User-Agent': user_agent,
50+
})
51+
retry = Retry(connect=3, backoff_factor=3)
52+
adapter = HTTPAdapter(max_retries=retry)
53+
client.mount('https://', adapter)
54+
return client
55+
56+
4157
class Request(object):
4258
"""Transport request object."""
4359

@@ -107,6 +123,15 @@ def __init__(self, endpoint_url=None, timeout=None, proxy=None, user_agent=None,
107123
self.proxy = proxy
108124
self.user_agent = user_agent or consts.USER_AGENT
109125
self.verify = verify
126+
self._client = None
127+
128+
@property
129+
def client(self):
130+
"""Returns client session object"""
131+
132+
if self._client is None:
133+
self._client = get_session(self.user_agent)
134+
return self._client
110135

111136
def __call__(self, request):
112137
"""Makes a SoftLayer API call against the XML-RPC endpoint.
@@ -154,13 +179,13 @@ def __call__(self, request):
154179
LOGGER.debug(payload)
155180

156181
try:
157-
resp = requests.request('POST', url,
158-
data=payload,
159-
headers=request.transport_headers,
160-
timeout=self.timeout,
161-
verify=verify,
162-
cert=request.cert,
163-
proxies=_proxies_dict(self.proxy))
182+
resp = self.client.request('POST', url,
183+
data=payload,
184+
headers=request.transport_headers,
185+
timeout=self.timeout,
186+
verify=verify,
187+
cert=request.cert,
188+
proxies=_proxies_dict(self.proxy))
164189
LOGGER.debug("=== RESPONSE ===")
165190
LOGGER.debug(resp.headers)
166191
LOGGER.debug(resp.content)
@@ -208,6 +233,15 @@ def __init__(self, endpoint_url=None, timeout=None, proxy=None, user_agent=None,
208233
self.proxy = proxy
209234
self.user_agent = user_agent or consts.USER_AGENT
210235
self.verify = verify
236+
self._client = None
237+
238+
@property
239+
def client(self):
240+
"""Returns client session object"""
241+
242+
if self._client is None:
243+
self._client = get_session(self.user_agent)
244+
return self._client
211245

212246
def __call__(self, request):
213247
"""Makes a SoftLayer API call against the REST endpoint.
@@ -217,9 +251,6 @@ def __call__(self, request):
217251
218252
:param request request: Request object
219253
"""
220-
request.transport_headers.setdefault('Content-Type', 'application/json')
221-
request.transport_headers.setdefault('User-Agent', self.user_agent)
222-
223254
params = request.headers.copy()
224255
if request.mask:
225256
params['objectMask'] = _format_object_mask(request.mask)
@@ -275,15 +306,15 @@ def __call__(self, request):
275306
LOGGER.debug(request.transport_headers)
276307
LOGGER.debug(raw_body)
277308
try:
278-
resp = requests.request(method, url,
279-
auth=auth,
280-
headers=request.transport_headers,
281-
params=params,
282-
data=raw_body,
283-
timeout=self.timeout,
284-
verify=verify,
285-
cert=request.cert,
286-
proxies=_proxies_dict(self.proxy))
309+
resp = self.client.request(method, url,
310+
auth=auth,
311+
headers=request.transport_headers,
312+
params=params,
313+
data=raw_body,
314+
timeout=self.timeout,
315+
verify=verify,
316+
cert=request.cert,
317+
proxies=_proxies_dict(self.proxy))
287318
LOGGER.debug("=== RESPONSE ===")
288319
LOGGER.debug(resp.headers)
289320
LOGGER.debug(resp.text)
@@ -339,13 +370,11 @@ def __call__(self, call):
339370
module_path = 'SoftLayer.fixtures.%s' % call.service
340371
module = importlib.import_module(module_path)
341372
except ImportError:
342-
raise NotImplementedError('%s fixture is not implemented'
343-
% call.service)
373+
raise NotImplementedError('%s fixture is not implemented' % call.service)
344374
try:
345375
return getattr(module, call.method)
346376
except AttributeError:
347-
raise NotImplementedError('%s::%s fixture is not implemented'
348-
% (call.service, call.method))
377+
raise NotImplementedError('%s::%s fixture is not implemented' % (call.service, call.method))
349378

350379

351380
def _proxies_dict(proxy):

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@
3333
'six >= 1.7.0',
3434
'prettytable >= 0.7.0',
3535
'click >= 5',
36-
'requests >= 2.7.0',
36+
'requests >= 2.18.4',
3737
'prompt_toolkit >= 0.53',
3838
'pygments >= 2.0.0',
39+
'urllib3 >= 1.22'
3940
],
4041
keywords=['softlayer', 'cloud'],
4142
classifiers=[

tests/decoration_tests.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import logging
99
import mock
10-
import unittest
1110

1211
from SoftLayer.decoration import retry
1312
from SoftLayer import exceptions
@@ -89,7 +88,3 @@ def raise_unexpected_error():
8988
raise TypeError('unexpected error')
9089

9190
self.assertRaises(TypeError, raise_unexpected_error)
92-
93-
if __name__ == '__main__':
94-
95-
unittest.main()

0 commit comments

Comments
 (0)