18
18
19
19
# use urlfetch as request_lib on google app engine, otherwise use requests
20
20
request_lib = None
21
+ # use a max timeout equal to that of all customer-facing backend operations
22
+ _max_timeout = 90
21
23
try :
22
24
from google .appengine .api import urlfetch
23
25
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 )
24
28
except ImportError :
25
29
try :
26
30
import requests
47
51
# config
48
52
api_key = None
49
53
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 )
50
56
51
57
52
58
USER_AGENT = 'EasyPost/v2 PythonClient/{0}' .format (VERSION )
@@ -293,6 +299,9 @@ def request_raw(self, method, url, params=None, apiKeyRequired=True):
293
299
'Content-type' : 'application/x-www-form-urlencoded'
294
300
}
295
301
302
+ if timeout > _max_timeout :
303
+ raise Error ("`timeout` must not exceed %d; it is %d" % (_max_timeout , timeout ))
304
+
296
305
if request_lib == 'urlfetch' :
297
306
http_body , http_status = self .urlfetch_request (method , abs_url , headers , params )
298
307
elif request_lib == 'requests' :
@@ -330,7 +339,7 @@ def requests_request(self, method, abs_url, headers, params):
330
339
abs_url ,
331
340
headers = headers ,
332
341
data = data ,
333
- timeout = 60 ,
342
+ timeout = timeout ,
334
343
verify = True ,
335
344
)
336
345
http_body = result .text
@@ -354,7 +363,7 @@ def urlfetch_request(self, method, abs_url, headers, params):
354
363
args ['method' ] = method
355
364
args ['headers' ] = headers
356
365
args ['validate_certificate' ] = False
357
- args ['deadline' ] = 55 # GAE times out after 60
366
+ args ['deadline' ] = timeout
358
367
359
368
try :
360
369
result = urlfetch .fetch (** args )
0 commit comments