这是indexloc提供的服务,不要输入任何密码
Skip to content

Allow overriding base_url on Client object initialization #364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions googlemaps/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ 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=50, channel=None,
retry_over_query_limit=True, experience_id=None):
retry_over_query_limit=True, experience_id=None,
base_url=_DEFAULT_BASE_URL):
"""
:param key: Maps API key. Required, unless "client_id" and
"client_secret" are set. Most users should use an API key.
Expand Down Expand Up @@ -115,6 +116,10 @@ def __init__(self, key=None, client_id=None, client_secret=None,
implemented. See the official requests docs for more info:
http://docs.python-requests.org/en/latest/api/#main-interface
:type requests_kwargs: dict

:param base_url: The base URL for all requests. Defaults to the Maps API
server. Should not have a trailing slash.
:type base_url: string

"""
if not key and not (client_secret and client_id):
Expand Down Expand Up @@ -167,6 +172,7 @@ def __init__(self, key=None, client_id=None, client_secret=None,
self.retry_over_query_limit = retry_over_query_limit
self.sent_times = collections.deque("", queries_per_second)
self.set_experience_id(experience_id)
self.base_url = base_url

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

def _request(self, url, params, first_request_time=None, retry_counter=0,
base_url=_DEFAULT_BASE_URL, accepts_clientid=True,
base_url=None, accepts_clientid=True,
extract_body=None, requests_kwargs=None, post_json=None):
"""Performs HTTP GET/POST with credentials, returning the body as
JSON.
Expand Down Expand Up @@ -246,6 +252,9 @@ def _request(self, url, params, first_request_time=None, retry_counter=0,
exceute a request.
"""

if base_url is None:
base_url = self.base_url

if not first_request_time:
first_request_time = datetime.now()

Expand Down
17 changes: 16 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,22 @@ def test_transport_error(self):
self.assertEqual(e.exception.status_code, 404)

@responses.activate
def test_host_override(self):
def test_host_override_on_init(self):
responses.add(
responses.GET,
"https://foo.com/bar",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)

client = googlemaps.Client(key="AIzaasdf", base_url="https://foo.com")
client._get("/bar", {})

self.assertEqual(1, len(responses.calls))

@responses.activate
def test_host_override_per_request(self):
responses.add(
responses.GET,
"https://foo.com/bar",
Expand Down