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

Commit b287451

Browse files
committed
fix: use api-core exception
1 parent 380b32c commit b287451

6 files changed

Lines changed: 166 additions & 27 deletions

File tree

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
@@ -210,7 +210,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
210210
self._transport = Transport(
211211
credentials=credentials,
212212
credentials_file=client_options.credentials_file,
213-
host=self.DEFAULT_ENDPOINT,
213+
host=client_options.api_endpoint,
214214
scopes=client_options.scopes,
215215
api_mtls_endpoint=client_options.api_endpoint,
216216
client_cert_source=client_options.client_cert_source,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import abc
55
import typing
66

77
from google import auth
8+
from google.api_core import exceptions # type: ignore
89
{%- if service.has_lro %}
910
from google.api_core import operations_v1 # type: ignore
1011
{%- endif %}
@@ -57,7 +58,7 @@ class {{ service.name }}Transport(abc.ABC):
5758
# If no credentials are provided, then determine the appropriate
5859
# defaults.
5960
if credentials and credentials_file:
60-
raise ValueError("'credentials_file' and 'credentials' are mutually exclusive")
61+
raise exceptions.DuplicateCredentialArgs("'credentials_file' and 'credentials' are mutually exclusive")
6162

6263
if credentials_file is not None:
6364
credentials, _ = auth.load_credentials_from_file(credentials_file, scopes=scopes)

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport):
7575
Raises:
7676
google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport
7777
creation failed for any reason.
78-
ValueError: If both ``credentials`` and ``credentials_file`` are passed.
78+
google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials``
79+
and ``credentials_file`` are passed.
7980
"""
8081
if channel:
8182
# Sanity check: Ensure that channel and credentials are not both
@@ -110,7 +111,13 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport):
110111
)
111112

112113
# Run the base constructor.
113-
super().__init__(host=host, credentials=credentials, credentials_file=credentials_file, scopes=scopes)
114+
super().__init__(
115+
host=host,
116+
credentials=credentials,
117+
credentials_file=credentials_file,
118+
scopes=scopes or self.AUTH_SCOPES
119+
)
120+
114121
self._stubs = {} # type: Dict[str, Callable]
115122

116123
@classmethod
@@ -140,7 +147,8 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport):
140147
grpc.Channel: A gRPC channel object.
141148

142149
Raises:
143-
ValueError: If both ``credentials`` and ``credentials_file`` are passed.
150+
google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials``
151+
and ``credentials_file`` are passed.
144152
"""
145153
scopes = scopes or cls.AUTH_SCOPES
146154
return grpc_helpers.create_channel(

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class {{ service.grpc_asyncio_transport_name }}({{ service.name }}Transport):
4343
def create_channel(cls,
4444
host: str{% if service.host %} = '{{ service.host }}'{% endif %},
4545
credentials: credentials.Credentials = None,
46+
credentials_file: Optional[str] = None,
4647
scopes: Optional[Sequence[str]] = None,
4748
**kwargs) -> aio.Channel:
4849
"""Create and return a gRPC AsyncIO channel object.
@@ -53,6 +54,9 @@ class {{ service.grpc_asyncio_transport_name }}({{ service.name }}Transport):
5354
credentials identify this application to the service. If
5455
none are specified, the client will attempt to ascertain
5556
the credentials from the environment.
57+
credentials_file (Optional[str]): A file with credentials that can
58+
be loaded with :func:`google.auth.load_credentials_from_file`.
59+
This argument is ignored if ``channel`` is provided.
5660
scopes (Optional[Sequence[str]]): A optional list of scopes needed for this
5761
service. These are only used when credentials are not specified and
5862
are passed to :func:`google.auth.default`.
@@ -65,13 +69,16 @@ class {{ service.grpc_asyncio_transport_name }}({{ service.name }}Transport):
6569
return grpc_helpers_async.create_channel(
6670
host,
6771
credentials=credentials,
72+
credentials_file=credentials_file,
6873
scopes=scopes,
6974
**kwargs
7075
)
7176

7277
def __init__(self, *,
7378
host: str{% if service.host %} = '{{ service.host }}'{% endif %},
7479
credentials: credentials.Credentials = None,
80+
credentials_file: Optional[str] = None,
81+
scopes: Optional[Sequence[str]] = None,
7582
channel: aio.Channel = None,
7683
api_mtls_endpoint: str = None,
7784
client_cert_source: Callable[[], Tuple[bytes, bytes]] = None) -> None:
@@ -86,6 +93,12 @@ class {{ service.grpc_asyncio_transport_name }}({{ service.name }}Transport):
8693
are specified, the client will attempt to ascertain the
8794
credentials from the environment.
8895
This argument is ignored if ``channel`` is provided.
96+
credentials_file (Optional[str]): A file with credentials that can
97+
be loaded with :func:`google.auth.load_credentials_from_file`.
98+
This argument is ignored if ``channel`` is provided.
99+
scopes (Optional[Sequence[str]]): A optional list of scopes needed for this
100+
service. These are only used when credentials are not specified and
101+
are passed to :func:`google.auth.default`.
89102
channel (Optional[aio.Channel]): A ``Channel`` instance through
90103
which to make calls.
91104
api_mtls_endpoint (Optional[str]): The mutual TLS endpoint. If
@@ -98,8 +111,10 @@ class {{ service.grpc_asyncio_transport_name }}({{ service.name }}Transport):
98111
is None.
99112

100113
Raises:
101-
google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport
114+
google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport
102115
creation failed for any reason.
116+
google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials``
117+
and ``credentials_file`` are passed.
103118
"""
104119
if channel:
105120
# Sanity check: Ensure that channel and credentials are not both
@@ -125,12 +140,19 @@ class {{ service.grpc_asyncio_transport_name }}({{ service.name }}Transport):
125140
self._grpc_channel = type(self).create_channel(
126141
host,
127142
credentials=credentials,
143+
credentials_file=credentials_file,
128144
ssl_credentials=ssl_credentials,
129-
scopes=self.AUTH_SCOPES,
145+
scopes=scopes or self.AUTH_SCOPES,
130146
)
131147

132148
# Run the base constructor.
133-
super().__init__(host=host, credentials=credentials)
149+
super().__init__(
150+
host=host,
151+
credentials=credentials,
152+
credentials_file=credentials_file,
153+
scopes=scopes or self.AUTH_SCOPES
154+
)
155+
134156
self._stubs = {}
135157

136158
@property

0 commit comments

Comments
 (0)