This repository was archived by the owner on Mar 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
feat: add support for reading google.api.api_version #1999
Merged
Merged
Changes from 30 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
4e3138c
feat: add support for reading google.api.api_version
parthea 009fc71
add showcase tests; clean up code
parthea b574ed1
remove noise in diff
parthea dd96aa4
remove noise in diff
parthea de4ba22
remove noise in diff
parthea 28e6991
use gapic-showcase 0.35.0
parthea fca5b55
update comment
parthea 8cd648a
add constraints to ads templates
parthea 35755b7
add new line
parthea ea5666f
update google-api-core and googleapis-common-protos setup.py to match…
parthea 79f1767
update grpc in setup.py to match the one in googleads/google-ads-python
parthea 7eb5c92
style
parthea c0b7810
coverage
parthea d52e78e
fix test
parthea 5129f61
mypy
parthea ab18eea
coverage
parthea 6946304
revert
parthea 6dfa53c
remove test code
parthea 2e666ea
update copyright year
parthea 1e6e96e
address review feedback
parthea 6416902
fix comment
parthea 68b4010
revert constraints files for ads
parthea 178c964
remove test code
parthea a5d6109
address review feedback
parthea 109edd4
address review feedback
parthea 12ff101
revert WORKSPACE
parthea ca95a6b
clean up
parthea 0e669ab
Update gapic/templates/%namespace/%name_%version/%sub/services/%servi…
parthea 7957ded
convert to symlink
parthea 542e4e1
Revert "convert to symlink"
parthea 0f68169
Update gapic/templates/%namespace/%name_%version/%sub/services/%servi…
parthea 921f20b
address review feedback
parthea 16402e1
Revert "Update gapic/templates/%namespace/%name_%version/%sub/service…
parthea 555cff7
fix build
parthea f150e67
Address review feedback
parthea 220f873
Address review feedback
parthea a62b313
address review feedback
parthea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
69 changes: 69 additions & 0 deletions
69
gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/_shared_macros.j2
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| {# | ||
| # Copyright (C) 2024 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| #} | ||
|
|
||
| {% macro auto_populate_uuid4_fields(api, method) %} | ||
| {# | ||
| Automatically populate UUID4 fields according to | ||
| https://google.aip.dev/client-libraries/4235 when the | ||
| field satisfies either of: | ||
| - The field supports explicit presence and has not been set by the user. | ||
| - The field doesn't support explicit presence, and its value is the empty | ||
| string (i.e. the default value). | ||
| When using this macro, ensure the calling template generates a line `import uuid` | ||
| #} | ||
| {% with method_settings = api.all_method_settings.get(method.meta.address.proto) %} | ||
| {% if method_settings is not none %} | ||
| {% for auto_populated_field in method_settings.auto_populated_fields %} | ||
| {% if method.input.fields[auto_populated_field].proto3_optional %} | ||
| if '{{ auto_populated_field }}' not in request: | ||
| {% else %} | ||
| if not request.{{ auto_populated_field }}: | ||
| {% endif %} | ||
| request.{{ auto_populated_field }} = str(uuid.uuid4()) | ||
| {% endfor %} | ||
| {% endif %}{# if method_settings is not none #} | ||
| {% endwith %}{# method_settings #} | ||
| {% endmacro %} | ||
|
|
||
| {% macro add_google_api_core_version_header_import(service) %} | ||
| {# | ||
| The `version_header` module was added to `google-api-core` | ||
| in version 2.19.0. | ||
| https://github.com/googleapis/python-api-core/releases/tag/v2.19.0 | ||
| The `try/except` below can be removed once the minimum version of | ||
| `google-api-core` is 2.19.0 or newer. | ||
| #} | ||
| {% if service.version %} | ||
| try: | ||
| from google.api_core import version_header | ||
| HAS_GOOGLE_API_CORE_VERSION_HEADER = True # pragma: NO COVER | ||
| except ImportError: # pragma: NO COVER | ||
| HAS_GOOGLE_API_CORE_VERSION_HEADER = False | ||
| {% endif %}{# service.version #} | ||
| {% endmacro %} | ||
| {% macro add_api_version_header_to_metadata(service) %} | ||
| {# | ||
| Add API Version to metadata as per https://github.com/aip-dev/google.aip.dev/pull/1331. | ||
| When using this macro, ensure the calling template also calls macro | ||
| `add_google_api_core_version_header_import` to add the necessary import statements. | ||
| #} | ||
| {% if service.version %} | ||
| if HAS_GOOGLE_API_CORE_VERSION_HEADER: # pragma: NO COVER | ||
| metadata = tuple(metadata) + ( | ||
| version_header.to_api_version_header("{{ service.version }}"), | ||
| ) | ||
| {% endif %}{# service.version #} | ||
| {% endmacro %} | ||
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
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
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
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
70 changes: 70 additions & 0 deletions
70
gapic/templates/%namespace/%name_%version/%sub/services/%service/_shared_macros.j2
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| {# | ||
| # Copyright (C) 2024 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| #} | ||
|
|
||
| {% macro auto_populate_uuid4_fields(api, method) %} | ||
| {# | ||
| Automatically populate UUID4 fields according to | ||
| https://google.aip.dev/client-libraries/4235 when the | ||
| field satisfies either of: | ||
| - The field supports explicit presence and has not been set by the user. | ||
| - The field doesn't support explicit presence, and its value is the empty | ||
| string (i.e. the default value). | ||
| When using this macro, ensure the calling template generates a line `import uuid` | ||
| #} | ||
| {% with method_settings = api.all_method_settings.get(method.meta.address.proto) %} | ||
| {% if method_settings is not none %} | ||
| {% for auto_populated_field in method_settings.auto_populated_fields %} | ||
| {% if method.input.fields[auto_populated_field].proto3_optional %} | ||
| if '{{ auto_populated_field }}' not in request: | ||
| {% else %} | ||
| if not request.{{ auto_populated_field }}: | ||
| {% endif %} | ||
| request.{{ auto_populated_field }} = str(uuid.uuid4()) | ||
| {% endfor %} | ||
| {% endif %}{# if method_settings is not none #} | ||
| {% endwith %}{# method_settings #} | ||
| {% endmacro %} | ||
ohmayr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| {% macro add_google_api_core_version_header_import(service) %} | ||
| {# | ||
| The `version_header` module was added to `google-api-core` | ||
| in version 2.19.0. | ||
| https://github.com/googleapis/python-api-core/releases/tag/v2.19.0 | ||
| The `try/except` below can be removed once the minimum version of | ||
| `google-api-core` is 2.19.0 or newer. | ||
| #} | ||
| {% if service.version %} | ||
| try: | ||
| from google.api_core import version_header | ||
| HAS_GOOGLE_API_CORE_VERSION_HEADER = True # pragma: NO COVER | ||
| except ImportError: # pragma: NO COVER | ||
| HAS_GOOGLE_API_CORE_VERSION_HEADER = False | ||
| {% endif %}{# service.version #} | ||
| {% endmacro %} | ||
|
|
||
| {% macro add_api_version_header_to_metadata(service) %} | ||
| {# | ||
| Add API Version to metadata as per https://github.com/aip-dev/google.aip.dev/pull/1331. | ||
| When using this macro, ensure the calling template also calls macro | ||
| `add_google_api_core_version_header_import` to add the necessary import statements. | ||
| #} | ||
| {% if service.version %} | ||
| if HAS_GOOGLE_API_CORE_VERSION_HEADER: # pragma: NO COVER | ||
| metadata = tuple(metadata) + ( | ||
| version_header.to_api_version_header("{{ service.version }}"), | ||
ohmayr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
| {% endif %}{# service.version #} | ||
| {% endmacro %} | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| {% block content %} | ||
| {% import "%namespace/%name_%version/%sub/services/%service/_client_macros.j2" as macros %} | ||
| {% import "%namespace/%name_%version/%sub/services/%service/_shared_macros.j2" as shared_macros %} | ||
vchudnov-g marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| from collections import OrderedDict | ||
| import functools | ||
|
|
@@ -24,6 +25,7 @@ from google.api_core import retry_async as retries | |
| from google.auth import credentials as ga_credentials # type: ignore | ||
| from google.oauth2 import service_account # type: ignore | ||
|
|
||
| {{ shared_macros.add_google_api_core_version_header_import(service) }} | ||
|
||
| try: | ||
| OptionalRetry = Union[retries.AsyncRetry, gapic_v1.method._MethodDefault, None] | ||
| except AttributeError: # pragma: NO COVER | ||
|
|
@@ -54,7 +56,8 @@ from .client import {{ service.client_name }} | |
|
|
||
| {# TODO(yon-mg): handle rest transport async client interaction #} | ||
| class {{ service.async_client_name }}: | ||
| """{{ service.meta.doc|rst(width=72, indent=4) }}""" | ||
| """{{ service.meta.doc|rst(width=72, indent=4) }}{% if service.version|length %} | ||
| This class implements API version {{ service.version }}.{% endif %}""" | ||
|
|
||
| _client: {{ service.client_name }} | ||
|
|
||
|
|
@@ -388,7 +391,8 @@ class {{ service.async_client_name }}: | |
| ) | ||
| {% endif %} | ||
|
|
||
| {{ macros.auto_populate_uuid4_fields(api, method) }} | ||
| {{ shared_macros.add_api_version_header_to_metadata(service) }} | ||
| {{ shared_macros.auto_populate_uuid4_fields(api, method) }} | ||
|
|
||
| # Validate the universe domain. | ||
| self._client._validate_universe_domain() | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a regular file, not a symlink:
See the meaning of Git modes. (Credit: SO answer)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch and thanks for the link! Fixed in 7957ded
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reverted this change in 542e4e1 and filed https://github.com/googleapis/gapic-generator-python/issues/2028 to investigate if symlinks can be used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the symlinks don't work (weird, they should), can you specify a template outside the current directory (something like
{% include ../templates/foo/bar %}If that also doesn't work, I would suggest keeping the duplicate files but adding a prominent header to the ads one saying that this is a copy of the one in the standard templates, intended to be a symlink. My rationale: we have duplicate code either way, and the macro file9s) makes them more organized....and it paves the way for the symlink once we can figure it out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see. You reverted back to the duplicate file, like I said in that last paragraph. SG. Could you add a note that this is intended to be a symlink to the other file, and for now is a duplicate?