@@ -20,15 +20,26 @@ class Context(object):
2020 settings"""
2121
2222 def __init__ (self , key = None , client_id = None , client_secret = None ,
23- timeout = None , retry_timeout = 60 ):
23+ timeout = None , connect_timeout = None , read_timeout = None ,
24+ retry_timeout = 60 ):
2425 """
2526 :param key: Maps API key. Required, unless "client_id" and
2627 "client_secret" are set.
2728 :type key: basestring
2829
29- :param timeout: Timeout for requests, in seconds.
30+ :param timeout: Combined connect and read timeout for HTTP requests, in
31+ seconds. Specify "None" for no timeout.
3032 :type timeout: int
3133
34+ :param connect_timeout: Connection timeout for HTTP requests, in
35+ seconds. You should specify read_timeout in addition to this option.
36+ :type connect_timeout: int
37+
38+ :param read_timeout: Read timeout for HTTP requests, in
39+ seconds. You should specify connect_timeout in addition to this
40+ option.
41+ :type read_timeout: int
42+
3243 :param retry_timeout: Timeout across multiple retriable requests, in seconds.
3344 :type retry_timeout: int
3445
@@ -47,7 +58,11 @@ def __init__(self, key=None, client_id=None, client_secret=None,
4758 raise Exception ("Invalid API key provided." )
4859
4960 self .key = key
50- self .timeout = timeout
61+
62+ if timeout and (connect_timeout or read_timeout ):
63+ raise ValueError ("Specify either timeout, or connect_timeout and read_timeout" )
64+
65+ self .timeout = timeout or (connect_timeout , read_timeout )
5166 self .client_id = client_id
5267 self .client_secret = client_secret
5368 self .retry_timeout = timedelta (seconds = retry_timeout )
@@ -104,12 +119,10 @@ def _get(ctx, url, params, first_request_time=None):
104119 if datetime .now () - first_request_time > ctx .retry_timeout :
105120 raise Exception ("Timed out while retrying." )
106121
107- # TODO(mdr-eng): implement rate limiting, etc.
108- # TODO(mdr-eng): add jitter (might not be necessary since most uses will be
109- # single threaded)
110122 resp = requests .get (
111123 "https://maps.googleapis.com" + ctx ._auth_url (url , params ),
112124 headers = {"User-Agent" : _USER_AGENT },
125+ timeout = ctx .timeout ,
113126 verify = True ) # NOTE(cbro): verify SSL certs.
114127
115128 if resp .status_code in [500 , 503 , 504 ]:
0 commit comments