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
… exception
  • Loading branch information
s-block committed Dec 2, 2014
1 parent d2a7a6d commit 760c667
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 @@ -84,6 +84,7 @@ def __call__(self, id=None, format=None, url_override=None):
def _request(self, method, data=None, files=None, params=None):
s = self._store["serializer"]
url = self.url()
timeout = self._store.get("timeout", None)

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

Expand All @@ -92,7 +93,7 @@ def _request(self, method, data=None, files=None, params=None):
if data is not None:
data = s.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:
raise exceptions.HttpClientError("Client Error %s: %s" % (resp.status_code, url), response=resp, content=resp.content)
Expand Down Expand Up @@ -179,7 +180,7 @@ def delete(self, **kwargs):

class API(ResourceAttributesMixin, object):

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 @@ -193,6 +194,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 @@ -36,7 +36,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 @@ -65,7 +66,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 @@ -99,7 +101,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 @@ -128,7 +131,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 @@ -162,7 +166,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 @@ -191,7 +196,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 @@ -225,7 +231,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 @@ -254,7 +261,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 @@ -297,7 +305,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 @@ -355,3 +364,19 @@ def test_api(self):

def test_url(self):
self.assertEqual(self.base_resource.url(), "http://example/api/v1/test")

def test_connect_timeout(self):

client = slumber.API(base_url='http://httpbin.org/', 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/', 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 760c667

Please sign in to comment.