Skip to content

Commit cce2203

Browse files
committed
Merge branch 'jamesbeith-custom-timeout'
Closes #57
2 parents 4f91ae4 + e3d3f7c commit cce2203

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

easypost/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818

1919
# use urlfetch as request_lib on google app engine, otherwise use requests
2020
request_lib = None
21+
# use a max timeout equal to that of all customer-facing backend operations
22+
_max_timeout = 90
2123
try:
2224
from google.appengine.api import urlfetch
2325
request_lib = 'urlfetch'
26+
# use the GAE application-wide "deadline" (or its default) if it's less than our existing max timeout
27+
_max_timeout = min(urlfetch.get_default_fetch_deadline() or 60, _max_timeout)
2428
except ImportError:
2529
try:
2630
import requests
@@ -47,6 +51,8 @@
4751
# config
4852
api_key = None
4953
api_base = 'https://api.easypost.com/v2'
54+
# use our default timeout, or our max timeout if that is less
55+
timeout = min(60, _max_timeout)
5056

5157

5258
USER_AGENT = 'EasyPost/v2 PythonClient/{0}'.format(VERSION)
@@ -293,6 +299,9 @@ def request_raw(self, method, url, params=None, apiKeyRequired=True):
293299
'Content-type': 'application/x-www-form-urlencoded'
294300
}
295301

302+
if timeout > _max_timeout:
303+
raise Error("`timeout` must not exceed %d; it is %d" % (_max_timeout, timeout))
304+
296305
if request_lib == 'urlfetch':
297306
http_body, http_status = self.urlfetch_request(method, abs_url, headers, params)
298307
elif request_lib == 'requests':
@@ -330,7 +339,7 @@ def requests_request(self, method, abs_url, headers, params):
330339
abs_url,
331340
headers=headers,
332341
data=data,
333-
timeout=60,
342+
timeout=timeout,
334343
verify=True,
335344
)
336345
http_body = result.text
@@ -354,7 +363,7 @@ def urlfetch_request(self, method, abs_url, headers, params):
354363
args['method'] = method
355364
args['headers'] = headers
356365
args['validate_certificate'] = False
357-
args['deadline'] = 55 # GAE times out after 60
366+
args['deadline'] = timeout
358367

359368
try:
360369
result = urlfetch.fetch(**args)

0 commit comments

Comments
 (0)