Skip to content

feat: support visibility labels in discovery.build() and requests#2760

Open
Capstan wants to merge 1 commit into
googleapis:mainfrom
Capstan:add-visibility-labels
Open

feat: support visibility labels in discovery.build() and requests#2760
Capstan wants to merge 1 commit into
googleapis:mainfrom
Capstan:add-visibility-labels

Conversation

@Capstan
Copy link
Copy Markdown

@Capstan Capstan commented Jun 4, 2026

PR: feat: support visibility labels in discovery.build() and requests

Note

Status: Unit tests passing. Live integration testing completed and verified against a private local mock gateway.

Description

This PR adds support for passing a list of visibility labels (e.g., PREVIEW, GOOGLE_INTERNAL) when building client API resources using discovery.build(), aligning the Python client with visibility configurations defined in google/api/visibility.proto (AIP-185).

How It Works

  1. Discovery Phase: Added a new labels parameter (a list of strings) to discovery.build() and build_from_document(). If provided, these labels are dynamically expanded as query parameters in the discovery document URL (using RFC 6570 list expansion, e.g. ?labels=PREVIEW&labels=GOOGLE_INTERNAL).
  2. Automatic Cache Fallback: When labels are provided, static_discovery defaults to False (unless explicitly requested otherwise) to ensure the client fetches the live discovery document containing the restricted private/preview definitions instead of relying on stale static files.
  3. API Request Phase: The labels list is stored on the client Resource instance (self._labels). On subsequent API requests dynamically generated by the resource, the X-Goog-Visibilities header is automatically injected containing the comma-separated list of labels (e.g., X-Goog-Visibilities: PREVIEW,GOOGLE_INTERNAL).

Important

Backend Limitation: While the Python client supports passing and serializing multiple labels (expanding to multiple query parameters in discovery and a comma-separated list in headers), Google's API servers may not currently support evaluating multiple visibility labels simultaneously on a single request.

Code Changes

  • googleapiclient/discovery.py:
    • Modified DISCOVERY_URI and V2_DISCOVERY_URI templates to support query parameter list expansions: {?labels*} and {&labels*}.
    • Added labels parameter to build() and build_from_document().
    • Stored labels on the Resource instance.
    • Injected the X-Goog-Visibilities header in createMethod().
  • tests/test_discovery.py:
    • Added test_discovery_with_labels unit test to verify both the dynamic discovery URL query string parameters and the automatic injection of X-Goog-Visibilities headers on subsequent API requests.
    • Added test_discovery_with_labels_and_cache_miss and test_discovery_with_labels_and_cache_hit unit tests to validate that discovery caching behaves correctly and uses the expanded discovery URL (including the labels query parameters) as the cache key.
    • Added test_discovery_with_labels_v2_fallback unit test to validate V2 dynamic discovery URL expansion and fallback.
    • Updated test_pickle object keys to account for the new _labels instance variable.

Verification

Ran the unit tests locally under pytest:

pytest tests/test_discovery.py::Discovery -k "test_discovery_with_labels"

Result: 7 passed, 173 deselected in 0.36s (Passed successfully).

Related Issues

@Capstan Capstan requested a review from a team as a code owner June 4, 2026 17:27
@product-auto-label product-auto-label Bot added the size: m Pull request size is medium. label Jun 4, 2026
Copy link
Copy Markdown

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

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 introduces support for visibility labels in the Google API Discovery service, allowing users to filter discovery documents and restrict request visibility. It updates discovery URI templates, adds a labels parameter to build() and build_from_document(), and injects the X-Goog-Visibilities header into API requests. The review feedback highlights a potential runtime TypeError if an iterable of non-string elements is passed to labels, suggesting stricter validation to ensure all elements are strings, along with an additional test case to verify this behavior.

Comment thread googleapiclient/discovery.py
Comment thread googleapiclient/discovery.py
Comment thread tests/test_discovery.py
Allow developers to provide a list of `labels` (visibilities) when building the API client using `discovery.build()`.
These labels are dynamically appended to the discovery document URL and are stored in the `Resource` object to be automatically sent as the `X-Goog-Visibilities` header on subsequent requests.

Fixes googleapis#935.
@Capstan Capstan force-pushed the add-visibility-labels branch from 0487e9e to 8200f90 Compare June 4, 2026 17:34
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.

Support visibility labels in discovery requests

1 participant