Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,6 @@
{% endif %}
)
{% if method.lro %}
{% if service.version %}
lro_metadata = None
if HAS_GOOGLE_API_CORE_VERSION_HEADER: # pragma: NO COVER
lro_metadata = [version_header.to_api_version_header("{{ service.version }}")]
{% endif %}

# Wrap the response in an operation future.
response = {{ method.client_output.ident.module_alias or method.client_output.ident.module }}.from_gapic(
Expand All @@ -187,7 +182,7 @@
{{ method.lro.response_type.ident }},
metadata_type={{ method.lro.metadata_type.ident }},
{% if service.version %}
grpc_metadata=lro_metadata,
grpc_metadata=[version_header.to_api_version_header("{{ service.version }}")],
{% endif %}
)
{% elif method.paged_result_field %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,8 @@
{% endmacro %}

{% macro add_google_api_core_version_header_import(service_version) %}
{#
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
{# NOTE: `pragma: NO COVER` is needed since the coverage for presubmits isn't combined. #}
except ImportError: # pragma: NO COVER
HAS_GOOGLE_API_CORE_VERSION_HEADER = False
from google.api_core import version_header
{% endif %}{# service_version #}
{% endmacro %}

Expand Down Expand Up @@ -98,10 +86,9 @@ except ImportError: # pragma: NO COVER
`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 }}"),
)
metadata = tuple(metadata) + (
version_header.to_api_version_header("{{ service_version }}"),
)
{% endif %}{# service_version #}
{% endmacro %}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,6 @@ class {{ service.async_client_name }}:
{% endif %}
)
{% if method.lro %}
{% if service.version %}
lro_metadata = None
if HAS_GOOGLE_API_CORE_VERSION_HEADER: # pragma: NO COVER
lro_metadata = [version_header.to_api_version_header("{{ service.version }}")]
{% endif %}

# Wrap the response in an operation future.
response = {{ method.client_output_async.ident.module_alias or method.client_output_async.ident.module }}.from_gapic(
Expand All @@ -443,7 +438,7 @@ class {{ service.async_client_name }}:
{{ method.lro.response_type.ident }},
metadata_type={{ method.lro.metadata_type.ident }},
{% if service.version %}
grpc_metadata=lro_metadata,
grpc_metadata=[version_header.to_api_version_header("{{ service.version }}")],
{% endif %}
)
{% elif method.paged_result_field %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{% macro assert_api_version_header(service_version) -%}
{% if service_version %}

if HAS_GOOGLE_API_CORE_VERSION_HEADER: # pragma: NO COVER
assert (
version_header.API_VERSION_METADATA_KEY,
"{{ service_version }}",
) in kw["metadata"]
assert (
version_header.API_VERSION_METADATA_KEY,
"{{ service_version }}",
) in kw["metadata"]
{%- endif -%}
{% endmacro -%}
{% if api.has_operations_mixin and ('grpc' in opts.transport or 'grpc_asyncio' in opts.transport) %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,41 +206,35 @@ async def test_{{ method_name }}_api_version_header_async(transport_name="grpc")
def test_{{ method_name }}_api_version_header(transport_name):
client = {{ service.client_name }}(credentials=ga_credentials.AnonymousCredentials(), transport=transport_name)
{% endif %}
# TODO: Make this test unconditional once the minimum supported version of
# google-api-core becomes 2.19.0 or higher.
api_core_major, api_core_minor = [int(part) for part in api_core_version.__version__.split(".")[0:2]]
if api_core_major > 2 or (api_core_major == 2 and api_core_minor >= 19):
# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(
type(client.transport.{{ method.transport_safe_name|snake_case }}),
'__call__'
) as call:
call.return_value = {{ method.output.ident }}()
{% set response_prefix = "response = " if method.lro else "" %}
{% if mode == "async" %}
{{ response_prefix }}await client.{{ method_name }}()
{% else %}
{{ response_prefix }}client.{{ method_name }}()
{% endif %}

# Establish that the api version header was sent.
_, _, kw = call.mock_calls[0]
assert (
version_header.API_VERSION_METADATA_KEY,
"{{ service.version }}",
) in kw['metadata']
{% if method.lro %}
assert (
version_header.API_VERSION_METADATA_KEY,
"{{ service.version }}",
) in response._refresh.keywords["metadata"]
assert (
version_header.API_VERSION_METADATA_KEY,
"{{ service.version }}",
) in response._cancel.keywords["metadata"]
# Mock the actual call within the gRPC stub, and fake the request.
with mock.patch.object(
type(client.transport.{{ method.transport_safe_name|snake_case }}),
'__call__'
) as call:
call.return_value = {{ method.output.ident }}()
{% set response_prefix = "response = " if method.lro else "" %}
{% if mode == "async" %}
{{ response_prefix }}await client.{{ method_name }}()
{% else %}
{{ response_prefix }}client.{{ method_name }}()
{% endif %}
else:
pytest.skip("google-api-core>=2.19.0 is required for `google.api_core.version_header`")

# Establish that the api version header was sent.
_, _, kw = call.mock_calls[0]
assert (
version_header.API_VERSION_METADATA_KEY,
"{{ service.version }}",
) in kw['metadata']
{% if method.lro %}
assert (
version_header.API_VERSION_METADATA_KEY,
"{{ service.version }}",
) in response._refresh.keywords["metadata"]
assert (
version_header.API_VERSION_METADATA_KEY,
"{{ service.version }}",
) in response._cancel.keywords["metadata"]
{% endif %}
{% endfor %}{# mode #}
{% endwith %}
{% endfor %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -743,10 +743,9 @@ def test_{{ method_name }}_pager(transport_name: str = "grpc"):
)
{% endif %}
{% if service.version %}
if HAS_GOOGLE_API_CORE_VERSION_HEADER:
expected_metadata = tuple(expected_metadata) + (
version_header.to_api_version_header("{{ service.version }}"),
)
expected_metadata = tuple(expected_metadata) + (
version_header.to_api_version_header("{{ service.version }}"),
)
{% endif %}
pager = client.{{ method_name }}(request={}, retry=retry, timeout=timeout)

Expand Down
Loading
Loading