Skip to content

fix: honor api_endpoint, universe and mTLS in new_batch_http_request - #2849

Open
farkhalit wants to merge 1 commit into
googleapis:mainfrom
farkhalit:batch-uri-base-url
Open

farkhalit wants to merge 1 commit into
googleapis:mainfrom
farkhalit:batch-uri-base-url

Conversation

@farkhalit

Copy link
Copy Markdown

new_batch_http_request() builds its endpoint as rootUrl + batchPath straight from the discovery document, while every ordinary method request goes through self._baseUrl, which already carries the universe domain substitution, the client_options.api_endpoint override and the mTLS endpoint choice. A client configured for a non-default universe, a restricted or private api_endpoint, or GOOGLE_API_USE_MTLS_ENDPOINT=always therefore POSTs its batch, with each sub-request's bearer token inside the multipart body, to the public www.googleapis.com host. The universe check in _validate_credentials does not catch this because it compares the configured universes rather than the host the batch is actually sent to. The batch URI now takes its host from _baseUrl through the same _fix_up_media_path_base_url correction that media upload URLs already get, and the three new tests fail on main and pass with the change.

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

@farkhalit
farkhalit requested a review from a team as a code owner October 31, 2026 07:35
@product-auto-label product-auto-label Bot added the size: m Pull request size is medium. label Oct 31, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request ensures that batch request URIs correctly respect custom API endpoints, mTLS endpoints, and universe domains by using the base URL in googleapiclient/discovery.py. It also adds comprehensive unit tests to verify these scenarios. The feedback suggests improving code readability by using f-strings instead of % formatting for string interpolation.

# 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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: m Pull request size is medium.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant