Skip to content

Commit

Permalink
pass timeout through to python requests and add tests to test timeout…
Browse files Browse the repository at this point in the history
… exceptions
  • Loading branch information
s-block committed Jul 22, 2015
1 parent 4c4b7bc commit 123f328
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
6 changes: 4 additions & 2 deletions slumber/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def __call__(self, id=None, format=None, url_override=None):
def _request(self, method, data=None, files=None, params=None):
serializer = self._store["serializer"]
url = self.url()
timeout = self._store.get("timeout", None)

headers = {"accept": serializer.get_content_type()}

Expand All @@ -94,7 +95,7 @@ def _request(self, method, data=None, files=None, params=None):
if data is not None:
data = serializer.dumps(data)

resp = self._store["session"].request(method, url, data=data, params=params, files=files, headers=headers)
resp = self._store["session"].request(method, url, data=data, params=params, files=files, headers=headers, timeout=timeout)

if 400 <= resp.status_code <= 499:
exception_class = exceptions.HttpNotFoundError if resp.status_code == 404 else exceptions.HttpClientError
Expand Down Expand Up @@ -193,7 +194,7 @@ class API(ResourceAttributesMixin, object):

resource_class = Resource

def __init__(self, base_url=None, auth=None, format=None, append_slash=True, session=None, serializer=None):
def __init__(self, base_url=None, auth=None, format=None, append_slash=True, session=None, serializer=None, timeout=None):
if serializer is None:
serializer = Serializer(default=format)

Expand All @@ -209,6 +210,7 @@ def __init__(self, base_url=None, auth=None, format=None, append_slash=True, ses
"append_slash": append_slash,
"session": session,
"serializer": serializer,
"timeout": timeout,
}

# Do some Checks for Required Values
Expand Down
43 changes: 34 additions & 9 deletions tests/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def test_get_200_json(self):
data=None,
files=None,
params=None,
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()}
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()},
timeout=None
)

resp = self.base_resource.get()
Expand Down Expand Up @@ -66,7 +67,8 @@ def test_get_200_text(self):
data=None,
files=None,
params=None,
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()}
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()},
timeout=None
)

resp = self.base_resource.get()
Expand Down Expand Up @@ -163,7 +165,8 @@ def test_post_201_redirect(self):
data=None,
files=None,
params=None,
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()}
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()},
timeout=None
)

resp = self.base_resource.post(data={'foo': 'bar'})
Expand Down Expand Up @@ -192,7 +195,8 @@ def test_post_decodable_response(self):
data=None,
files=None,
params=None,
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()}
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()},
timeout=None
)

resp = self.base_resource.post(data={'foo': 'bar'})
Expand Down Expand Up @@ -226,7 +230,8 @@ def test_patch_201_redirect(self):
data=None,
files=None,
params=None,
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()}
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()},
timeout=None
)

resp = self.base_resource.patch(data={'foo': 'bar'})
Expand Down Expand Up @@ -255,7 +260,8 @@ def test_patch_decodable_response(self):
data=None,
files=None,
params=None,
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()}
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()},
timeout=None
)

resp = self.base_resource.patch(data={'foo': 'bar'})
Expand Down Expand Up @@ -289,7 +295,8 @@ def test_put_201_redirect(self):
data=None,
files=None,
params=None,
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()}
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()},
timeout=None
)

resp = self.base_resource.put(data={'foo': 'bar'})
Expand Down Expand Up @@ -318,7 +325,8 @@ def test_put_decodable_response(self):
data=None,
files=None,
params=None,
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()}
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()},
timeout=None
)

resp = self.base_resource.put(data={'foo': 'bar'})
Expand Down Expand Up @@ -377,7 +385,8 @@ def test_get_200_subresource_json(self):
data=None,
files=None,
params=None,
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()}
headers={"content-type": self.base_resource._store["serializer"].get_content_type(), "accept": self.base_resource._store["serializer"].get_content_type()},
timeout=None
)

resp = self.base_resource.get()
Expand Down Expand Up @@ -543,3 +552,19 @@ def test_unicode_decodable_response(self):
resp = self.base_resource.post(data={'foo': 'bar'})
expected = b'Pr\xc3\xa9paratoire'.decode('utf8')
self.assertEqual(resp['result'], expected)

def test_connect_timeout(self):

client = slumber.API(base_url='http://httpbin.org/', append_slash=False, timeout=(0.00000000001, None))

with self.assertRaises(requests.exceptions.ConnectTimeout):
client.delay(10).get()
assert False, "The connect() request should time out."

def test_read_timeout(self):

client = slumber.API(base_url='http://httpbin.org/', append_slash=False, timeout=(None, 0.01))

with self.assertRaises(requests.exceptions.ReadTimeout):
client.delay(10).get()
assert False, "The recv() request should time out."

0 comments on commit 123f328

Please sign in to comment.