Skip to content

Commit

Permalink
Add timeout to pass through to requests and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
s-block committed Dec 2, 2014
1 parent d2a7a6d commit 34c171c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 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
49 changes: 39 additions & 10 deletions tests/resource.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import sys
import mock
import requests
import slumber
import slumber.serialize
import unittest2 as unittest
import SimpleHTTPServer
import SocketServer
import threading

from slumber import exceptions

Expand Down Expand Up @@ -36,7 +38,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 +68,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 +103,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 +133,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 +168,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 +198,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 +233,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 +263,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 +307,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 +366,21 @@ def test_api(self):

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

def test_timeout(self):
handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = TestTCPServer(("", 8001), handler)
httpd_thread = threading.Thread(target=httpd.serve_forever)
httpd_thread.setDaemon(True)
httpd_thread.start()

client = slumber.API(base_url="http://localhost:8001", timeout=(0.000000000000001, 0.000000000000001))

with self.assertRaises(requests.exceptions.ConnectTimeout):
client.test.get()

httpd.shutdown()


class TestTCPServer(SocketServer.TCPServer):
allow_reuse_address = True

0 comments on commit 34c171c

Please sign in to comment.