Skip to content
Draft
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 @@ -78,6 +78,9 @@ from google.longrunning import operations_pb2 # type: ignore
{% endif %}
{% endfilter %}
from .transports.base import {{ service.name }}Transport, DEFAULT_CLIENT_INFO
{% if service.any_extended_operations_methods %}
from .transports.base import _inherit_universe_in_client_options
{% endif %}
{% if 'grpc' in opts.transport %}
from .transports.grpc import {{ service.grpc_transport_name }}
from .transports.grpc_asyncio import {{ service.grpc_asyncio_transport_name }}
Expand Down Expand Up @@ -476,6 +479,42 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
)
self._transport = cast({{ service.name }}Transport, transport)
self._api_endpoint = self._transport.host
{% if service.any_extended_operations_methods %}
if (
client_options is not None
or self._universe_domain != {{ service.client_name }}._DEFAULT_UNIVERSE
):
configure_ext_ops = getattr(
self._transport,
"_configure_extended_operations_client_options",
None,
)
if callable(configure_ext_ops):
existing_ext_ops_options = getattr(
self._transport,
"_extended_operations_client_options",
None,
)
if not isinstance(
existing_ext_ops_options, client_options_lib.ClientOptions
):
existing_ext_ops_options = None
effective_options = (
existing_ext_ops_options
if client_options is None
else client_options
)
fallback_endpoint = (
existing_ext_ops_options.api_endpoint
if existing_ext_ops_options is not None
else None
)
configure_ext_ops(
self._universe_domain,
effective_options,
fallback_endpoint=fallback_endpoint,
)
{% endif %}

self._api_endpoint = (self._api_endpoint or
get_api_endpoint(
Expand Down Expand Up @@ -523,6 +562,15 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
client_info=client_info,
**({"client_options": client_options} if client_options is not None else {}),
)
{% if service.any_extended_operations_methods %}
self._transport._extended_operations_client_options = ( # pragma: NO COVER
_inherit_universe_in_client_options(
self._universe_domain,
self._client_options,
clear_credentials=True,
)
)
{% endif %}
return

{% endif %}
Expand Down Expand Up @@ -565,6 +613,15 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
**({"client_options": client_options} if client_options is not None else {}),
}
self._transport = transport_init(**transport_kwargs)
{% if service.any_extended_operations_methods %}
self._transport._extended_operations_client_options = (
_inherit_universe_in_client_options(
self._universe_domain,
self._client_options,
clear_credentials=True,
)
)
{% endif %}

if "async" not in str(self._transport):
if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(std_logging.DEBUG): # pragma: NO COVER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@
{% block content %}

import abc
{% if service.any_extended_operations_methods %}
from collections.abc import Mapping
import copy
import os
{% endif %}
Comment thread
chalmerlowe marked this conversation as resolved.
Resolved
from typing import {% if service.any_extended_operations_methods %}Any, {% endif %}Awaitable, Callable, Dict, Optional, Sequence, Union

{% set package_path = api.naming.module_namespace|join('.') + "." + api.naming.versioned_module_name %}
from {{package_path}} import gapic_version as package_version
from {{package_path}}._compat import (
ASYNC_WRAP_METHOD_SUPPORTS_KIND as _ASYNC_WRAP_METHOD_SUPPORTS_KIND,
{% if service.any_extended_operations_methods %}
DEFAULT_UNIVERSE as _DEFAULT_UNIVERSE,
{% endif %}
SUPPORTS_DEFAULT_HOST as _SUPPORTS_DEFAULT_HOST,
WRAP_METHOD_SUPPORTS_TRACING as _WRAP_METHOD_SUPPORTS_TRACING,
{% if service.any_extended_operations_methods %}
get_universe_domain as _get_universe_domain,
{% endif %}
)
_ASYNC_WRAP_METHOD_SUPPORTS_TRACING = _WRAP_METHOD_SUPPORTS_TRACING
_WRAP_METHOD_SUPPORTS_KIND = _WRAP_METHOD_SUPPORTS_TRACING
Expand Down Expand Up @@ -63,6 +74,87 @@ from {{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + ser

DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(gapic_version=package_version.__version__)
DEFAULT_CLIENT_INFO.protobuf_runtime_version = google.protobuf.__version__
{% if service.any_extended_operations_methods %}


def _inherit_universe_in_client_options(
parent_universe: str,
client_options: Union[
client_options_lib.ClientOptions, Mapping[str, Any], None
] = None,
*,
parent_api_endpoint: Optional[str] = None,
clear_credentials: bool = False,
) -> Optional[client_options_lib.ClientOptions]:
"""Inherit ``parent_universe`` onto child ``client_options`` when unset.

Args:
parent_universe (str): Resolved universe domain of the parent client.
client_options:
Optional child ``ClientOptions`` instance or mapping. If ``None``,
a new ``ClientOptions`` is created only when propagation is required.
parent_api_endpoint (Optional[str]): Optional parent ``api_endpoint``
override inherited when both ``api_endpoint`` and ``universe_domain``
are unset on ``client_options``. If ``None``, no endpoint is inherited.
clear_credentials (bool): Whether to clear ``credentials_file`` and
``api_key`` on the returned copy to avoid duplicate-credential errors.

Returns:
Optional[google.api_core.client_options.ClientOptions]: Updated
``ClientOptions`` instance (never mutating caller inputs), or ``None``
when ``client_options`` is ``None`` and default resolution already
matches the parent.

Raises:
TypeError: If ``client_options`` is not a ``ClientOptions`` instance,
``Mapping``, or ``None``.
"""
env_universe = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN")
should_propagate = parent_universe != _DEFAULT_UNIVERSE or (
env_universe is not None and env_universe.strip() != parent_universe
)
has_parent_endpoint = bool(parent_api_endpoint)
if client_options is None:
if should_propagate or has_parent_endpoint:
return client_options_lib.ClientOptions(
api_endpoint=parent_api_endpoint if has_parent_endpoint else None,
universe_domain=parent_universe if should_propagate else None,
)
return None
if isinstance(client_options, Mapping):
opts = client_options_lib.from_dict(client_options)
is_fresh_copy = True
elif isinstance(client_options, client_options_lib.ClientOptions):
opts = client_options
is_fresh_copy = False
else:
raise TypeError(
"client_options must be a ClientOptions instance, Mapping, or None; "
f"got {type(client_options).__name__}"
)
opts_universe = getattr(opts, "universe_domain", None)
needs_universe = opts_universe is None and should_propagate
needs_endpoint = (
has_parent_endpoint
and not opts.api_endpoint
and opts_universe is None
)
needs_cred_clear = clear_credentials and bool(
getattr(opts, "credentials_file", None)
or getattr(opts, "api_key", None)
)
if needs_universe or needs_endpoint or needs_cred_clear:
if not is_fresh_copy:
opts = copy.copy(opts)
if needs_universe:
opts.universe_domain = parent_universe
if needs_endpoint:
opts.api_endpoint = parent_api_endpoint
if needs_cred_clear:
opts.credentials_file = None
opts.api_key = None
return opts
{% endif %}


class {{ service.name }}Transport(abc.ABC):
Expand All @@ -75,6 +167,11 @@ class {{ service.name }}Transport(abc.ABC):
)

DEFAULT_HOST: str = '{% if service.host %}{{ service.host }}{% endif %}'
{% if service.any_extended_operations_methods %}
_extended_operations_client_options: Optional[
client_options_lib.ClientOptions
] = None
{% endif %}

def __init__(
self, *,
Expand Down Expand Up @@ -172,6 +269,34 @@ class {{ service.name }}Transport(abc.ABC):
self._default_host: Optional[str] = (
None if self._user_provided_channel else self._host
)
{% if service.any_extended_operations_methods %}
self._extended_operations_client_options = None
env_universe = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN")
if isinstance(client_options, Mapping):
client_universe = client_options.get("universe_domain")
else:
client_universe = getattr(client_options, "universe_domain", None)
if client_universe is not None or env_universe is None or env_universe.strip():
extended_ops_universe = _get_universe_domain(
client_universe,
env_universe,
default_universe=_DEFAULT_UNIVERSE,
)
default_hosts = (
self.DEFAULT_HOST,
f"{self.DEFAULT_HOST}:443",
f"https://{self.DEFAULT_HOST}",
f"https://{self.DEFAULT_HOST}:443",
)
fallback_endpoint = None
if self._default_host and self._default_host not in default_hosts:
fallback_endpoint = self._default_host
self._configure_extended_operations_client_options(
extended_ops_universe,
client_options,
fallback_endpoint=fallback_endpoint,
)
{% endif %}
self._wrapped_methods: Dict[Callable, Callable] = {}

@property
Expand Down Expand Up @@ -494,6 +619,39 @@ class {{ service.name }}Transport(abc.ABC):
def kind(self) -> str:
raise NotImplementedError()

{% if service.any_extended_operations_methods %}
def _configure_extended_operations_client_options(
self,
universe_domain: str,
client_options: Optional[
Union[client_options_lib.ClientOptions, Mapping[str, Any]]
] = None,
*,
fallback_endpoint: Optional[str] = None,
) -> None:
"""Configure ``ClientOptions`` forwarded to extended operations sub-clients.

Preserves ``fallback_endpoint`` (such as a custom transport ``host``) when
``client_options`` does not set ``api_endpoint``, without mutating caller inputs.
"""
opts = _inherit_universe_in_client_options(
universe_domain,
client_options,
clear_credentials=self._credentials is not None,
)
if fallback_endpoint:
if opts is None:
opts = client_options_lib.ClientOptions(
api_endpoint=fallback_endpoint
)
elif not opts.api_endpoint:
if opts is client_options:
opts = copy.copy(opts)
opts.api_endpoint = fallback_endpoint
self._extended_operations_client_options = opts
self._extended_operations_services.clear()
{% endif %}

{% for operations_service in api.get_extended_operations_services(service)|sort(attribute="name") %}
@property
def {{ operations_service.client_name|snake_case|make_private }}(self) -> {{ operations_service.name|snake_case }}.{{ operations_service.client_name }}:
Expand All @@ -502,6 +660,7 @@ class {{ service.name }}Transport(abc.ABC):
ex_op_service = {{ operations_service.name|snake_case }}.{{ operations_service.client_name }}(
credentials=self._credentials,
transport=self.kind,
client_options=self._extended_operations_client_options,
)
self._extended_operations_services["{{ operations_service.name|snake_case }}"] = ex_op_service

Expand Down
Loading
Loading