Skip to content

Commit 60f5aee

Browse files
committed
Merge pull request #9 from internap/refactored_exception_handling
Refactored broken exception handling
2 parents 1b77c12 + 693383e commit 60f5aee

File tree

6 files changed

+48
-63
lines changed

6 files changed

+48
-63
lines changed

tests/api_test.py

Lines changed: 0 additions & 39 deletions
This file was deleted.

tests/ubersmith_request_test.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,25 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import unittest
15-
from mock import Mock
15+
16+
import ubersmith_client
17+
from mock import Mock, patch
1618

1719
from hamcrest import assert_that, raises, calling
20+
from requests.exceptions import ConnectionError, Timeout
1821

19-
from ubersmith_client.exceptions import UbersmithException, BadRequest, UnknownError, Forbidden, NotFound, Unauthorized
22+
from ubersmith_client.exceptions import UbersmithException, BadRequest, UnknownError, Forbidden, NotFound, Unauthorized, UbersmithConnectionError, \
23+
UbersmithTimeout
2024
from tests.ubersmith_json.response_data_structure import a_response_data
2125
from ubersmith_client.ubersmith_request import UbersmithRequest
2226

2327

2428
class UbersmithRequestTest(unittest.TestCase):
29+
def setUp(self):
30+
self.url = 'http://ubersmith.example.com/'
31+
self.username = 'admin'
32+
self.password = 'test'
33+
2534
def test_process_ubersmith_response(self):
2635
response = Mock()
2736
response.status_code = 200
@@ -57,3 +66,17 @@ def test_process_ubersmith_response_raise_exception(self):
5766
response.json = Mock(return_value={'status': False, 'error_code': 42, 'error_message': 'come and watch tv'})
5867
assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response),
5968
raises(UbersmithException, "Error code 42 - message: come and watch tv"))
69+
70+
@patch('ubersmith_client.ubersmith_request_post.requests')
71+
def test_api_method_returns_handle_connection_error_exception(self, requests_mock):
72+
ubersmith_api = ubersmith_client.api.init(self.url, self.username, self.password)
73+
requests_mock.post = Mock(side_effect=ConnectionError())
74+
75+
assert_that(calling(ubersmith_api.client.list), raises(UbersmithConnectionError))
76+
77+
@patch('ubersmith_client.ubersmith_request_post.requests')
78+
def test_api_method_returns_handle_timeout_exception(self, requests_mock):
79+
ubersmith_api = ubersmith_client.api.init(self.url, self.username, self.password)
80+
requests_mock.post = Mock(side_effect=Timeout())
81+
82+
assert_that(calling(ubersmith_api.client.list), raises(UbersmithTimeout))

ubersmith_client/ubersmith_api.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
13-
from requests.exceptions import ConnectionError, Timeout
14-
15-
from ubersmith_client.exceptions import UbersmithConnectionError, UbersmithTimeout
1613
from ubersmith_client.ubersmith_request_get import UbersmithRequestGet
1714
from ubersmith_client.ubersmith_request_post import UbersmithRequestPost
1815

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

2825
def __getattr__(self, module):
29-
try:
30-
return self.ubersmith_request(self.url, self.user, self.password, module, self.timeout)
31-
except ConnectionError:
32-
raise UbersmithConnectionError(self.url)
33-
except Timeout:
34-
raise UbersmithTimeout(self.url, self.timeout)
26+
return self.ubersmith_request(self.url, self.user, self.password, module, self.timeout)

ubersmith_client/ubersmith_request.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313
from abc import abstractmethod
14+
from requests import Timeout, ConnectionError
1415

15-
from ubersmith_client.exceptions import get_exception_for, UbersmithException
16+
from ubersmith_client.exceptions import get_exception_for, UbersmithException, UbersmithConnectionError, \
17+
UbersmithTimeout
1618

1719

1820
class UbersmithRequest(object):
@@ -32,6 +34,15 @@ def __getattr__(self, function):
3234
def __call__(self, **kwargs):
3335
raise
3436

37+
def _process_request(self, method, **kwargs):
38+
try:
39+
return method(**kwargs)
40+
41+
except ConnectionError:
42+
raise UbersmithConnectionError(self.url)
43+
except Timeout:
44+
raise UbersmithTimeout(self.url, self.timeout)
45+
3546
def _build_request_params(self, kwargs):
3647
_methods = ".".join(self.methods)
3748
kwargs['method'] = "{0}.{1}".format(self.module, _methods)

ubersmith_client/ubersmith_request_get.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,13 @@
1616

1717

1818
class UbersmithRequestGet(UbersmithRequest):
19-
def __getattr__(self, function):
20-
self.methods.append(function)
21-
return self
22-
2319
def __call__(self, **kwargs):
2420
self._build_request_params(kwargs)
2521

26-
response = requests.get(url=self.url,
27-
auth=(self.user, self.password),
28-
timeout=self.timeout,
29-
params=kwargs)
22+
response = self._process_request(method=requests.get,
23+
url=self.url,
24+
auth=(self.user, self.password),
25+
timeout=self.timeout,
26+
params=kwargs)
3027

3128
return UbersmithRequest.process_ubersmith_response(response)

ubersmith_client/ubersmith_request_post.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ class UbersmithRequestPost(UbersmithRequest):
1919
def __call__(self, **kwargs):
2020
self._build_request_params(kwargs)
2121

22-
response = requests.post(url=self.url,
23-
auth=(self.user, self.password),
24-
timeout=self.timeout,
25-
data=kwargs)
22+
response = self._process_request(method=requests.post,
23+
url=self.url,
24+
auth=(self.user, self.password),
25+
timeout=self.timeout,
26+
data=kwargs)
2627

2728
return UbersmithRequest.process_ubersmith_response(response)

0 commit comments

Comments
 (0)