@@ -26,8 +26,10 @@ def get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]:
2626
2727 Convert "*.sandbox.googleapis.com" and "*.googleapis.com" to
2828 "*.mtls.sandbox.googleapis.com" and "*.mtls.googleapis.com" respectively.
29+
2930 Args:
3031 api_endpoint (Optional[str]): the api endpoint to convert.
32+
3133 Returns:
3234 Optional[str]: converted mTLS api endpoint.
3335 """
@@ -52,9 +54,30 @@ def get_api_endpoint(
5254 use_mtls_endpoint : str ,
5355 default_universe : str ,
5456 default_mtls_endpoint : Optional [str ],
55- default_endpoint_template : Optional [str ],
56- ) -> Optional [str ]:
57- """Return the API endpoint used by the client."""
57+ default_endpoint_template : str ,
58+ ) -> str :
59+ """Return the API endpoint used by the client.
60+
61+ Args:
62+ api_override (Optional[str]): The API endpoint override. If specified,
63+ this is always returned.
64+ client_cert_source (Optional[Any]): The client certificate source used by the client.
65+ universe_domain (str): The universe domain used by the client.
66+ use_mtls_endpoint (str): How to use the mTLS endpoint. Possible values
67+ are "always", "auto", or "never".
68+ default_universe (str): The default universe domain.
69+ default_mtls_endpoint (Optional[str]): The default mTLS endpoint.
70+ default_endpoint_template (str): The default endpoint template containing
71+ a placeholder `{UNIVERSE_DOMAIN}`.
72+
73+ Returns:
74+ str: The API endpoint to be used by the client.
75+
76+ Raises:
77+ google.auth.exceptions.MutualTLSChannelError: If mTLS is requested but
78+ not supported in the configured universe domain.
79+ ValueError: If mTLS is requested but no mTLS endpoint is available.
80+ """
5881 if api_override is not None :
5982 return api_override
6083 elif use_mtls_endpoint == "always" or (
@@ -64,26 +87,40 @@ def get_api_endpoint(
6487 raise MutualTLSChannelError (
6588 f"mTLS is not supported in any universe other than { default_universe } ."
6689 )
90+ if not default_mtls_endpoint :
91+ raise ValueError ("mTLS endpoint is not available." )
6792 return default_mtls_endpoint
6893 else :
69- return (
70- default_endpoint_template .format (UNIVERSE_DOMAIN = universe_domain )
71- if default_endpoint_template
72- else None
73- )
94+ return default_endpoint_template .format (UNIVERSE_DOMAIN = universe_domain )
7495
7596
7697def get_universe_domain (
7798 client_universe_domain : Optional [str ],
7899 universe_domain_env : Optional [str ],
79100 default_universe : str ,
80101) -> str :
81- """Return the universe domain used by the client."""
82- universe_domain = default_universe
102+ """Return the universe domain used by the client.
103+
104+ Args:
105+ client_universe_domain (Optional[str]): The universe domain configured
106+ via client options.
107+ universe_domain_env (Optional[str]): The universe domain configured
108+ via environment variable.
109+ default_universe (str): The default universe domain.
110+
111+ Returns:
112+ str: The universe domain to be used by the client.
113+
114+ Raises:
115+ ValueError: If the resolved universe domain is an empty string.
116+ """
83117 if client_universe_domain is not None :
84118 universe_domain = client_universe_domain .strip ()
85119 elif universe_domain_env is not None :
86120 universe_domain = universe_domain_env .strip ()
121+ else :
122+ universe_domain = default_universe
123+
87124 if not universe_domain :
88125 raise ValueError ("Universe Domain cannot be an empty string." )
89126 return universe_domain
0 commit comments