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
39 changes: 0 additions & 39 deletions tests/api_test.py

This file was deleted.

27 changes: 25 additions & 2 deletions tests/ubersmith_request_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,25 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
from mock import Mock

import ubersmith_client
from mock import Mock, patch

from hamcrest import assert_that, raises, calling
from requests.exceptions import ConnectionError, Timeout

from ubersmith_client.exceptions import UbersmithException, BadRequest, UnknownError, Forbidden, NotFound, Unauthorized
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


class UbersmithRequestTest(unittest.TestCase):
def setUp(self):
self.url = 'http://ubersmith.example.com/'
self.username = 'admin'
self.password = 'test'

def test_process_ubersmith_response(self):
response = Mock()
response.status_code = 200
Expand Down Expand Up @@ -57,3 +66,17 @@ def test_process_ubersmith_response_raise_exception(self):
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"))

@patch('ubersmith_client.ubersmith_request_post.requests')
def test_api_method_returns_handle_connection_error_exception(self, requests_mock):
ubersmith_api = ubersmith_client.api.init(self.url, self.username, self.password)
requests_mock.post = Mock(side_effect=ConnectionError())

assert_that(calling(ubersmith_api.client.list), raises(UbersmithConnectionError))

@patch('ubersmith_client.ubersmith_request_post.requests')
def test_api_method_returns_handle_timeout_exception(self, requests_mock):
ubersmith_api = ubersmith_client.api.init(self.url, self.username, self.password)
requests_mock.post = Mock(side_effect=Timeout())

assert_that(calling(ubersmith_api.client.list), raises(UbersmithTimeout))
10 changes: 1 addition & 9 deletions ubersmith_client/ubersmith_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from requests.exceptions import ConnectionError, Timeout

from ubersmith_client.exceptions import UbersmithConnectionError, UbersmithTimeout
from ubersmith_client.ubersmith_request_get import UbersmithRequestGet
from ubersmith_client.ubersmith_request_post import UbersmithRequestPost

Expand All @@ -26,9 +23,4 @@ def __init__(self, url, user, password, timeout, use_http_get):
self.ubersmith_request = UbersmithRequestGet if use_http_get else UbersmithRequestPost

def __getattr__(self, module):
try:
return self.ubersmith_request(self.url, self.user, self.password, module, self.timeout)
except ConnectionError:
raise UbersmithConnectionError(self.url)
except Timeout:
raise UbersmithTimeout(self.url, self.timeout)
return self.ubersmith_request(self.url, self.user, self.password, module, self.timeout)
13 changes: 12 additions & 1 deletion ubersmith_client/ubersmith_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from abc import abstractmethod
from requests import Timeout, ConnectionError

from ubersmith_client.exceptions import get_exception_for, UbersmithException
from ubersmith_client.exceptions import get_exception_for, UbersmithException, UbersmithConnectionError, \
UbersmithTimeout


class UbersmithRequest(object):
Expand All @@ -32,6 +34,15 @@ def __getattr__(self, function):
def __call__(self, **kwargs):
raise

def _process_request(self, method, **kwargs):
try:
return method(**kwargs)

except ConnectionError:
raise UbersmithConnectionError(self.url)
except Timeout:
raise UbersmithTimeout(self.url, self.timeout)

def _build_request_params(self, kwargs):
_methods = ".".join(self.methods)
kwargs['method'] = "{0}.{1}".format(self.module, _methods)
Expand Down
13 changes: 5 additions & 8 deletions ubersmith_client/ubersmith_request_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@


class UbersmithRequestGet(UbersmithRequest):
def __getattr__(self, function):
self.methods.append(function)
return self

def __call__(self, **kwargs):
self._build_request_params(kwargs)

response = requests.get(url=self.url,
auth=(self.user, self.password),
timeout=self.timeout,
params=kwargs)
response = self._process_request(method=requests.get,
url=self.url,
auth=(self.user, self.password),
timeout=self.timeout,
params=kwargs)

return UbersmithRequest.process_ubersmith_response(response)
9 changes: 5 additions & 4 deletions ubersmith_client/ubersmith_request_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ class UbersmithRequestPost(UbersmithRequest):
def __call__(self, **kwargs):
self._build_request_params(kwargs)

response = requests.post(url=self.url,
auth=(self.user, self.password),
timeout=self.timeout,
data=kwargs)
response = self._process_request(method=requests.post,
url=self.url,
auth=(self.user, self.password),
timeout=self.timeout,
data=kwargs)

return UbersmithRequest.process_ubersmith_response(response)