feat: support visibility labels in discovery.build() and requests#2760
Open
Capstan wants to merge 1 commit into
Open
feat: support visibility labels in discovery.build() and requests#2760Capstan wants to merge 1 commit into
Capstan wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
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.
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.
0487e9e to
8200f90
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 usingdiscovery.build(), aligning the Python client with visibility configurations defined ingoogle/api/visibility.proto(AIP-185).How It Works
labelsparameter (a list of strings) todiscovery.build()andbuild_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).labelsare provided,static_discoverydefaults toFalse(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.labelslist is stored on the clientResourceinstance (self._labels). On subsequent API requests dynamically generated by the resource, theX-Goog-Visibilitiesheader 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:DISCOVERY_URIandV2_DISCOVERY_URItemplates to support query parameter list expansions:{?labels*}and{&labels*}.labelsparameter tobuild()andbuild_from_document().labelson theResourceinstance.X-Goog-Visibilitiesheader increateMethod().tests/test_discovery.py:test_discovery_with_labelsunit test to verify both the dynamic discovery URL query string parameters and the automatic injection ofX-Goog-Visibilitiesheaders on subsequent API requests.test_discovery_with_labels_and_cache_missandtest_discovery_with_labels_and_cache_hitunit tests to validate that discovery caching behaves correctly and uses the expanded discovery URL (including the labels query parameters) as the cache key.test_discovery_with_labels_v2_fallbackunit test to validate V2 dynamic discovery URL expansion and fallback.test_pickleobject keys to account for the new_labelsinstance 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