Skip to content

Commit 9ad21f4

Browse files
authored
feat(core): change default api_request() timeout to non-None (#10219)
1 parent 8a0e129 commit 9ad21f4

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

core/google/cloud/_http.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
'extra_headers' instead.
4747
"""
4848

49+
_DEFAULT_TIMEOUT = 60 # in seconds
50+
4951

5052
class Connection(object):
5153
"""A generic connection to Google Cloud Platform.
@@ -222,7 +224,7 @@ def _make_request(
222224
content_type=None,
223225
headers=None,
224226
target_object=None,
225-
timeout=None,
227+
timeout=_DEFAULT_TIMEOUT,
226228
):
227229
"""A low level method to send a request to the API.
228230
@@ -253,7 +255,7 @@ def _make_request(
253255
254256
:type timeout: float or tuple
255257
:param timeout: (optional) The amount of time, in seconds, to wait
256-
for the server response. By default, the method waits indefinitely.
258+
for the server response.
257259
258260
Can also be passed as a tuple (connect_timeout, read_timeout).
259261
See :meth:`requests.Session.request` documentation for details.
@@ -276,7 +278,7 @@ def _make_request(
276278
)
277279

278280
def _do_request(
279-
self, method, url, headers, data, target_object, timeout=None
281+
self, method, url, headers, data, target_object, timeout=_DEFAULT_TIMEOUT
280282
): # pylint: disable=unused-argument
281283
"""Low-level helper: perform the actual API request over HTTP.
282284
@@ -301,7 +303,7 @@ def _do_request(
301303
302304
:type timeout: float or tuple
303305
:param timeout: (optional) The amount of time, in seconds, to wait
304-
for the server response. By default, the method waits indefinitely.
306+
for the server response.
305307
306308
Can also be passed as a tuple (connect_timeout, read_timeout).
307309
See :meth:`requests.Session.request` documentation for details.
@@ -325,7 +327,7 @@ def api_request(
325327
api_version=None,
326328
expect_json=True,
327329
_target_object=None,
328-
timeout=None,
330+
timeout=_DEFAULT_TIMEOUT,
329331
):
330332
"""Make a request over the HTTP transport to the API.
331333
@@ -382,7 +384,7 @@ def api_request(
382384
383385
:type timeout: float or tuple
384386
:param timeout: (optional) The amount of time, in seconds, to wait
385-
for the server response. By default, the method waits indefinitely.
387+
for the server response.
386388
387389
Can also be passed as a tuple (connect_timeout, read_timeout).
388390
See :meth:`requests.Session.request` documentation for details.

core/tests/unit/test__http.py

+26-8
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ class TestJSONConnection(unittest.TestCase):
146146
JSON_HEADERS = {"content-type": "application/json"}
147147
EMPTY_JSON_RESPONSE = make_response(content=b"{}", headers=JSON_HEADERS)
148148

149+
@staticmethod
150+
def _get_default_timeout():
151+
from google.cloud._http import _DEFAULT_TIMEOUT
152+
153+
return _DEFAULT_TIMEOUT
154+
149155
@staticmethod
150156
def _get_target_class():
151157
from google.cloud._http import JSONConnection
@@ -217,7 +223,11 @@ def test__make_request_no_data_no_content_type_no_headers(self):
217223
CLIENT_INFO_HEADER: conn.user_agent,
218224
}
219225
http.request.assert_called_once_with(
220-
method="GET", url=url, headers=expected_headers, data=None, timeout=None
226+
method="GET",
227+
url=url,
228+
headers=expected_headers,
229+
data=None,
230+
timeout=self._get_default_timeout(),
221231
)
222232

223233
def test__make_request_w_data_no_extra_headers(self):
@@ -238,7 +248,11 @@ def test__make_request_w_data_no_extra_headers(self):
238248
CLIENT_INFO_HEADER: conn.user_agent,
239249
}
240250
http.request.assert_called_once_with(
241-
method="GET", url=url, headers=expected_headers, data=data, timeout=None
251+
method="GET",
252+
url=url,
253+
headers=expected_headers,
254+
data=data,
255+
timeout=self._get_default_timeout(),
242256
)
243257

244258
def test__make_request_w_extra_headers(self):
@@ -258,7 +272,11 @@ def test__make_request_w_extra_headers(self):
258272
CLIENT_INFO_HEADER: conn.user_agent,
259273
}
260274
http.request.assert_called_once_with(
261-
method="GET", url=url, headers=expected_headers, data=None, timeout=None
275+
method="GET",
276+
url=url,
277+
headers=expected_headers,
278+
data=None,
279+
timeout=self._get_default_timeout(),
262280
)
263281

264282
def test__make_request_w_timeout(self):
@@ -309,7 +327,7 @@ def test_api_request_defaults(self):
309327
url=expected_url,
310328
headers=expected_headers,
311329
data=None,
312-
timeout=None,
330+
timeout=self._get_default_timeout(),
313331
)
314332

315333
def test_api_request_w_non_json_response(self):
@@ -352,7 +370,7 @@ def test_api_request_w_query_params(self):
352370
url=mock.ANY,
353371
headers=expected_headers,
354372
data=None,
355-
timeout=None,
373+
timeout=self._get_default_timeout(),
356374
)
357375

358376
url = http.request.call_args[1]["url"]
@@ -386,7 +404,7 @@ def test_api_request_w_headers(self):
386404
url=mock.ANY,
387405
headers=expected_headers,
388406
data=None,
389-
timeout=None,
407+
timeout=self._get_default_timeout(),
390408
)
391409

392410
def test_api_request_w_extra_headers(self):
@@ -416,7 +434,7 @@ def test_api_request_w_extra_headers(self):
416434
url=mock.ANY,
417435
headers=expected_headers,
418436
data=None,
419-
timeout=None,
437+
timeout=self._get_default_timeout(),
420438
)
421439

422440
def test_api_request_w_data(self):
@@ -443,7 +461,7 @@ def test_api_request_w_data(self):
443461
url=mock.ANY,
444462
headers=expected_headers,
445463
data=expected_data,
446-
timeout=None,
464+
timeout=self._get_default_timeout(),
447465
)
448466

449467
def test_api_request_w_timeout(self):

0 commit comments

Comments
 (0)