From f21165263d3f8cae4c4c50d20d99ddf9a1bc3c6d Mon Sep 17 00:00:00 2001 From: farkhalit rida Date: Fri, 30 Oct 2026 17:00:23 +0530 Subject: [PATCH] fix: honor api_endpoint, universe and mTLS in new_batch_http_request --- googleapiclient/discovery.py | 9 ++++-- tests/test_discovery.py | 54 ++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/googleapiclient/discovery.py b/googleapiclient/discovery.py index 4e92b2cc1b..c8e867a600 100644 --- a/googleapiclient/discovery.py +++ b/googleapiclient/discovery.py @@ -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")), + self._baseUrl, ) def new_batch_http_request(callback=None): diff --git a/tests/test_discovery.py b/tests/test_discovery.py index 6912783451..6e9ba2c3bb 100644 --- a/tests/test_discovery.py +++ b/tests/test_discovery.py @@ -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") @@ -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"), @@ -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"):