Skip to content

Commit 1f95ebc

Browse files
committed
feat(generator): add _compat.py fallback for routing helpers
1 parent 69be436 commit 1f95ebc

3 files changed

Lines changed: 78 additions & 2 deletions

File tree

packages/gapic-generator/gapic/generator/generator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def get_response(self, api_schema: api.API, opts: Options) -> CodeGeneratorRespo
120120
for template_name in client_templates:
121121
# Quick check: Skip "private" templates.
122122
filename = template_name.split("/")[-1]
123-
if filename.startswith("_") and filename != "__init__.py.j2":
123+
if filename.startswith("_") and filename not in ("__init__.py.j2", "_compat.py.j2"):
124124
continue
125125

126126
# Append to the output files dictionary.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# {% include '_license.j2' %}
2+
3+
import re
4+
from typing import Optional, Callable, Tuple, Union
5+
from google.auth.exceptions import MutualTLSChannelError
6+
7+
try:
8+
from google.api_core.gapic_v1.client_utils import (
9+
get_default_mtls_endpoint,
10+
get_api_endpoint,
11+
get_universe_domain,
12+
)
13+
except ImportError:
14+
# TODO: Remove these fallbacks when google-api-core >= 2.18.0 is the minimum required version.
15+
16+
def get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]:
17+
"""Converts api endpoint to mTLS endpoint."""
18+
if not api_endpoint:
19+
return api_endpoint
20+
21+
mtls_endpoint_re = re.compile(
22+
r"(?P<name>[^.]+)(?P<mtls>\.mtls)?(?P<sandbox>\.sandbox)?(?P<googledomain>\.googleapis\.com)?"
23+
)
24+
25+
m = mtls_endpoint_re.match(api_endpoint)
26+
if m is None:
27+
# Could not parse api_endpoint; return as-is.
28+
return api_endpoint
29+
30+
name, mtls, sandbox, googledomain = m.groups()
31+
if mtls or not googledomain:
32+
return api_endpoint
33+
34+
if sandbox:
35+
return api_endpoint.replace(
36+
"sandbox.googleapis.com", "mtls.sandbox.googleapis.com"
37+
)
38+
39+
return api_endpoint.replace(".googleapis.com", ".mtls.googleapis.com")
40+
41+
def get_api_endpoint(
42+
api_override: Optional[str],
43+
client_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]],
44+
universe_domain: str,
45+
use_mtls_endpoint: str,
46+
default_universe: str,
47+
default_mtls_endpoint: str,
48+
default_endpoint_template: str,
49+
) -> str:
50+
"""Return the API endpoint used by the client."""
51+
if api_override is not None:
52+
api_endpoint = api_override
53+
elif use_mtls_endpoint == "always" or (use_mtls_endpoint == "auto" and client_cert_source):
54+
if universe_domain != default_universe:
55+
raise MutualTLSChannelError(
56+
f"mTLS is not supported in any universe other than {default_universe}."
57+
)
58+
api_endpoint = default_mtls_endpoint
59+
else:
60+
api_endpoint = default_endpoint_template.format(UNIVERSE_DOMAIN=universe_domain)
61+
return api_endpoint
62+
63+
def get_universe_domain(
64+
client_universe_domain: Optional[str],
65+
universe_domain_env: Optional[str],
66+
default_universe: str,
67+
) -> str:
68+
"""Return the universe domain used by the client."""
69+
universe_domain = default_universe
70+
if client_universe_domain is not None:
71+
universe_domain = client_universe_domain
72+
elif universe_domain_env is not None:
73+
universe_domain = universe_domain_env
74+
if len(universe_domain.strip()) == 0:
75+
raise ValueError("Universe Domain cannot be an empty string.")
76+
return universe_domain

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ from google.api_core import exceptions as core_exceptions
3030
from google.api_core import extended_operation
3131
{% endif %}
3232
from google.api_core import gapic_v1
33-
from google.api_core.gapic_v1 import client_utils
33+
from {{package_path}} import _compat as client_utils
3434
from google.api_core import retry as retries
3535
from google.auth import credentials as ga_credentials # type: ignore
3636
from google.auth.transport import mtls # type: ignore

0 commit comments

Comments
 (0)