Skip to content

Commit 8725aff

Browse files
committed
Merge pull request #4 from fguillot/adding-request-timeout
Add timeout to http requests
2 parents c6dfe65 + bbe71ac commit 8725aff

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

test-requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
nose==1.2.1
22
requests-mock==0.7.0
3-
pyhamcrest==1.8.1
3+
pyhamcrest==1.8.1
4+
flexmock

tests/api_test.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
# limitations under the License.
1414
import base64
1515
import unittest
16+
import requests
1617

18+
from flexmock import flexmock, flexmock_teardown
1719
from hamcrest import assert_that, equal_to, raises, calling
1820
import requests_mock
1921
from requests_mock.exceptions import NoMockAddress
@@ -28,6 +30,9 @@ def setUp(self):
2830
self.username = 'admin'
2931
self.password = 'test'
3032

33+
def tearDown(self):
34+
flexmock_teardown()
35+
3136
@requests_mock.mock()
3237
def test_api_method_returns_without_arguments(self, request_mock):
3338
json_data = [
@@ -173,6 +178,28 @@ def test_api_http_post_method_raises_on_result_414(self, request_mock):
173178

174179
assert_that(calling(ubersmith_api.support.ticket_submit.http_post), raises(UnknownError))
175180

181+
def test_api_http_timeout(self):
182+
payload = dict(status=True, data="plop")
183+
response = flexmock(status_code=200, json=lambda: payload)
184+
ubersmith_api = api.init(self.url, self.username, self.password, 666)
185+
186+
flexmock(requests).should_receive("get").with_args(
187+
url=self.url, auth=(self.username, self.password), timeout=666, params={'method': 'uber.method_list'}
188+
).and_return(response)
189+
190+
ubersmith_api.uber.method_list()
191+
192+
def test_api_http_default_timeout(self):
193+
payload = dict(status=True, data="plop")
194+
response = flexmock(status_code=200, json=lambda: payload)
195+
ubersmith_api = api.init(self.url, self.username, self.password)
196+
197+
flexmock(requests).should_receive("get").with_args(
198+
url=self.url, auth=(self.username, self.password), timeout=60, params={'method': 'uber.method_list'}
199+
).and_return(response)
200+
201+
ubersmith_api.uber.method_list()
202+
176203
@requests_mock.mock()
177204
def test_api_http_post_method_raises_on_result_500(self, request_mock):
178205
json_data = {

ubersmith_client/api.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,30 @@
1616
from ubersmith_client.exceptions import UbersmithException, get_exception_for
1717

1818

19-
def init(url, user, password):
20-
return UbersmithApi(url, user, password)
19+
def init(url, user, password, timeout=60):
20+
return UbersmithApi(url, user, password, timeout)
2121

2222

2323
class UbersmithApi(object):
24-
def __init__(self, url, user, password):
24+
def __init__(self, url, user, password, timeout):
2525
self.url = url
2626
self.user = user
2727
self.password = password
28+
self.timeout = timeout
2829

2930
def __getattr__(self, module):
30-
return UbersmithRequest(self.url, self.user, self.password, module)
31+
return UbersmithRequest(self.url, self.user, self.password, module, self.timeout)
3132

3233

3334
class UbersmithRequest(object):
34-
def __init__(self, url, user, password, module):
35+
def __init__(self, url, user, password, module, timeout):
3536
self.url = url
3637
self.user = user
3738
self.password = password
3839
self.module = module
3940
self.methods = []
4041
self.http_methods = {'GET': 'get', 'POST': 'post'}
42+
self.timeout = timeout
4143

4244
def __getattr__(self, function):
4345
self.methods.append(function)
@@ -48,7 +50,13 @@ def __call__(self, **kwargs):
4850

4951
def process_request(self, http_method, **kwargs):
5052
callable_http_method = getattr(requests, http_method)
51-
response = callable_http_method(self.url, auth=(self.user, self.password), **kwargs)
53+
54+
response = callable_http_method(
55+
self.url,
56+
auth=(self.user, self.password),
57+
timeout=self.timeout,
58+
**kwargs
59+
)
5260

5361
if response.status_code < 200 or response.status_code >= 400:
5462
raise get_exception_for(status_code=response.status_code)

0 commit comments

Comments
 (0)