Skip to content

Commit 73792c0

Browse files
committed
Removes support for Python 2.5
* Adds fallback to distutils if setuptools is not installed * Removes the xmlrpc transport and moved the requests transport to SoftLayer/transport.py * Adds make_rest_api_call to SoftLayer.transport and related tests. SoftLayer.metadata is currently the only thing that uses it. I think both of the transport types should have the same interface, but that's not the case at the moment. * Removes requirement for simplejson if python version is less than 2.6. * setup.py bails out early if the python version is less than 2.
1 parent 021381b commit 73792c0

13 files changed

Lines changed: 200 additions & 211 deletions

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
language: python
22
python:
3-
- "2.5"
43
- "2.6"
54
- "2.7"
65
- "3.2"
@@ -9,7 +8,7 @@ python:
98
# command to install dependencies
109
install:
1110
- "pip install -r requirements.txt --use-mirrors"
12-
- "if [[ $TRAVIS_PYTHON_VERSION = 2.6 || $TRAVIS_PYTHON_VERSION = 2.5 ]]; then pip install unittest2 --use-mirrors; fi"
11+
- "if [[ $TRAVIS_PYTHON_VERSION = 2.6 ]]; then pip install unittest2 --use-mirrors; fi"
1312
# command to run tests
1413
script: python setup.py nosetests
1514

SoftLayer/API.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"""
99
from SoftLayer.consts import API_PUBLIC_ENDPOINT, API_PRIVATE_ENDPOINT, \
1010
USER_AGENT
11-
from SoftLayer.transport import make_api_call
11+
from SoftLayer.transport import make_xml_rpc_api_call
1212
from SoftLayer.exceptions import SoftLayerError
1313
import os
1414

@@ -281,9 +281,11 @@ def call(self, service, method, *args, **kwargs):
281281
'offset': int(offset)
282282
}
283283
uri = '/'.join([self._endpoint_url, service])
284-
return make_api_call(uri, method, args, headers=headers,
285-
http_headers=http_headers, timeout=self.timeout,
286-
verbose=self.verbose)
284+
return make_xml_rpc_api_call(uri, method, args,
285+
headers=headers,
286+
http_headers=http_headers,
287+
timeout=self.timeout,
288+
verbose=self.verbose)
287289

288290
__call__ = call
289291

SoftLayer/metadata.py

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
:copyright: (c) 2013, SoftLayer Technologies, Inc. All rights reserved.
77
:license: BSD, see LICENSE for more details.
88
"""
9-
try:
10-
import simplejson as json
11-
except ImportError: # pragma: no cover
12-
import json # NOQA
13-
import urllib2
9+
import json
1410

11+
from SoftLayer.transport import make_rest_api_call
1512
from SoftLayer.consts import API_PRIVATE_ENDPOINT_REST, USER_AGENT
1613
from SoftLayer.exceptions import SoftLayerAPIError, SoftLayerError
1714

@@ -64,26 +61,14 @@ def __init__(self, client=None, timeout=5):
6461

6562
def make_request(self, path):
6663
url = '/'.join([self.url, 'SoftLayer_Resource_Metadata', path])
67-
req = urllib2.Request(url)
68-
req.add_header('User-Agent', USER_AGENT)
69-
7064
try:
71-
resp = urllib2.urlopen(req, timeout=self.timeout)
72-
except urllib2.HTTPError, e: # pragma: no cover
73-
if e.code == 404:
65+
return make_rest_api_call('GET', url,
66+
http_headers={'User-Agent': USER_AGENT},
67+
timeout=self.timeout)
68+
except SoftLayerAPIError, e:
69+
if e.faultCode == 404:
7470
return None
75-
76-
try:
77-
content = json.loads(e.read())
78-
raise SoftLayerAPIError(content['code'], content['error'])
79-
except (ValueError, KeyError):
80-
pass
81-
82-
raise SoftLayerAPIError(e.code, e.reason)
83-
except urllib2.URLError, e:
84-
raise SoftLayerAPIError(0, e.reason)
85-
else:
86-
return resp.read()
71+
raise e
8772

8873
def get(self, name, param=None):
8974
""" Retreive a metadata attribute
@@ -104,11 +89,11 @@ def get(self, name, param=None):
10489
if not param:
10590
raise SoftLayerError(
10691
'Parameter required to get this attribute.')
107-
url = "%s/%s%s" % (self.attribs[name]['call'], param, extension)
92+
path = "%s/%s%s" % (self.attribs[name]['call'], param, extension)
10893
else:
109-
url = "%s%s" % (self.attribs[name]['call'], extension)
94+
path = "%s%s" % (self.attribs[name]['call'], extension)
11095

111-
data = self.make_request(url)
96+
data = self.make_request(path)
11297
if data and extension == '.json':
11398
return json.loads(data)
11499
return data

SoftLayer/tests/API/client_tests.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
try:
99
import unittest2 as unittest
1010
except ImportError:
11-
import unittest # NOQA
11+
import unittest # NOQA
1212

1313
from mock import patch, MagicMock, call
1414

@@ -118,15 +118,15 @@ def setUp(self):
118118
username='doesnotexist', api_key='issurelywrong',
119119
endpoint_url="ENDPOINT")
120120

121-
@patch('SoftLayer.API.make_api_call')
122-
def test_old_api(self, make_api_call):
121+
@patch('SoftLayer.API.make_xml_rpc_api_call')
122+
def test_old_api(self, make_xml_rpc_api_call):
123123
client = SoftLayer.API.Client(
124124
'SoftLayer_SERVICE', None, 'doesnotexist', 'issurelywrong',
125125
endpoint_url="ENDPOINT")
126126

127127
client.METHOD()
128128

129-
make_api_call.assert_called_with(
129+
make_xml_rpc_api_call.assert_called_with(
130130
'ENDPOINT/SoftLayer_SERVICE', 'METHOD', (),
131131
headers={
132132
'authenticate': {
@@ -138,8 +138,8 @@ def test_old_api(self, make_api_call):
138138
'User-Agent': USER_AGENT,
139139
})
140140

141-
@patch('SoftLayer.API.make_api_call')
142-
def test_complex_old_api(self, make_api_call):
141+
@patch('SoftLayer.API.make_xml_rpc_api_call')
142+
def test_complex_old_api(self, make_xml_rpc_api_call):
143143
client = SoftLayer.API.Client(
144144
'SoftLayer_SERVICE', None, 'doesnotexist', 'issurelywrong',
145145
endpoint_url="ENDPOINT")
@@ -156,7 +156,7 @@ def test_complex_old_api(self, make_api_call):
156156
'TYPE': {'obj': {'attribute': {'operation': '^= prefix'}}}},
157157
limit=9, offset=10)
158158

159-
make_api_call.assert_called_with(
159+
make_xml_rpc_api_call.assert_called_with(
160160
'ENDPOINT/SoftLayer_SERVICE', 'METHOD', (1234, ),
161161
headers={
162162
'SoftLayer_SERVICEObjectMask': {
@@ -181,10 +181,10 @@ def test_old_api_no_service(self):
181181
api_key='issurelywrong')
182182
self.assertRaises(SoftLayer.SoftLayerError, client.METHOD)
183183

184-
@patch('SoftLayer.API.make_api_call')
185-
def test_simple_call(self, make_api_call):
184+
@patch('SoftLayer.API.make_xml_rpc_api_call')
185+
def test_simple_call(self, make_xml_rpc_api_call):
186186
self.client['SERVICE'].METHOD()
187-
make_api_call.assert_called_with(
187+
make_xml_rpc_api_call.assert_called_with(
188188
'ENDPOINT/SoftLayer_SERVICE', 'METHOD', (),
189189
headers={
190190
'authenticate': {
@@ -196,8 +196,8 @@ def test_simple_call(self, make_api_call):
196196
'User-Agent': USER_AGENT,
197197
})
198198

199-
@patch('SoftLayer.API.make_api_call')
200-
def test_complex(self, make_api_call):
199+
@patch('SoftLayer.API.make_xml_rpc_api_call')
200+
def test_complex(self, make_xml_rpc_api_call):
201201
self.client['SERVICE'].METHOD(
202202
1234,
203203
id=5678,
@@ -207,7 +207,7 @@ def test_complex(self, make_api_call):
207207
'TYPE': {'obj': {'attribute': {'operation': '^= prefix'}}}},
208208
limit=9, offset=10)
209209

210-
make_api_call.assert_called_with(
210+
make_xml_rpc_api_call.assert_called_with(
211211
'ENDPOINT/SoftLayer_SERVICE', 'METHOD', (1234, ),
212212
headers={
213213
'SoftLayer_SERVICEObjectMask': {
@@ -227,11 +227,11 @@ def test_complex(self, make_api_call):
227227
'User-Agent': USER_AGENT,
228228
})
229229

230-
@patch('SoftLayer.API.make_api_call')
231-
def test_mask_call_v2(self, make_api_call):
230+
@patch('SoftLayer.API.make_xml_rpc_api_call')
231+
def test_mask_call_v2(self, make_xml_rpc_api_call):
232232
self.client['SERVICE'].METHOD(
233233
mask="mask[something[nested]]")
234-
make_api_call.assert_called_with(
234+
make_xml_rpc_api_call.assert_called_with(
235235
'ENDPOINT/SoftLayer_SERVICE', 'METHOD', (),
236236
headers={
237237
'authenticate': {
@@ -244,11 +244,11 @@ def test_mask_call_v2(self, make_api_call):
244244
'User-Agent': USER_AGENT,
245245
})
246246

247-
@patch('SoftLayer.API.make_api_call')
248-
def test_mask_call_v2_dot(self, make_api_call):
247+
@patch('SoftLayer.API.make_xml_rpc_api_call')
248+
def test_mask_call_v2_dot(self, make_xml_rpc_api_call):
249249
self.client['SERVICE'].METHOD(
250250
mask="mask.something.nested")
251-
make_api_call.assert_called_with(
251+
make_xml_rpc_api_call.assert_called_with(
252252
'ENDPOINT/SoftLayer_SERVICE', 'METHOD', (),
253253
headers={
254254
'authenticate': {
@@ -261,8 +261,8 @@ def test_mask_call_v2_dot(self, make_api_call):
261261
'User-Agent': USER_AGENT,
262262
})
263263

264-
@patch('SoftLayer.API.make_api_call')
265-
def test_mask_call_invalid_mask(self, make_api_call):
264+
@patch('SoftLayer.API.make_xml_rpc_api_call')
265+
def test_mask_call_invalid_mask(self, make_xml_rpc_api_call):
266266
try:
267267
self.client['SERVICE'].METHOD(mask="mask[something.nested")
268268
except SoftLayer.SoftLayerError, e:

SoftLayer/tests/API/functional_tests.py

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
try:
1212
import unittest2 as unittest
1313
except ImportError:
14-
import unittest # NOQA
15-
from mock import patch
16-
from SoftLayer.transport import xmlrpclib_transport
14+
import unittest # NOQA
1715

1816

1917
def get_creds():
@@ -38,14 +36,6 @@ def test_failed_auth(self):
3836
self.assertRaises(SoftLayer.SoftLayerAPIError,
3937
client.getPortalLoginToken)
4038

41-
@patch('SoftLayer.API.make_api_call', xmlrpclib_transport.make_api_call)
42-
def test_with_xmlrpc_transport(self):
43-
client = SoftLayer.Client(
44-
'SoftLayer_User_Customer', None, 'doesnotexist', 'issurelywrong',
45-
timeout=20)
46-
self.assertRaises(SoftLayer.SoftLayerAPIError,
47-
client.getPortalLoginToken)
48-
4939
def test_404(self):
5040
client = SoftLayer.Client(
5141
'SoftLayer_User_Customer', None, 'doesnotexist', 'issurelywrong',
@@ -60,35 +50,10 @@ def test_404(self):
6050
except:
6151
self.fail('No Exception Raised')
6252

63-
@patch('SoftLayer.API.make_api_call', xmlrpclib_transport.make_api_call)
64-
def test_404_with_xmlrpc_transport(self):
65-
client = SoftLayer.Client(
66-
'SoftLayer_User_Customer', None, 'doesnotexist', 'issurelywrong',
67-
timeout=20, endpoint_url='http://httpbin.org/status/404')
68-
69-
try:
70-
client.doSomething()
71-
except SoftLayer.SoftLayerAPIError, e:
72-
self.assertEqual(e.faultCode, 404)
73-
self.assertIn('NOT FOUND', e.faultString)
74-
self.assertIn('NOT FOUND', e.reason)
75-
7653
def test_no_hostname(self):
7754
try:
7855
# This test will fail if 'notvalidsoftlayer.com' becomes a thing
79-
SoftLayer.API.make_api_call(
80-
'http://notvalidsoftlayer.com', 'getObject')
81-
except SoftLayer.SoftLayerAPIError, e:
82-
self.assertEqual(e.faultCode, 0)
83-
self.assertIn('not known', e.faultString)
84-
self.assertIn('not known', e.reason)
85-
except:
86-
self.fail('No Exception Raised')
87-
88-
def test_no_hostname_with_xmlrpc_transport(self):
89-
try:
90-
# This test will fail if 'notvalidsoftlayer.com' becomes a thing
91-
xmlrpclib_transport.make_api_call(
56+
SoftLayer.API.make_xml_rpc_api_call(
9257
'http://notvalidsoftlayer.com', 'getObject')
9358
except SoftLayer.SoftLayerAPIError, e:
9459
self.assertEqual(e.faultCode, 0)

SoftLayer/tests/API/metadata_tests.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,8 @@
1010
try:
1111
import unittest2 as unittest
1212
except ImportError:
13-
import unittest # NOQA
13+
import unittest # NOQA
1414
from mock import patch, MagicMock
15-
import urllib2
16-
import sys
17-
18-
if sys.version_info >= (3,):
19-
REQ_PATH = 'urllib.request.Request'
20-
URLOPEN_PATH = 'urllib.request.urlopen'
21-
else:
22-
REQ_PATH = 'urllib2.Request'
23-
URLOPEN_PATH = 'urllib2.urlopen'
2415

2516

2617
class MetadataTests(unittest.TestCase):
@@ -96,17 +87,26 @@ def setUp(self):
9687
'SoftLayer_Resource_Metadata',
9788
'something.json'])
9889

99-
@patch(REQ_PATH)
100-
@patch(URLOPEN_PATH)
101-
def test_basic(self, urlopen, req):
90+
@patch('SoftLayer.metadata.make_rest_api_call')
91+
def test_basic(self, make_api_call):
10292
r = self.metadata.make_request('something.json')
103-
req.assert_called_with(self.url)
104-
self.assertEqual(r, urlopen().read())
105-
106-
@patch(REQ_PATH)
107-
@patch(URLOPEN_PATH)
108-
def test_raise_urlerror(self, urlopen, req):
109-
urlopen.side_effect = urllib2.URLError('Error')
93+
make_api_call.assert_called_with(
94+
'GET', self.url,
95+
timeout=5,
96+
http_headers={'User-Agent': 'SoftLayer Python v2.2.0'})
97+
self.assertEqual(make_api_call(), r)
98+
99+
@patch('SoftLayer.metadata.make_rest_api_call')
100+
def test_raise_error(self, make_api_call):
101+
make_api_call.side_effect = SoftLayer.SoftLayerAPIError(
102+
'faultCode', 'faultString')
110103
self.assertRaises(
111104
SoftLayer.SoftLayerAPIError,
112105
self.metadata.make_request, 'something.json')
106+
107+
@patch('SoftLayer.metadata.make_rest_api_call')
108+
def test_raise_404_error(self, make_api_call):
109+
make_api_call.side_effect = SoftLayer.SoftLayerAPIError(
110+
404, 'faultString')
111+
r = self.metadata.make_request('something.json')
112+
self.assertEqual(r, None)

0 commit comments

Comments
 (0)