Skip to content

Commit 40a2426

Browse files
committed
Timeout added for opener
Fixes #20
1 parent adf4a31 commit 40a2426

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

python_http_client/client.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,20 @@ def _build_client(self, name=None):
143143
url_path=url_path,
144144
append_slash=self.append_slash)
145145

146-
def _make_request(self, opener, request):
146+
def _make_request(self, opener, request, timeout=4):
147147
"""Make the API call and return the response. This is separated into
148148
it's own function, so we can mock it easily for testing.
149149
150150
:param opener:
151151
:type opener:
152152
:param request: url payload to request
153153
:type request: urllib.Request object
154+
:param timeout: timeout for request in seconds, default 4 sec
155+
:type integer:
154156
:return: urllib response
155157
"""
156158
try:
157-
return opener.open(request)
159+
return opener.open(request, timeout=timeout)
158160
except HTTPError as err:
159161
exc = handle_error(err)
160162
exc.__cause__ = None
@@ -224,7 +226,7 @@ def http_request(*_, **kwargs):
224226
if data and not ('Content-Type' in self.request_headers):
225227
request.add_header('Content-Type', 'application/json')
226228
request.get_method = lambda: method
227-
return Response(self._make_request(opener, request))
229+
return Response(self._make_request(opener, request, timeout=4))
228230
return http_request
229231
else:
230232
# Add a segment to the URL

tests/test_unit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def __init__(self, host, response_code):
5959
self.response_code = 200
6060
Client.__init__(self, host)
6161

62-
def _make_request(self, opener, request):
62+
def _make_request(self, opener, request, timeout):
6363
if 200 <= self.response_code <299: # if successsful code
6464
return MockResponse(self.response_code)
6565
else:

0 commit comments

Comments
 (0)