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

Commit b0a0a5b

Browse files
committed
feat: add credentials_file and scopes support
1 parent d8baafc commit b0a0a5b

3 files changed

Lines changed: 43 additions & 8 deletions

File tree

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,22 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
196196
# instance provides an extensibility point for unusual situations.
197197
if isinstance(transport, {{ service.name }}Transport):
198198
# transport is a {{ service.name }}Transport instance.
199-
if credentials:
199+
if credentials or client_options.credentials_file:
200200
raise ValueError('When providing a transport instance, '
201201
'provide its credentials directly.')
202+
if client_options.scopes:
203+
raise ValueError(
204+
"When providing a transport instance, "
205+
"provide its scopes directly."
206+
)
202207
self._transport = transport
203208
else:
204209
Transport = type(self).get_transport_class(transport)
205210
self._transport = Transport(
206211
credentials=credentials,
207-
host=client_options.api_endpoint,
212+
credentials_file=client_options.credentials_file,
213+
host=self.DEFAULT_ENDPOINT,
214+
scopes=client_options.scopes,
208215
api_mtls_endpoint=client_options.api_endpoint,
209216
client_cert_source=client_options.client_cert_source,
210217
)

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class {{ service.name }}Transport(abc.ABC):
3030
self, *,
3131
host: str{% if service.host %} = '{{ service.host }}'{% endif %},
3232
credentials: credentials.Credentials = None,
33+
credentials_file: str = None,
34+
scopes: Sequence[str] = self.AUTH_SCOPES,
3335
**kwargs,
3436
) -> None:
3537
"""Instantiate the transport.
@@ -42,6 +44,10 @@ class {{ service.name }}Transport(abc.ABC):
4244
credentials identify the application to the service; if none
4345
are specified, the client will attempt to ascertain the
4446
credentials from the environment.
47+
credentials_file (Optional[str]): A file with credentials that can
48+
be loaded with :func:`google.auth.load_credentials_from_file`.
49+
This argument is mutually exclusive with credentials.
50+
scope (Optional[Sequence[str]]): A list of scopes.
4551
"""
4652
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
4753
if ':' not in host:
@@ -50,8 +56,13 @@ class {{ service.name }}Transport(abc.ABC):
5056

5157
# If no credentials are provided, then determine the appropriate
5258
# defaults.
53-
if credentials is None:
54-
credentials, _ = auth.default(scopes=self.AUTH_SCOPES)
59+
if credentials and credentials_file:
60+
raise ValueError("'credentials_file' and 'credentials' are mutually exclusive")
61+
62+
if credentials_file is not None:
63+
credentials, _ = auth.load_credentials_from_file(credentials_file, scopes=scopes)
64+
elif credentials is None:
65+
credentials, _ = auth.default(scopes=scopes)
5566

5667
# Save the credentials.
5768
self._credentials = credentials

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport):
4040
def __init__(self, *,
4141
host: str{% if service.host %} = '{{ service.host }}'{% endif %},
4242
credentials: credentials.Credentials = None,
43+
credentials_file: str = None,
44+
scopes: Sequence[str] = None,
4345
channel: grpc.Channel = None,
4446
api_mtls_endpoint: str = None,
4547
client_cert_source: Callable[[], Tuple[bytes, bytes]] = None) -> None:
@@ -54,6 +56,11 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport):
5456
are specified, the client will attempt to ascertain the
5557
credentials from the environment.
5658
This argument is ignored if ``channel`` is provided.
59+
credentials_file (Optional[str]): A file with credentials that can
60+
be loaded with :func:`google.auth.load_credentials_from_file`.
61+
This argument is ignored if ``channel`` is provided.
62+
scopes (Optional(Sequence[str])): A list of scopes. This argument is
63+
ignored if ``channel`` is provided.
5764
channel (Optional[grpc.Channel]): A ``Channel`` instance through
5865
which to make calls.
5966
api_mtls_endpoint (Optional[str]): The mutual TLS endpoint. If
@@ -66,8 +73,9 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport):
6673
is None.
6774

6875
Raises:
69-
google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport
70-
creation failed for any reason.
76+
google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport
77+
creation failed for any reason.
78+
ValueError: If both ``credentials`` and ``credentials_file`` are passed.
7179
"""
7280
if channel:
7381
# Sanity check: Ensure that channel and credentials are not both
@@ -96,18 +104,20 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport):
96104
self._grpc_channel = type(self).create_channel(
97105
host,
98106
credentials=credentials,
107+
credentials_file=credentials_file,
99108
ssl_credentials=ssl_credentials,
100-
scopes=self.AUTH_SCOPES,
109+
scopes=scopes or self.AUTH_SCOPES,
101110
)
102111

103112
# Run the base constructor.
104-
super().__init__(host=host, credentials=credentials)
113+
super().__init__(host=host, credentials=credentials, credentials_file=credentials_file, scopes=scopes)
105114
self._stubs = {} # type: Dict[str, Callable]
106115

107116
@classmethod
108117
def create_channel(cls,
109118
host: str{% if service.host %} = '{{ service.host }}'{% endif %},
110119
credentials: credentials.Credentials = None,
120+
credentials_file: str = None,
111121
scopes: Optional[Sequence[str]] = None,
112122
**kwargs) -> grpc.Channel:
113123
"""Create and return a gRPC channel object.
@@ -118,18 +128,25 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport):
118128
credentials identify this application to the service. If
119129
none are specified, the client will attempt to ascertain
120130
the credentials from the environment.
131+
credentials_file (Optional[str]): A file with credentials that can
132+
be loaded with :func:`google.auth.load_credentials_from_file`.
133+
This argument is mutually exclusive with credentials.
121134
scopes (Optional[Sequence[str]]): A optional list of scopes needed for this
122135
service. These are only used when credentials are not specified and
123136
are passed to :func:`google.auth.default`.
124137
kwargs (Optional[dict]): Keyword arguments, which are passed to the
125138
channel creation.
126139
Returns:
127140
grpc.Channel: A gRPC channel object.
141+
142+
Raises:
143+
ValueError: If both ``credentials`` and ``credentials_file`` are passed.
128144
"""
129145
scopes = scopes or cls.AUTH_SCOPES
130146
return grpc_helpers.create_channel(
131147
host,
132148
credentials=credentials,
149+
credentials_file=credentials_file,
133150
scopes=scopes,
134151
**kwargs
135152
)

0 commit comments

Comments
 (0)