feat: support visibility labels in discovery.build() and requests#2760
feat: support visibility labels in discovery.build() and requests#2760Capstan 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.
0487e9e to
8200f90
Compare
|
In this PR, static discovery is configured to default to However, I have implemented a follow-up feature that extends static discovery to support loading pre-packaged labeled discovery documents (using a dash I can either:
Please let me know your preference! |
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.
8200f90 to
2657d19
Compare
|
If you are google-internal, I have a standalone zoo repro case that models all the visibility behaviors for discovery and API use you can run locally; LMK if you'd like a pointer. I did some additional integration testing (beyond the unit testing) against this server. |
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