Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit 9b75600

Browse files
committed
chore: refactor shared code between services
1 parent 00f31ce commit 9b75600

File tree

6 files changed

+68
-48
lines changed

6 files changed

+68
-48
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{% extends '_base.py.j2' %}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
from google.auth.exceptions import MutualTLSChannelError # type: ignore
3+
4+
def _read_environment_variables():
5+
"""Returns the environment variables used by the client.
6+
7+
Returns:
8+
Tuple[bool, str, str]: returns the GOOGLE_API_USE_CLIENT_CERTIFICATE,
9+
GOOGLE_API_USE_MTLS_ENDPOINT, and GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variables.
10+
11+
Raises:
12+
ValueError: If GOOGLE_API_USE_CLIENT_CERTIFICATE is not
13+
any of ["true", "false"].
14+
google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT
15+
is not any of ["auto", "never", "always"].
16+
"""
17+
use_client_cert = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false").lower()
18+
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower()
19+
universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN")
20+
if use_client_cert not in ("true", "false"):
21+
raise ValueError("Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`")
22+
if use_mtls_endpoint not in ("auto", "never", "always"):
23+
raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`")
24+
return use_client_cert == "true", use_mtls_endpoint, universe_domain_env

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

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ except ImportError as e: # pragma: NO COVER
8989
{% endif %}{# if rest_async_io_enabled #}
9090
{% endif %}
9191

92+
from ._compat import legacy_helpers
9293

9394
class {{ service.client_name }}Meta(type):
9495
"""Metaclass for the {{ service.name }} client.
@@ -257,29 +258,6 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
257258

258259
{% endfor %}{# common resources #}
259260

260-
@staticmethod
261-
def _read_environment_variables():
262-
"""Returns the environment variables used by the client.
263-
264-
Returns:
265-
Tuple[bool, str, str]: returns the GOOGLE_API_USE_CLIENT_CERTIFICATE,
266-
GOOGLE_API_USE_MTLS_ENDPOINT, and GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variables.
267-
268-
Raises:
269-
ValueError: If GOOGLE_API_USE_CLIENT_CERTIFICATE is not
270-
any of ["true", "false"].
271-
google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT
272-
is not any of ["auto", "never", "always"].
273-
"""
274-
use_client_cert = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false").lower()
275-
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower()
276-
universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN")
277-
if use_client_cert not in ("true", "false"):
278-
raise ValueError("Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`")
279-
if use_mtls_endpoint not in ("auto", "never", "always"):
280-
raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`")
281-
return use_client_cert == "true", use_mtls_endpoint, universe_domain_env
282-
283261
@staticmethod
284262
def _get_client_cert_source(provided_cert_source, use_cert_flag):
285263
"""Return the client cert source to be used by the client.
@@ -473,7 +451,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
473451

474452
universe_domain_opt = getattr(self._client_options, 'universe_domain', None)
475453

476-
self._use_client_cert, self._use_mtls_endpoint, self._universe_domain_env = {{ service.client_name }}._read_environment_variables()
454+
self._use_client_cert, self._use_mtls_endpoint, self._universe_domain_env = legacy_helpers._read_environment_variables()
477455
self._client_cert_source = {{ service.client_name }}._get_client_cert_source(self._client_options.client_cert_source, self._use_client_cert)
478456
self._universe_domain = {{ service.client_name }}._get_universe_domain(universe_domain_opt, self._universe_domain_env)
479457
self._api_endpoint = None # updated below, depending on `transport`
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2025 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
from google.auth.exceptions import MutualTLSChannelError # type: ignore
3+
4+
def _read_environment_variables():
5+
"""Returns the environment variables used by the client.
6+
7+
Returns:
8+
Tuple[bool, str, str]: returns the GOOGLE_API_USE_CLIENT_CERTIFICATE,
9+
GOOGLE_API_USE_MTLS_ENDPOINT, and GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variables.
10+
11+
Raises:
12+
ValueError: If GOOGLE_API_USE_CLIENT_CERTIFICATE is not
13+
any of ["true", "false"].
14+
google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT
15+
is not any of ["auto", "never", "always"].
16+
"""
17+
use_client_cert = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false").lower()
18+
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower()
19+
universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN")
20+
if use_client_cert not in ("true", "false"):
21+
raise ValueError("Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`")
22+
if use_mtls_endpoint not in ("auto", "never", "always"):
23+
raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`")
24+
return use_client_cert == "true", use_mtls_endpoint, universe_domain_env

tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
from .transports.grpc_asyncio import AssetServiceGrpcAsyncIOTransport
6464
from .transports.rest import AssetServiceRestTransport
6565

66+
from ._compat import legacy_helpers
6667

6768
class AssetServiceClientMeta(type):
6869
"""Metaclass for the AssetService client.
@@ -317,29 +318,6 @@ def parse_common_location_path(path: str) -> Dict[str,str]:
317318
m = re.match(r"^projects/(?P<project>.+?)/locations/(?P<location>.+?)$", path)
318319
return m.groupdict() if m else {}
319320

320-
@staticmethod
321-
def _read_environment_variables():
322-
"""Returns the environment variables used by the client.
323-
324-
Returns:
325-
Tuple[bool, str, str]: returns the GOOGLE_API_USE_CLIENT_CERTIFICATE,
326-
GOOGLE_API_USE_MTLS_ENDPOINT, and GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variables.
327-
328-
Raises:
329-
ValueError: If GOOGLE_API_USE_CLIENT_CERTIFICATE is not
330-
any of ["true", "false"].
331-
google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT
332-
is not any of ["auto", "never", "always"].
333-
"""
334-
use_client_cert = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false").lower()
335-
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower()
336-
universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN")
337-
if use_client_cert not in ("true", "false"):
338-
raise ValueError("Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`")
339-
if use_mtls_endpoint not in ("auto", "never", "always"):
340-
raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`")
341-
return use_client_cert == "true", use_mtls_endpoint, universe_domain_env
342-
343321
@staticmethod
344322
def _get_client_cert_source(provided_cert_source, use_cert_flag):
345323
"""Return the client cert source to be used by the client.
@@ -530,7 +508,7 @@ def __init__(self, *,
530508

531509
universe_domain_opt = getattr(self._client_options, 'universe_domain', None)
532510

533-
self._use_client_cert, self._use_mtls_endpoint, self._universe_domain_env = AssetServiceClient._read_environment_variables()
511+
self._use_client_cert, self._use_mtls_endpoint, self._universe_domain_env = legacy_helpers._read_environment_variables()
534512
self._client_cert_source = AssetServiceClient._get_client_cert_source(self._client_options.client_cert_source, self._use_client_cert)
535513
self._universe_domain = AssetServiceClient._get_universe_domain(universe_domain_opt, self._universe_domain_env)
536514
self._api_endpoint = None # updated below, depending on `transport`

0 commit comments

Comments
 (0)