Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tests/ubersmith_request_get_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions tests/ubersmith_request_post_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
response = MagicMock(status_code=status_code, headers={'content-type': 'application/json'})
requests_mock.post = MagicMock(return_value=response)
response.json = MagicMock(return_value=returning)

Expand Down
20 changes: 11 additions & 9 deletions tests/ubersmith_request_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@

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
Expand All @@ -32,8 +31,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',
Expand All @@ -45,9 +44,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
Expand All @@ -65,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):
Expand Down
7 changes: 4 additions & 3 deletions ubersmith_client/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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} times out after {timeout}'.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))
23 changes: 13 additions & 10 deletions ubersmith_client/ubersmith_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __getattr__(self, function):

@abstractmethod
def __call__(self, **kwargs):
raise
raise AttributeError

def _process_request(self, method, **kwargs):
try:
Expand All @@ -44,19 +44,22 @@ 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):
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