Skip to content

Commit c875f35

Browse files
author
thatguysimon
authored
feat: Allow overriding base_url on Client object initialization (googlemaps#364)
1 parent cc13049 commit c875f35

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

‎googlemaps/client.py‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ def __init__(self, key=None, client_id=None, client_secret=None,
5353
timeout=None, connect_timeout=None, read_timeout=None,
5454
retry_timeout=60, requests_kwargs=None,
5555
queries_per_second=50, channel=None,
56-
retry_over_query_limit=True, experience_id=None):
56+
retry_over_query_limit=True, experience_id=None,
57+
base_url=_DEFAULT_BASE_URL):
5758
"""
5859
:param key: Maps API key. Required, unless "client_id" and
5960
"client_secret" are set. Most users should use an API key.
@@ -115,6 +116,10 @@ def __init__(self, key=None, client_id=None, client_secret=None,
115116
implemented. See the official requests docs for more info:
116117
http://docs.python-requests.org/en/latest/api/#main-interface
117118
:type requests_kwargs: dict
119+
120+
:param base_url: The base URL for all requests. Defaults to the Maps API
121+
server. Should not have a trailing slash.
122+
:type base_url: string
118123
119124
"""
120125
if not key and not (client_secret and client_id):
@@ -167,6 +172,7 @@ def __init__(self, key=None, client_id=None, client_secret=None,
167172
self.retry_over_query_limit = retry_over_query_limit
168173
self.sent_times = collections.deque("", queries_per_second)
169174
self.set_experience_id(experience_id)
175+
self.base_url = base_url
170176

171177
def set_experience_id(self, *experience_id_args):
172178
"""Sets the value for the HTTP header field name
@@ -204,7 +210,7 @@ def clear_experience_id(self):
204210
self.requests_kwargs["headers"] = headers
205211

206212
def _request(self, url, params, first_request_time=None, retry_counter=0,
207-
base_url=_DEFAULT_BASE_URL, accepts_clientid=True,
213+
base_url=None, accepts_clientid=True,
208214
extract_body=None, requests_kwargs=None, post_json=None):
209215
"""Performs HTTP GET/POST with credentials, returning the body as
210216
JSON.
@@ -246,6 +252,9 @@ def _request(self, url, params, first_request_time=None, retry_counter=0,
246252
exceute a request.
247253
"""
248254

255+
if base_url is None:
256+
base_url = self.base_url
257+
249258
if not first_request_time:
250259
first_request_time = datetime.now()
251260

‎tests/test_client.py‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,22 @@ def test_transport_error(self):
209209
self.assertEqual(e.exception.status_code, 404)
210210

211211
@responses.activate
212-
def test_host_override(self):
212+
def test_host_override_on_init(self):
213+
responses.add(
214+
responses.GET,
215+
"https://foo.com/bar",
216+
body='{"status":"OK","results":[]}',
217+
status=200,
218+
content_type="application/json",
219+
)
220+
221+
client = googlemaps.Client(key="AIzaasdf", base_url="https://foo.com")
222+
client._get("/bar", {})
223+
224+
self.assertEqual(1, len(responses.calls))
225+
226+
@responses.activate
227+
def test_host_override_per_request(self):
213228
responses.add(
214229
responses.GET,
215230
"https://foo.com/bar",

0 commit comments

Comments
 (0)