Skip to content

Commit 8a9d858

Browse files
upgrading requests and urllib3 libraries to better support retries
1 parent 017e3fa commit 8a9d858

File tree

7 files changed

+97
-14
lines changed

7 files changed

+97
-14
lines changed

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: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import requests
1414
from requests.adapters import HTTPAdapter
15-
from requests.packages.urllib3.util.retry import Retry
15+
from urllib3.util.retry import Retry
1616

1717
from SoftLayer import consts
1818
from SoftLayer import exceptions
@@ -41,6 +41,8 @@
4141

4242

4343
def get_session(user_agent):
44+
"""Sets up urllib sessions"""
45+
4446
client = requests.Session()
4547
client.headers.update({
4648
'Content-Type': 'application/json',
@@ -121,10 +123,13 @@ def __init__(self, endpoint_url=None, timeout=None, proxy=None, user_agent=None,
121123
self.proxy = proxy
122124
self.user_agent = user_agent or consts.USER_AGENT
123125
self.verify = verify
126+
self._client = None
124127

125128
@property
126129
def client(self):
127-
if not hasattr(self, '_client'):
130+
"""Returns client session object"""
131+
132+
if self._client is None:
128133
self._client = get_session(self.user_agent)
129134
return self._client
130135

@@ -228,10 +233,13 @@ def __init__(self, endpoint_url=None, timeout=None, proxy=None, user_agent=None,
228233
self.proxy = proxy
229234
self.user_agent = user_agent or consts.USER_AGENT
230235
self.verify = verify
236+
self._client = None
231237

232238
@property
233239
def client(self):
234-
if not hasattr(self, '_client'):
240+
"""Returns client session object"""
241+
242+
if self._client is None:
235243
self._client = get_session(self.user_agent)
236244
return self._client
237245

@@ -362,13 +370,11 @@ def __call__(self, call):
362370
module_path = 'SoftLayer.fixtures.%s' % call.service
363371
module = importlib.import_module(module_path)
364372
except ImportError:
365-
raise NotImplementedError('%s fixture is not implemented'
366-
% call.service)
373+
raise NotImplementedError('%s fixture is not implemented' % call.service)
367374
try:
368375
return getattr(module, call.method)
369376
except AttributeError:
370-
raise NotImplementedError('%s::%s fixture is not implemented'
371-
% (call.service, call.method))
377+
raise NotImplementedError('%s::%s fixture is not implemented' % (call.service, call.method))
372378

373379

374380
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()

tests/transport_tests.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,3 +523,81 @@ def test_unknown_error(self, request):
523523
req.method = 'getObject'
524524

525525
self.assertRaises(SoftLayer.TransportError, self.transport, req)
526+
527+
@mock.patch('SoftLayer.transports.requests.Session.request')
528+
def test_with_limits(self, request):
529+
request().text = '{}'
530+
531+
req = transports.Request()
532+
req.service = 'SoftLayer_Service'
533+
req.method = 'getObject'
534+
req.limit = 10
535+
req.offset = 10
536+
537+
resp = self.transport(req)
538+
539+
self.assertEqual(resp, {})
540+
request.assert_called_with(
541+
'GET',
542+
'http://something.com/SoftLayer_Service/getObject.json',
543+
headers=mock.ANY,
544+
auth=None,
545+
data=None,
546+
params={'limit': 10, 'offset': 10},
547+
verify=True,
548+
cert=None,
549+
proxies=None,
550+
timeout=None)
551+
552+
@mock.patch('SoftLayer.transports.requests.Session.request')
553+
def test_with_username(self, request):
554+
request().text = '{}'
555+
556+
req = transports.Request()
557+
req.service = 'SoftLayer_Service'
558+
req.method = 'getObject'
559+
req.transport_user = 'Bob'
560+
req.transport_password = '123456'
561+
auth = requests.auth.HTTPBasicAuth(req.transport_user, req.transport_password)
562+
563+
resp = self.transport(req)
564+
565+
self.assertEqual(resp, {})
566+
request.assert_called_with(
567+
'GET',
568+
'http://something.com/SoftLayer_Service/getObject.json',
569+
headers=mock.ANY,
570+
auth=auth,
571+
data=None,
572+
params={},
573+
verify=True,
574+
cert=None,
575+
proxies=None,
576+
timeout=None)
577+
578+
579+
class TestFixtureTransport(testing.TestCase):
580+
581+
def set_up(self):
582+
transport = testing.MockableTransport(SoftLayer.FixtureTransport())
583+
self.env.client = SoftLayer.BaseClient(transport=transport)
584+
585+
def test_base_case(self):
586+
result = self.env.client.call('SoftLayer_Account', 'getObject')
587+
self.assertEqual(result['accountId'], 1234)
588+
589+
def test_Import_Service_Error(self):
590+
# assertRaises doesn't work here for some reason
591+
try:
592+
self.env.client.call('FAKE', 'getObject')
593+
self.assertTrue(False)
594+
except NotImplementedError:
595+
self.assertTrue(True)
596+
597+
def test_Import_Method_Error(self):
598+
# assertRaises doesn't work here for some reason
599+
try:
600+
self.env.client.call('SoftLayer_Account', 'getObject111')
601+
self.assertTrue(False)
602+
except NotImplementedError:
603+
self.assertTrue(True)

tools/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ click >= 5
33
prettytable >= 0.7.0
44
six >= 1.7.0
55
prompt_toolkit
6+
urllib3

tools/test-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ pytest-cov
44
mock
55
sphinx
66
testtools
7+
urllib3

0 commit comments

Comments
 (0)