Skip to content

Commit 4e3138c

Browse files
committed
feat: add support for reading google.api.api_version
1 parent e403acc commit 4e3138c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+147
-43
lines changed

gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ class {{ service.client_name }}Meta(type):
9494

9595

9696
class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
97-
"""{{ service.meta.doc|rst(width=72, indent=4) }}"""
97+
"""{{ service.meta.doc|rst(width=72, indent=4) }}{% if service.version|length %}
98+
This class implements API version {{ service.version }}."""
99+
{% else %}"""
100+
{% endif %}
98101

99102
@staticmethod
100103
def _get_default_mtls_endpoint(api_endpoint):
@@ -476,6 +479,11 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
476479
)
477480
{% endif %}
478481

482+
{% if service.version %}
483+
metadata = tuple(metadata) + (
484+
gapic_v1.version_header.to_grpc_metadata("{{ service.version }}"),
485+
)
486+
{% endif %}
479487
{#
480488
Automatically populate UUID4 fields according to
481489
https://google.aip.dev/client-libraries/4235 when the

gapic/ads-templates/setup.py.j2

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ else:
2929
release_status = "Development Status :: 5 - Production/Stable"
3030

3131
dependencies = [
32-
"google-api-core[grpc] >= 2.10.0, < 3.0.0dev",
32+
"google-api-core[grpc] @ git+https://github.com/googleapis/python-api-core.git@add-api-version-header",
33+
#"google-api-core[grpc] >= 2.10.0, < 3.0.0dev",
3334
"google-auth >= 2.14.1, <3.0.0dev",
3435
"googleapis-common-protos >= 1.53.0",
3536
"grpcio >= 1.10.0",

gapic/schema/wrappers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,6 +1734,17 @@ def host(self) -> str:
17341734
return self.options.Extensions[client_pb2.default_host]
17351735
return ''
17361736

1737+
@property
1738+
def version(self) -> str:
1739+
"""Return the API version for this service, if specified.
1740+
1741+
Returns:
1742+
str: The API version for this service.
1743+
"""
1744+
if self.options.Extensions[client_pb2.api_version]:
1745+
return self.options.Extensions[client_pb2.api_version]
1746+
return ''
1747+
17371748
@property
17381749
def shortname(self) -> str:
17391750
"""Return the API short name. DRIFT uses this to identify

gapic/templates/%namespace/%name_%version/%sub/services/%service/_client_macros.j2

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@
183183
)
184184
{% endif %} {# method.explicit_routing #}
185185

186+
{% if service.version %}
187+
metadata = tuple(metadata) + (
188+
gapic_v1.version_header.to_grpc_metadata("{{ service.version }}"),
189+
)
190+
{% endif %}
191+
186192
{{ auto_populate_uuid4_fields(api, method) }}
187193

188194
# Validate the universe domain.

gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ from .client import {{ service.client_name }}
5454

5555
{# TODO(yon-mg): handle rest transport async client interaction #}
5656
class {{ service.async_client_name }}:
57-
"""{{ service.meta.doc|rst(width=72, indent=4) }}"""
57+
"""{{ service.meta.doc|rst(width=72, indent=4) }}{% if service.version|length %}
58+
This class implements API version {{ service.version }}."""
59+
{% else %}"""
60+
{% endif %}
5861

5962
_client: {{ service.client_name }}
6063

@@ -388,6 +391,12 @@ class {{ service.async_client_name }}:
388391
)
389392
{% endif %}
390393

394+
{% if service.version %}
395+
metadata = tuple(metadata) + (
396+
gapic_v1.version_header.to_grpc_metadata("{{ service.version }}")
397+
)
398+
{% endif %}
399+
391400
{{ macros.auto_populate_uuid4_fields(api, method) }}
392401

393402
# Validate the universe domain.

gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ class {{ service.client_name }}Meta(type):
102102

103103

104104
class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
105-
"""{{ service.meta.doc|rst(width=72, indent=4) }}"""
105+
"""{{ service.meta.doc|rst(width=72, indent=4) }}{% if service.version|length %}
106+
This class implements API version {{ service.version }}."""
107+
{% else %}"""
108+
{% endif %}
106109

107110
@staticmethod
108111
def _get_default_mtls_endpoint(api_endpoint):

gapic/templates/setup.py.j2

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ else:
3030
release_status = "Development Status :: 5 - Production/Stable"
3131

3232
dependencies = [
33-
"google-api-core[grpc] >= 1.34.1, <3.0.0dev,!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,!=2.10.*",
33+
"google-api-core[grpc] @ git+https://github.com/googleapis/python-api-core.git@add-api-version-header",
34+
#"google-api-core[grpc] >= 1.34.1, <3.0.0dev,!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,!=2.10.*",
3435
# Exclude incompatible versions of `google-auth`
3536
# See https://github.com/googleapis/google-cloud-python/issues/12364
3637
"google-auth >= 2.14.1, <3.0.0dev,!=2.24.0,!=2.25.0",

gapic/templates/testing/_default_constraints.j2

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{% from '_pypi_packages.j2' import pypi_packages %}
22
# This constraints file is required for unit tests.
33
# List all library dependencies and extras in this file.
4-
google-api-core
54
proto-plus
65
protobuf
76
{% for package_tuple, package_info in pypi_packages.items() %}

gapic/templates/testing/constraints-3.7.txt.j2

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
# Pin the version to the lower bound.
66
# e.g., if setup.py has "google-cloud-foo >= 1.14.0, < 2.0.0dev",
77
# Then this file should have google-cloud-foo==1.14.0
8-
google-api-core==1.34.1
98
google-auth==2.14.1
109
proto-plus==1.22.3
1110
protobuf==3.19.5

gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,31 @@ def test_{{ method_name }}(request_type, transport: str = 'grpc'):
121121
{% endif %}
122122
{% endwith %}{# auto_populated_field_sample_value #}
123123

124+
{% if service.version %}
125+
{% for mode in ["", "async"] %}
126+
{% if mode == "async" %}
127+
async def test_{{ method_name }}_api_version_header_async():
128+
client = {{ service.async_client_name }}(credentials=ga_credentials.AnonymousCredentials())
129+
{% else %}
130+
def test_{{ method_name }}_api_version_header():
131+
client = {{ service.client_name }}(credentials=ga_credentials.AnonymousCredentials())
132+
{% endif %}
133+
# Mock the actual call within the gRPC stub, and fake the request.
134+
with mock.patch.object(
135+
type(client.transport.{{ method.transport_safe_name|snake_case }}),
136+
'__call__'
137+
) as call:
138+
{% if mode == "async" %}
139+
await client.{{ method_name }}()
140+
{% else %}
141+
client.{{ method_name }}()
142+
{% endif %}
143+
144+
# Establish that the api version header was sent.
145+
_, _, kw = call.mock_calls[0]
146+
assert kw['metadata'][0] == (gapic_v1.version_header.API_VERSION_METADATA_KEY, "{{ service.version }}")
147+
{% endfor %}{# mode #}
148+
{% endif %}{# service.version #}
124149

125150
{% if not method.client_streaming %}
126151
def test_{{ method_name }}_empty_call():

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
# Ensure that the lower bounds of these dependencies match what we have in the
2929
# templated setup.py.j2: https://github.com/googleapis/gapic-generator-python/blob/main/gapic/templates/setup.py.j2
3030
"click >= 6.7",
31-
"google-api-core[grpc] >= 1.34.1, <3.0.0dev,!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,!=2.10.*",
31+
"google-api-core @ git+https://github.com/googleapis/python-api-core.git@add-api-version-header",
32+
#"google-api-core[grpc] >= 1.34.1, <3.0.0dev,!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,!=2.10.*",
3233
"googleapis-common-protos >= 1.55.0",
3334
"grpcio >= 1.24.3",
3435
# 2.11.0 is required which adds the `default` argument to `jinja-filters.map()`

test_utils/test_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def make_service(
3232
visible_resources: typing.Optional[
3333
typing.Mapping[str, wrappers.CommonResource]
3434
] = None,
35+
version: str = "",
3536
) -> wrappers.Service:
3637
visible_resources = visible_resources or {}
3738
# Define a service descriptor, and set a host and oauth scopes if
@@ -40,6 +41,8 @@ def make_service(
4041
if host:
4142
service_pb.options.Extensions[client_pb2.default_host] = host
4243
service_pb.options.Extensions[client_pb2.oauth_scopes] = ','.join(scopes)
44+
if version:
45+
service_pb.options.Extensions[client_pb2.api_version] = version
4346

4447
# Return a service object to test.
4548
return wrappers.Service(

tests/fragments/google/api/client.proto

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,20 @@ extend google.protobuf.ServiceOptions {
9696
// ...
9797
// }
9898
string oauth_scopes = 1050;
99+
100+
// The API version of this service, which should be sent by version-aware
101+
// clients to the service. This allows services to abide by the schema and
102+
// behavior of the service at the time this API version was deployed.
103+
// The format of the API version must be treated as opaque by clients.
104+
// Services may use a format with an apparent structure, but clients must
105+
// not rely on this to determine components within an API version, or attempt
106+
// to construct other valid API versions. Note that this is for upcoming
107+
// functionality and may not be implemented for all services.
108+
//
109+
// Example:
110+
//
111+
// service Foo {
112+
// option (google.api.api_version) = "v1_20230821_preview";
113+
// }
114+
string api_version = 525000001;
99115
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (C) 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
syntax = "proto3";
16+
17+
package google.fragment;
18+
19+
import "google/protobuf/struct.proto";
20+
import "google/api/client.proto";
21+
22+
service MyServiceWithVersion {
23+
option (google.api.api_version) = "v1_20230601";
24+
option (google.api.default_host) = "my.example.com";
25+
26+
rpc MyMethod(MethodRequest) returns (MethodResponse) {
27+
option (google.api.method_signature) = "parameter";
28+
}
29+
}
30+
31+
message MethodRequest {
32+
google.protobuf.Value parameter = 1;
33+
}
34+
35+
message MethodResponse {
36+
google.protobuf.Value result = 1;
37+
}

tests/integration/goldens/asset/setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
release_status = "Development Status :: 5 - Production/Stable"
4040

4141
dependencies = [
42-
"google-api-core[grpc] >= 1.34.1, <3.0.0dev,!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,!=2.10.*",
42+
"google-api-core[grpc] @ git+https://github.com/googleapis/python-api-core.git@add-api-version-header",
43+
#"google-api-core[grpc] >= 1.34.1, <3.0.0dev,!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,!=2.10.*",
4344
# Exclude incompatible versions of `google-auth`
4445
# See https://github.com/googleapis/google-cloud-python/issues/12364
4546
"google-auth >= 2.14.1, <3.0.0dev,!=2.24.0,!=2.25.0",

tests/integration/goldens/asset/testing/constraints-3.10.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22
# This constraints file is required for unit tests.
33
# List all library dependencies and extras in this file.
4-
google-api-core
54
proto-plus
65
protobuf
76
google-cloud-access-context-manager

tests/integration/goldens/asset/testing/constraints-3.11.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22
# This constraints file is required for unit tests.
33
# List all library dependencies and extras in this file.
4-
google-api-core
54
proto-plus
65
protobuf
76
google-cloud-access-context-manager

tests/integration/goldens/asset/testing/constraints-3.12.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22
# This constraints file is required for unit tests.
33
# List all library dependencies and extras in this file.
4-
google-api-core
54
proto-plus
65
protobuf
76
google-cloud-access-context-manager

tests/integration/goldens/asset/testing/constraints-3.7.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# Pin the version to the lower bound.
55
# e.g., if setup.py has "google-cloud-foo >= 1.14.0, < 2.0.0dev",
66
# Then this file should have google-cloud-foo==1.14.0
7-
google-api-core==1.34.1
87
google-auth==2.14.1
98
proto-plus==1.22.3
109
protobuf==3.19.5

tests/integration/goldens/asset/testing/constraints-3.8.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22
# This constraints file is required for unit tests.
33
# List all library dependencies and extras in this file.
4-
google-api-core
54
proto-plus
65
protobuf
76
google-cloud-access-context-manager

tests/integration/goldens/asset/testing/constraints-3.9.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22
# This constraints file is required for unit tests.
33
# List all library dependencies and extras in this file.
4-
google-api-core
54
proto-plus
65
protobuf
76
google-cloud-access-context-manager

tests/integration/goldens/credentials/setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
release_status = "Development Status :: 5 - Production/Stable"
4040

4141
dependencies = [
42-
"google-api-core[grpc] >= 1.34.1, <3.0.0dev,!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,!=2.10.*",
42+
"google-api-core[grpc] @ git+https://github.com/googleapis/python-api-core.git@add-api-version-header",
43+
#"google-api-core[grpc] >= 1.34.1, <3.0.0dev,!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,!=2.10.*",
4344
# Exclude incompatible versions of `google-auth`
4445
# See https://github.com/googleapis/google-cloud-python/issues/12364
4546
"google-auth >= 2.14.1, <3.0.0dev,!=2.24.0,!=2.25.0",
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# -*- coding: utf-8 -*-
22
# This constraints file is required for unit tests.
33
# List all library dependencies and extras in this file.
4-
google-api-core
54
proto-plus
65
protobuf
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# -*- coding: utf-8 -*-
22
# This constraints file is required for unit tests.
33
# List all library dependencies and extras in this file.
4-
google-api-core
54
proto-plus
65
protobuf
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# -*- coding: utf-8 -*-
22
# This constraints file is required for unit tests.
33
# List all library dependencies and extras in this file.
4-
google-api-core
54
proto-plus
65
protobuf

tests/integration/goldens/credentials/testing/constraints-3.7.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# Pin the version to the lower bound.
55
# e.g., if setup.py has "google-cloud-foo >= 1.14.0, < 2.0.0dev",
66
# Then this file should have google-cloud-foo==1.14.0
7-
google-api-core==1.34.1
87
google-auth==2.14.1
98
proto-plus==1.22.3
109
protobuf==3.19.5
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# -*- coding: utf-8 -*-
22
# This constraints file is required for unit tests.
33
# List all library dependencies and extras in this file.
4-
google-api-core
54
proto-plus
65
protobuf
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# -*- coding: utf-8 -*-
22
# This constraints file is required for unit tests.
33
# List all library dependencies and extras in this file.
4-
google-api-core
54
proto-plus
65
protobuf

tests/integration/goldens/eventarc/setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
release_status = "Development Status :: 5 - Production/Stable"
4040

4141
dependencies = [
42-
"google-api-core[grpc] >= 1.34.1, <3.0.0dev,!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,!=2.10.*",
42+
"google-api-core[grpc] @ git+https://github.com/googleapis/python-api-core.git@add-api-version-header",
43+
#"google-api-core[grpc] >= 1.34.1, <3.0.0dev,!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,!=2.10.*",
4344
# Exclude incompatible versions of `google-auth`
4445
# See https://github.com/googleapis/google-cloud-python/issues/12364
4546
"google-auth >= 2.14.1, <3.0.0dev,!=2.24.0,!=2.25.0",
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22
# This constraints file is required for unit tests.
33
# List all library dependencies and extras in this file.
4-
google-api-core
54
proto-plus
65
protobuf
76
grpc-google-iam-v1
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22
# This constraints file is required for unit tests.
33
# List all library dependencies and extras in this file.
4-
google-api-core
54
proto-plus
65
protobuf
76
grpc-google-iam-v1
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22
# This constraints file is required for unit tests.
33
# List all library dependencies and extras in this file.
4-
google-api-core
54
proto-plus
65
protobuf
76
grpc-google-iam-v1

tests/integration/goldens/eventarc/testing/constraints-3.7.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# Pin the version to the lower bound.
55
# e.g., if setup.py has "google-cloud-foo >= 1.14.0, < 2.0.0dev",
66
# Then this file should have google-cloud-foo==1.14.0
7-
google-api-core==1.34.1
87
google-auth==2.14.1
98
proto-plus==1.22.3
109
protobuf==3.19.5

0 commit comments

Comments
 (0)