Skip to content

Commit c5bb98c

Browse files
committed
Merge pull request #10 from internap/handle-content-type
Handle different content types
2 parents 60f5aee + b9a1206 commit c5bb98c

File tree

5 files changed

+33
-27
lines changed

5 files changed

+33
-27
lines changed

tests/ubersmith_request_get_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import unittest
15+
1516
from hamcrest import assert_that, equal_to
1617
from mock import patch, MagicMock
17-
1818
import ubersmith_client
1919
from tests.ubersmith_json.response_data_structure import a_response_data
2020

@@ -65,7 +65,7 @@ def test_api_get_method_returns_with_arguments(self, request_mock):
6565
expected_call()
6666

6767
def expect_a_ubersmith_call(self, requests_mock, returning=None, **kwargs):
68-
response = MagicMock(status_code=200)
68+
response = MagicMock(status_code=200, headers={'content-type': 'application/json'})
6969
requests_mock.get = MagicMock(return_value=response)
7070
response.json = MagicMock(return_value=returning)
7171

tests/ubersmith_request_post_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import unittest
15+
1516
from hamcrest import assert_that, equal_to, calling, raises
1617
from mock import patch, MagicMock
17-
1818
import ubersmith_client
1919
from tests.ubersmith_json.response_data_structure import a_response_data
2020

@@ -69,14 +69,14 @@ def test_api_raises_exception_with_if_data_status_is_false(self, requests_mock):
6969
data = a_response_data(status=False,
7070
error_code=1,
7171
error_message='invalid method specified: client.miss',
72-
data="schwifty")
72+
data='schwifty')
7373
ubersmith_api = ubersmith_client.api.init(self.url, self.username, self.password)
7474

7575
self.expect_a_ubersmith_call_post(requests_mock, method='client.miss', returning=data)
7676
assert_that(calling(ubersmith_api.client.miss), raises(ubersmith_client.exceptions.UbersmithException))
7777

7878
def expect_a_ubersmith_call_post(self, requests_mock, returning=None, status_code=200, **kwargs):
79-
response = MagicMock(status_code=status_code)
79+
response = MagicMock(status_code=status_code, headers={'content-type': 'application/json'})
8080
requests_mock.post = MagicMock(return_value=response)
8181
response.json = MagicMock(return_value=returning)
8282

tests/ubersmith_request_test.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515

1616
import ubersmith_client
1717
from mock import Mock, patch
18-
19-
from hamcrest import assert_that, raises, calling
18+
from hamcrest import assert_that, raises, calling, equal_to
2019
from requests.exceptions import ConnectionError, Timeout
21-
22-
from ubersmith_client.exceptions import UbersmithException, BadRequest, UnknownError, Forbidden, NotFound, Unauthorized, UbersmithConnectionError, \
20+
from ubersmith_client.exceptions import UbersmithException, BadRequest, UnknownError, Forbidden, NotFound, Unauthorized, \
21+
UbersmithConnectionError, \
2322
UbersmithTimeout
2423
from tests.ubersmith_json.response_data_structure import a_response_data
2524
from ubersmith_client.ubersmith_request import UbersmithRequest
@@ -32,8 +31,8 @@ def setUp(self):
3231
self.password = 'test'
3332

3433
def test_process_ubersmith_response(self):
35-
response = Mock()
36-
response.status_code = 200
34+
response = Mock(status_code=200, headers={'content-type': 'application/json'})
35+
3736
json_data = {
3837
'client_id': '1',
3938
'first': 'Rick',
@@ -45,9 +44,12 @@ def test_process_ubersmith_response(self):
4544

4645
self.assertDictEqual(json_data, UbersmithRequest.process_ubersmith_response(response))
4746

47+
def test_process_ubersmith_response_not_application_json(self):
48+
response = Mock(status_code=200, headers={'content-type': 'text/html'}, content='42')
49+
assert_that(response.content, equal_to(UbersmithRequest.process_ubersmith_response(response)))
50+
4851
def test_process_ubersmith_response_raise_exception(self):
49-
response = Mock()
50-
response.status_code = 400
52+
response = Mock(status_code=400, headers={'content-type': 'application/json'})
5153
assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response), raises(BadRequest))
5254

5355
response.status_code = 401
@@ -65,7 +67,7 @@ def test_process_ubersmith_response_raise_exception(self):
6567
response.status_code = 200
6668
response.json = Mock(return_value={'status': False, 'error_code': 42, 'error_message': 'come and watch tv'})
6769
assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response),
68-
raises(UbersmithException, "Error code 42 - message: come and watch tv"))
70+
raises(UbersmithException, 'Error code 42 - message: come and watch tv'))
6971

7072
@patch('ubersmith_client.ubersmith_request_post.requests')
7173
def test_api_method_returns_handle_connection_error_exception(self, requests_mock):

ubersmith_client/exceptions.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ def __init__(self, code):
6464

6565
class UbersmithConnectionError(UbersmithException):
6666
def __init__(self, url):
67-
super(UbersmithConnectionError, self).__init__(message="Could not connect to {0}".format(url))
67+
super(UbersmithConnectionError, self).__init__(message='Could not connect to {0}'.format(url))
6868

6969

7070
class UbersmithTimeout(UbersmithException):
7171
def __init__(self, url, timeout):
72-
super(UbersmithTimeout, self)\
73-
.__init__(message='Trying to connect to {url} times out after {timeout}'.format(url=url, timeout=timeout))
72+
super(UbersmithTimeout, self) \
73+
.__init__(
74+
message='Trying to connect to {url} timed out after {timeout} seconds'.format(url=url, timeout=timeout))

ubersmith_client/ubersmith_request.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def __getattr__(self, function):
3232

3333
@abstractmethod
3434
def __call__(self, **kwargs):
35-
raise
35+
raise AttributeError
3636

3737
def _process_request(self, method, **kwargs):
3838
try:
@@ -44,19 +44,22 @@ def _process_request(self, method, **kwargs):
4444
raise UbersmithTimeout(self.url, self.timeout)
4545

4646
def _build_request_params(self, kwargs):
47-
_methods = ".".join(self.methods)
48-
kwargs['method'] = "{0}.{1}".format(self.module, _methods)
47+
_methods = '.'.join(self.methods)
48+
kwargs['method'] = '{0}.{1}'.format(self.module, _methods)
4949

5050
@staticmethod
5151
def process_ubersmith_response(response):
5252
if response.status_code < 200 or response.status_code >= 400:
5353
raise get_exception_for(status_code=response.status_code)
5454

55-
response_json = response.json()
56-
if not response_json['status']:
57-
raise UbersmithException(
58-
response_json['error_code'],
59-
response_json['error_message']
60-
)
55+
if response.headers['content-type'] == 'application/json':
56+
response_json = response.json()
57+
if not response_json['status']:
58+
raise UbersmithException(
59+
response_json['error_code'],
60+
response_json['error_message']
61+
)
6162

62-
return response.json()["data"]
63+
return response_json['data']
64+
65+
return response.content

0 commit comments

Comments
 (0)