Skip to content

Commit 3f02e35

Browse files
committed
feat: split client_helpers into domain-specific modules
Splits the monolithic client_helpers.py into routing.py, client_cert.py, config_helpers.py, and method_helpers.py to improve readability and maintainability. Updates gapic_v1 exports and test imports accordingly.
1 parent 2353e82 commit 3f02e35

7 files changed

Lines changed: 352 additions & 306 deletions

File tree

packages/google-api-core/google/api_core/gapic_v1/__init__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from google.api_core.gapic_v1 import client_helpers
16-
from google.api_core.gapic_v1 import client_info
17-
from google.api_core.gapic_v1 import config
18-
from google.api_core.gapic_v1 import config_async
19-
from google.api_core.gapic_v1 import method
20-
from google.api_core.gapic_v1 import method_async
21-
from google.api_core.gapic_v1 import routing_header
15+
from google.api_core.gapic_v1 import (client_cert, client_info, config,
16+
config_async, config_helpers, method,
17+
method_async, method_helpers, routing,
18+
routing_header)
2219

2320
__all__ = [
24-
"client_helpers",
21+
"client_cert",
2522
"client_info",
2623
"config",
2724
"config_async",
25+
"config_helpers",
2826
"method",
2927
"method_async",
28+
"method_helpers",
29+
"routing",
3030
"routing_header",
3131
]
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2026 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+
#
16+
17+
"""Helpers for client certificate handling and mTLS authentication."""
18+
19+
import os
20+
from typing import Any, Optional
21+
22+
from google.auth.transport import mtls # type: ignore
23+
24+
25+
def use_client_cert_effective() -> bool:
26+
"""Returns whether client certificate should be used for mTLS if the
27+
google-auth version supports should_use_client_cert automatic mTLS
28+
enablement.
29+
30+
Alternatively, read from the GOOGLE_API_USE_CLIENT_CERTIFICATE env var.
31+
32+
Returns:
33+
bool: whether client certificate should be used for mTLS
34+
Raises:
35+
ValueError: (If using a version of google-auth without
36+
should_use_client_cert and GOOGLE_API_USE_CLIENT_CERTIFICATE is
37+
set to an unexpected value.)
38+
"""
39+
# check if google-auth version supports should_use_client_cert for
40+
# automatic mTLS enablement
41+
if hasattr(mtls, "should_use_client_cert"): # pragma: NO COVER
42+
return mtls.should_use_client_cert()
43+
else: # pragma: NO COVER
44+
# if unsupported, fallback to reading from env var
45+
use_client_cert_str = os.getenv(
46+
"GOOGLE_API_USE_CLIENT_CERTIFICATE", "false"
47+
).lower()
48+
if use_client_cert_str not in ("true", "false"):
49+
raise ValueError(
50+
"Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` "
51+
"must be either `true` or `false`"
52+
)
53+
return use_client_cert_str == "true"
54+
55+
56+
def get_client_cert_source(
57+
provided_cert_source: Optional[Any], use_cert_flag: bool
58+
) -> Optional[Any]:
59+
"""Return the client cert source to be used by the client.
60+
61+
Args:
62+
provided_cert_source (bytes): The client certificate source provided.
63+
use_cert_flag (bool): A flag indicating whether to use the
64+
client certificate.
65+
66+
Returns:
67+
bytes or None: The client cert source to be used by the client.
68+
"""
69+
client_cert_source = None
70+
if use_cert_flag:
71+
if provided_cert_source:
72+
client_cert_source = provided_cert_source
73+
elif (
74+
hasattr(mtls, "has_default_client_cert_source")
75+
and mtls.has_default_client_cert_source()
76+
):
77+
client_cert_source = mtls.default_client_cert_source()
78+
return client_cert_source
79+
80+
81+
# Backward compatibility aliases for private methods
82+
# Previously, gapic-generator-python generated clients used these methods
83+
_use_client_cert_effective = use_client_cert_effective
84+
_get_client_cert_source = get_client_cert_source

packages/google-api-core/google/api_core/gapic_v1/client_helpers.py

Lines changed: 0 additions & 257 deletions
This file was deleted.

0 commit comments

Comments
 (0)