From d7f52da7146d0149fea8ef7c5905f4f300dbd2bb Mon Sep 17 00:00:00 2001 From: Cris Bettis Date: Thu, 9 Feb 2017 20:14:39 -0500 Subject: [PATCH 1/4] Allow a requests.Session object to be passed into Client This will allow the client to re-use an existing session and not have re-establish the connection over subsequent calls. --- googlemaps/client.py | 11 +++++++++-- test/test_client.py | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/googlemaps/client.py b/googlemaps/client.py index f1cd1296..2874f649 100644 --- a/googlemaps/client.py +++ b/googlemaps/client.py @@ -47,11 +47,12 @@ class Client(object): """Performs requests to the Google Maps API web services.""" + session = requests.Session() def __init__(self, key=None, client_id=None, client_secret=None, timeout=None, connect_timeout=None, read_timeout=None, retry_timeout=60, requests_kwargs=None, - queries_per_second=10, channel=None): + queries_per_second=10, channel=None, requests_session=None): """ :param key: Maps API key. Required, unless "client_id" and "client_secret" are set. @@ -104,6 +105,9 @@ def __init__(self, key=None, client_id=None, client_secret=None, http://docs.python-requests.org/en/latest/api/#main-interface :type requests_kwargs: dict + :param requests_session: Re-usable requests.session object for re-using connections + :type requests_session: request.Session + """ if not key and not (client_secret and client_id): raise ValueError("Must provide API key or enterprise credentials " @@ -112,6 +116,9 @@ def __init__(self, key=None, client_id=None, client_secret=None, if key and not key.startswith("AIza"): raise ValueError("Invalid API key provided.") + if requests_session is not None: + self.session = requests_session + if channel: if not client_id: raise ValueError("The channel argument must be used with a " @@ -216,7 +223,7 @@ def _get(self, url, params, first_request_time=None, retry_counter=0, # requests_kwargs arg overriding. requests_kwargs = dict(self.requests_kwargs, **(requests_kwargs or {})) try: - resp = requests.get(base_url + authed_url, **requests_kwargs) + resp = self.session.get(base_url + authed_url, **requests_kwargs) except requests.exceptions.Timeout: raise googlemaps.exceptions.Timeout() except Exception as e: diff --git a/test/test_client.py b/test/test_client.py index f2d424a4..df6a70d5 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -43,6 +43,20 @@ def test_urlencode(self): encoded_params = _client.urlencode_params([("address", "=Sydney ~")]) self.assertEqual("address=%3DSydney+~", encoded_params) + @responses.activate + def test_query_with_session(self): + session = requests.Session() + responses.add(responses.GET, + "https://maps.googleapis.com/maps/api/geocode/json", + body='{"status":"OK","results":[]}', + status=200, + content_type="application/json") + client = googlemaps.Client(key="AIzaasdf", + queries_per_second=3, + requests_session=session) + client.geocode("Sesame St.") + + @responses.activate def test_queries_per_second(self): # This test assumes that the time to run a mocked query is From 573cc880c9e74d072312b61584ae473d7dfd5e0b Mon Sep 17 00:00:00 2001 From: Cris Bettis Date: Thu, 9 Feb 2017 22:05:52 -0500 Subject: [PATCH 2/4] fixup! Allow a requests.Session object to be passed into Client --- googlemaps/client.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/googlemaps/client.py b/googlemaps/client.py index 2874f649..0d1f17d8 100644 --- a/googlemaps/client.py +++ b/googlemaps/client.py @@ -47,7 +47,6 @@ class Client(object): """Performs requests to the Google Maps API web services.""" - session = requests.Session() def __init__(self, key=None, client_id=None, client_secret=None, timeout=None, connect_timeout=None, read_timeout=None, @@ -116,7 +115,9 @@ def __init__(self, key=None, client_id=None, client_secret=None, if key and not key.startswith("AIza"): raise ValueError("Invalid API key provided.") - if requests_session is not None: + if requests_session is None: + self.session = requests.Session() + else: self.session = requests_session if channel: From c778419c779c398a3347fe49d8f4199fd8901b5e Mon Sep 17 00:00:00 2001 From: Cris Bettis Date: Thu, 9 Feb 2017 22:11:04 -0500 Subject: [PATCH 3/4] fixup! Allow a requests.Session object to be passed into Client --- googlemaps/client.py | 11 ++--------- test/test_client.py | 4 ++-- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/googlemaps/client.py b/googlemaps/client.py index 0d1f17d8..26bd17b2 100644 --- a/googlemaps/client.py +++ b/googlemaps/client.py @@ -51,7 +51,7 @@ class Client(object): def __init__(self, key=None, client_id=None, client_secret=None, timeout=None, connect_timeout=None, read_timeout=None, retry_timeout=60, requests_kwargs=None, - queries_per_second=10, channel=None, requests_session=None): + queries_per_second=10, channel=None): """ :param key: Maps API key. Required, unless "client_id" and "client_secret" are set. @@ -104,9 +104,6 @@ def __init__(self, key=None, client_id=None, client_secret=None, http://docs.python-requests.org/en/latest/api/#main-interface :type requests_kwargs: dict - :param requests_session: Re-usable requests.session object for re-using connections - :type requests_session: request.Session - """ if not key and not (client_secret and client_id): raise ValueError("Must provide API key or enterprise credentials " @@ -115,11 +112,6 @@ def __init__(self, key=None, client_id=None, client_secret=None, if key and not key.startswith("AIza"): raise ValueError("Invalid API key provided.") - if requests_session is None: - self.session = requests.Session() - else: - self.session = requests_session - if channel: if not client_id: raise ValueError("The channel argument must be used with a " @@ -129,6 +121,7 @@ def __init__(self, key=None, client_id=None, client_secret=None, "alphanumeric string. The period (.), underscore (_)" "and hyphen (-) characters are allowed.") + self.session = requests.Session() self.key = key if timeout and (connect_timeout or read_timeout): diff --git a/test/test_client.py b/test/test_client.py index df6a70d5..41b90b26 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -52,8 +52,8 @@ def test_query_with_session(self): status=200, content_type="application/json") client = googlemaps.Client(key="AIzaasdf", - queries_per_second=3, - requests_session=session) + queries_per_second=3) + client.session = session client.geocode("Sesame St.") From 25940c4c165f7cdd1ebabdc0c4337742f6354298 Mon Sep 17 00:00:00 2001 From: Cris Bettis Date: Fri, 10 Feb 2017 07:53:49 -0500 Subject: [PATCH 4/4] fixup! Allow a requests.Session object to be passed into Client --- test/test_client.py | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/test/test_client.py b/test/test_client.py index 41b90b26..f2d424a4 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -43,20 +43,6 @@ def test_urlencode(self): encoded_params = _client.urlencode_params([("address", "=Sydney ~")]) self.assertEqual("address=%3DSydney+~", encoded_params) - @responses.activate - def test_query_with_session(self): - session = requests.Session() - responses.add(responses.GET, - "https://maps.googleapis.com/maps/api/geocode/json", - body='{"status":"OK","results":[]}', - status=200, - content_type="application/json") - client = googlemaps.Client(key="AIzaasdf", - queries_per_second=3) - client.session = session - client.geocode("Sesame St.") - - @responses.activate def test_queries_per_second(self): # This test assumes that the time to run a mocked query is