Skip to content
Open
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
9 changes: 6 additions & 3 deletions googleapiclient/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -1502,9 +1502,12 @@ def _set_service_methods(self):
def _add_basic_methods(self, resourceDesc, rootDesc, schema):
# If this is the root Resource, add a new_batch_http_request() method.
if resourceDesc == rootDesc:
batch_uri = "%s%s" % (
rootDesc["rootUrl"],
rootDesc.get("batchPath", "batch"),
# batchPath is relative to rootUrl, but the host must come from the
# base URL so the batch follows the universe domain, api_endpoint
# and mTLS endpoint the same way every other request does.
batch_uri = _fix_up_media_path_base_url(
"%s%s" % (rootDesc["rootUrl"], rootDesc.get("batchPath", "batch")),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

In accordance with the general rules, f-strings are preferred over % formatting for string interpolation in Python.

Suggested change
"%s%s" % (rootDesc["rootUrl"], rootDesc.get("batchPath", "batch")),
f"{rootDesc['rootUrl']}{rootDesc.get('batchPath', 'batch')}",
References
  1. Prefer using f-strings for string interpolation in Python.

self._baseUrl,
)

def new_batch_http_request(callback=None):
Expand Down
54 changes: 54 additions & 0 deletions tests/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,20 @@ def test_api_endpoint_override_from_client_options_dict(self):

self.assertEqual(plus._baseUrl, api_endpoint)

def test_batch_request_uses_api_endpoint_override(self):
discovery = read_datafile("zoo.json")
api_endpoint = "https://foo.googleapis.com/zoo/v1/"
zoo = build_from_document(
discovery,
client_options={"api_endpoint": api_endpoint},
credentials=self.MOCK_CREDENTIALS,
)
batch_request = zoo.new_batch_http_request()

self.assertEqual(
batch_request._batch_uri, "https://foo.googleapis.com/batchZoo"
)

def test_scopes_from_client_options(self):
discovery = read_datafile("plus.json")

Expand Down Expand Up @@ -983,6 +997,29 @@ def test_mtls_with_provided_client_cert_unset_environment_variable(
self.assertIsNotNone(plus)
self.assertEqual(plus._baseUrl, base_url)

def test_batch_request_uses_mtls_endpoint(self):
discovery = read_datafile("zoo.json")

with mock.patch.dict("os.environ", {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}):
with mock.patch.dict(
"os.environ", {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}
):
zoo = build_from_document(
discovery,
credentials=self.MOCK_CREDENTIALS,
client_options={
"client_encrypted_cert_source": self.client_encrypted_cert_source
},
)
batch_request = zoo.new_batch_http_request()

self.assertEqual(
zoo._baseUrl, "https://www.mtls.googleapis.com/zoo/v1/"
)
self.assertEqual(
batch_request._batch_uri, "https://www.mtls.googleapis.com/batchZoo"
)

@parameterized.expand(
[
("never", "true"),
Expand Down Expand Up @@ -2852,6 +2889,23 @@ def test_universe_env_var_configured_with_client_options_universe(self):

assert tasks._universe_domain == fake_universe

def test_batch_request_uses_universe_domain(self):
fake_universe = "foo.com"
credentials = mock.Mock(universe_domain=fake_universe)
discovery = read_datafile("zoo.json")

zoo = build_from_document(
discovery,
credentials=credentials,
client_options=google.api_core.client_options.ClientOptions(
universe_domain=fake_universe
),
)
batch_request = zoo.new_batch_http_request()

assert zoo._baseUrl == "https://www.foo.com/zoo/v1/"
assert batch_request._batch_uri == "https://www.foo.com/batchZoo"

else:
if hasattr(google.api_core.client_options.ClientOptions, "universe_domain"):

Expand Down
Loading