From 98a338e3259e84ccf6fd8e6c2813acc48fa4dded Mon Sep 17 00:00:00 2001 From: Maxime Belanger Date: Thu, 31 Mar 2016 16:51:31 -0400 Subject: [PATCH 1/3] Handle different content types --- tests/ubersmith_request_get_test.py | 2 +- tests/ubersmith_request_post_test.py | 2 +- tests/ubersmith_request_test.py | 16 ++++++++++------ ubersmith_client/exceptions.py | 2 +- ubersmith_client/ubersmith_request.py | 19 +++++++++++-------- 5 files changed, 24 insertions(+), 17 deletions(-) diff --git a/tests/ubersmith_request_get_test.py b/tests/ubersmith_request_get_test.py index 155a976..ef0a19e 100644 --- a/tests/ubersmith_request_get_test.py +++ b/tests/ubersmith_request_get_test.py @@ -65,7 +65,7 @@ def test_api_get_method_returns_with_arguments(self, request_mock): expected_call() def expect_a_ubersmith_call(self, requests_mock, returning=None, **kwargs): - response = MagicMock(status_code=200) + response = MagicMock(status_code=200, headers={"content-type": "application/json"}) requests_mock.get = MagicMock(return_value=response) response.json = MagicMock(return_value=returning) diff --git a/tests/ubersmith_request_post_test.py b/tests/ubersmith_request_post_test.py index 81417db..d03bb4b 100644 --- a/tests/ubersmith_request_post_test.py +++ b/tests/ubersmith_request_post_test.py @@ -76,7 +76,7 @@ def test_api_raises_exception_with_if_data_status_is_false(self, requests_mock): assert_that(calling(ubersmith_api.client.miss), raises(ubersmith_client.exceptions.UbersmithException)) def expect_a_ubersmith_call_post(self, requests_mock, returning=None, status_code=200, **kwargs): - response = MagicMock(status_code=status_code) + response = MagicMock(status_code=status_code, headers={"content-type": "application/json"}) requests_mock.post = MagicMock(return_value=response) response.json = MagicMock(return_value=returning) diff --git a/tests/ubersmith_request_test.py b/tests/ubersmith_request_test.py index ef3d0ee..2bef6bc 100644 --- a/tests/ubersmith_request_test.py +++ b/tests/ubersmith_request_test.py @@ -16,10 +16,11 @@ import ubersmith_client from mock import Mock, patch -from hamcrest import assert_that, raises, calling +from hamcrest import assert_that, raises, calling, equal_to from requests.exceptions import ConnectionError, Timeout -from ubersmith_client.exceptions import UbersmithException, BadRequest, UnknownError, Forbidden, NotFound, Unauthorized, UbersmithConnectionError, \ +from ubersmith_client.exceptions import UbersmithException, BadRequest, UnknownError, Forbidden, NotFound, Unauthorized, \ + UbersmithConnectionError, \ UbersmithTimeout from tests.ubersmith_json.response_data_structure import a_response_data from ubersmith_client.ubersmith_request import UbersmithRequest @@ -32,8 +33,8 @@ def setUp(self): self.password = 'test' def test_process_ubersmith_response(self): - response = Mock() - response.status_code = 200 + response = Mock(status_code=200, headers={"content-type": "application/json"}) + json_data = { 'client_id': '1', 'first': 'Rick', @@ -45,9 +46,12 @@ def test_process_ubersmith_response(self): self.assertDictEqual(json_data, UbersmithRequest.process_ubersmith_response(response)) + def test_process_ubersmith_response_not_application_json(self): + response = Mock(status_code=200, headers={"content-type": "text/html"}, content="42") + assert_that(response.content, equal_to(UbersmithRequest.process_ubersmith_response(response))) + def test_process_ubersmith_response_raise_exception(self): - response = Mock() - response.status_code = 400 + response = Mock(status_code=400, headers={"content-type": "application/json"}) assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response), raises(BadRequest)) response.status_code = 401 diff --git a/ubersmith_client/exceptions.py b/ubersmith_client/exceptions.py index 08b075d..ca3159b 100644 --- a/ubersmith_client/exceptions.py +++ b/ubersmith_client/exceptions.py @@ -70,4 +70,4 @@ def __init__(self, url): class UbersmithTimeout(UbersmithException): def __init__(self, url, timeout): super(UbersmithTimeout, self)\ - .__init__(message='Trying to connect to {url} times out after {timeout}'.format(url=url, timeout=timeout)) + .__init__(message='Trying to connect to {url} timed out after {timeout} seconds'.format(url=url, timeout=timeout)) diff --git a/ubersmith_client/ubersmith_request.py b/ubersmith_client/ubersmith_request.py index 905ccfc..3324fbf 100644 --- a/ubersmith_client/ubersmith_request.py +++ b/ubersmith_client/ubersmith_request.py @@ -32,7 +32,7 @@ def __getattr__(self, function): @abstractmethod def __call__(self, **kwargs): - raise + raise AttributeError def _process_request(self, method, **kwargs): try: @@ -52,11 +52,14 @@ def process_ubersmith_response(response): if response.status_code < 200 or response.status_code >= 400: raise get_exception_for(status_code=response.status_code) - response_json = response.json() - if not response_json['status']: - raise UbersmithException( - response_json['error_code'], - response_json['error_message'] - ) + if response.headers['content-type'] == 'application/json': + response_json = response.json() + if not response_json['status']: + raise UbersmithException( + response_json['error_code'], + response_json['error_message'] + ) - return response.json()["data"] + return response.json()["data"] + + return response.content From 239201d3a2f297369b4e88f0c4ba419a4135a28d Mon Sep 17 00:00:00 2001 From: Marc Aubry Date: Thu, 31 Mar 2016 17:12:01 -0400 Subject: [PATCH 2/3] Single quote to be standard. pep8 --- tests/ubersmith_request_get_test.py | 4 ++-- tests/ubersmith_request_post_test.py | 6 +++--- tests/ubersmith_request_test.py | 10 ++++------ ubersmith_client/exceptions.py | 7 ++++--- ubersmith_client/ubersmith_request.py | 6 +++--- 5 files changed, 16 insertions(+), 17 deletions(-) diff --git a/tests/ubersmith_request_get_test.py b/tests/ubersmith_request_get_test.py index ef0a19e..e14d9db 100644 --- a/tests/ubersmith_request_get_test.py +++ b/tests/ubersmith_request_get_test.py @@ -12,9 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. import unittest + from hamcrest import assert_that, equal_to from mock import patch, MagicMock - import ubersmith_client from tests.ubersmith_json.response_data_structure import a_response_data @@ -65,7 +65,7 @@ def test_api_get_method_returns_with_arguments(self, request_mock): expected_call() def expect_a_ubersmith_call(self, requests_mock, returning=None, **kwargs): - response = MagicMock(status_code=200, headers={"content-type": "application/json"}) + response = MagicMock(status_code=200, headers={'content-type': 'application/json'}) requests_mock.get = MagicMock(return_value=response) response.json = MagicMock(return_value=returning) diff --git a/tests/ubersmith_request_post_test.py b/tests/ubersmith_request_post_test.py index d03bb4b..44d407b 100644 --- a/tests/ubersmith_request_post_test.py +++ b/tests/ubersmith_request_post_test.py @@ -12,9 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. import unittest + from hamcrest import assert_that, equal_to, calling, raises from mock import patch, MagicMock - import ubersmith_client from tests.ubersmith_json.response_data_structure import a_response_data @@ -69,14 +69,14 @@ def test_api_raises_exception_with_if_data_status_is_false(self, requests_mock): data = a_response_data(status=False, error_code=1, error_message='invalid method specified: client.miss', - data="schwifty") + data='schwifty') ubersmith_api = ubersmith_client.api.init(self.url, self.username, self.password) self.expect_a_ubersmith_call_post(requests_mock, method='client.miss', returning=data) assert_that(calling(ubersmith_api.client.miss), raises(ubersmith_client.exceptions.UbersmithException)) def expect_a_ubersmith_call_post(self, requests_mock, returning=None, status_code=200, **kwargs): - response = MagicMock(status_code=status_code, headers={"content-type": "application/json"}) + response = MagicMock(status_code=status_code, headers={'content-type': 'application/json'}) requests_mock.post = MagicMock(return_value=response) response.json = MagicMock(return_value=returning) diff --git a/tests/ubersmith_request_test.py b/tests/ubersmith_request_test.py index 2bef6bc..7220aff 100644 --- a/tests/ubersmith_request_test.py +++ b/tests/ubersmith_request_test.py @@ -15,10 +15,8 @@ import ubersmith_client from mock import Mock, patch - from hamcrest import assert_that, raises, calling, equal_to from requests.exceptions import ConnectionError, Timeout - from ubersmith_client.exceptions import UbersmithException, BadRequest, UnknownError, Forbidden, NotFound, Unauthorized, \ UbersmithConnectionError, \ UbersmithTimeout @@ -33,7 +31,7 @@ def setUp(self): self.password = 'test' def test_process_ubersmith_response(self): - response = Mock(status_code=200, headers={"content-type": "application/json"}) + response = Mock(status_code=200, headers={'content-type': 'application/json'}) json_data = { 'client_id': '1', @@ -47,11 +45,11 @@ def test_process_ubersmith_response(self): self.assertDictEqual(json_data, UbersmithRequest.process_ubersmith_response(response)) def test_process_ubersmith_response_not_application_json(self): - response = Mock(status_code=200, headers={"content-type": "text/html"}, content="42") + response = Mock(status_code=200, headers={'content-type': 'text/html'}, content='42') assert_that(response.content, equal_to(UbersmithRequest.process_ubersmith_response(response))) def test_process_ubersmith_response_raise_exception(self): - response = Mock(status_code=400, headers={"content-type": "application/json"}) + response = Mock(status_code=400, headers={'content-type': 'application/json'}) assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response), raises(BadRequest)) response.status_code = 401 @@ -69,7 +67,7 @@ def test_process_ubersmith_response_raise_exception(self): response.status_code = 200 response.json = Mock(return_value={'status': False, 'error_code': 42, 'error_message': 'come and watch tv'}) assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response), - raises(UbersmithException, "Error code 42 - message: come and watch tv")) + raises(UbersmithException, 'Error code 42 - message: come and watch tv')) @patch('ubersmith_client.ubersmith_request_post.requests') def test_api_method_returns_handle_connection_error_exception(self, requests_mock): diff --git a/ubersmith_client/exceptions.py b/ubersmith_client/exceptions.py index ca3159b..1724c44 100644 --- a/ubersmith_client/exceptions.py +++ b/ubersmith_client/exceptions.py @@ -64,10 +64,11 @@ def __init__(self, code): class UbersmithConnectionError(UbersmithException): def __init__(self, url): - super(UbersmithConnectionError, self).__init__(message="Could not connect to {0}".format(url)) + super(UbersmithConnectionError, self).__init__(message='Could not connect to {0}'.format(url)) class UbersmithTimeout(UbersmithException): def __init__(self, url, timeout): - super(UbersmithTimeout, self)\ - .__init__(message='Trying to connect to {url} timed out after {timeout} seconds'.format(url=url, timeout=timeout)) + super(UbersmithTimeout, self) \ + .__init__( + message='Trying to connect to {url} timed out after {timeout} seconds'.format(url=url, timeout=timeout)) diff --git a/ubersmith_client/ubersmith_request.py b/ubersmith_client/ubersmith_request.py index 3324fbf..6c65aad 100644 --- a/ubersmith_client/ubersmith_request.py +++ b/ubersmith_client/ubersmith_request.py @@ -44,8 +44,8 @@ def _process_request(self, method, **kwargs): raise UbersmithTimeout(self.url, self.timeout) def _build_request_params(self, kwargs): - _methods = ".".join(self.methods) - kwargs['method'] = "{0}.{1}".format(self.module, _methods) + _methods = '.'.join(self.methods) + kwargs['method'] = '{0}.{1}'.format(self.module, _methods) @staticmethod def process_ubersmith_response(response): @@ -60,6 +60,6 @@ def process_ubersmith_response(response): response_json['error_message'] ) - return response.json()["data"] + return response.json()['data'] return response.content From b9a1206fabbaed881c565c595a72a713eb016503 Mon Sep 17 00:00:00 2001 From: Marc Aubry Date: Thu, 31 Mar 2016 17:16:49 -0400 Subject: [PATCH 3/3] Now is better than never. --- ubersmith_client/ubersmith_request.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ubersmith_client/ubersmith_request.py b/ubersmith_client/ubersmith_request.py index 6c65aad..d77966e 100644 --- a/ubersmith_client/ubersmith_request.py +++ b/ubersmith_client/ubersmith_request.py @@ -60,6 +60,6 @@ def process_ubersmith_response(response): response_json['error_message'] ) - return response.json()['data'] + return response_json['data'] return response.content