@@ -53,7 +53,7 @@ def __init__(
53
53
credentials : Optional [ga_credentials .Credentials ] = None ,
54
54
credentials_file : Optional [str ] = None ,
55
55
scopes : Optional [Sequence [str ]] = None ,
56
- channel : Optional [grpc .Channel ] = None ,
56
+ channel : Optional [Union [ grpc .Channel , Callable [..., grpc . Channel ]] ] = None ,
57
57
api_mtls_endpoint : Optional [str ] = None ,
58
58
client_cert_source : Optional [Callable [[], Tuple [bytes , bytes ]]] = None ,
59
59
ssl_channel_credentials : Optional [grpc .ChannelCredentials ] = None ,
@@ -73,14 +73,17 @@ def __init__(
73
73
credentials identify the application to the service; if none
74
74
are specified, the client will attempt to ascertain the
75
75
credentials from the environment.
76
- This argument is ignored if ``channel`` is provided.
76
+ This argument is ignored if a ``channel`` instance is provided.
77
77
credentials_file (Optional[str]): A file with credentials that can
78
78
be loaded with :func:`google.auth.load_credentials_from_file`.
79
- This argument is ignored if ``channel`` is provided.
79
+ This argument is ignored if a ``channel`` instance is provided.
80
80
scopes (Optional(Sequence[str])): A list of scopes. This argument is
81
- ignored if ``channel`` is provided.
82
- channel (Optional[grpc.Channel]): A ``Channel`` instance through
83
- which to make calls.
81
+ ignored if a ``channel`` instance is provided.
82
+ channel (Optional[Union[grpc.Channel, Callable[..., grpc.Channel]]]):
83
+ A ``Channel`` instance through which to make calls, or a Callable
84
+ that constructs and returns one. If set to None, ``self.create_channel``
85
+ is used to create the channel. If a Callable is given, it will be called
86
+ with the same arguments as used in ``self.create_channel``.
84
87
api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint.
85
88
If provided, it overrides the ``host`` argument and tries to create
86
89
a mutual TLS channel with client SSL credentials from
@@ -90,11 +93,11 @@ def __init__(
90
93
private key bytes, both in PEM format. It is ignored if
91
94
``api_mtls_endpoint`` is None.
92
95
ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials
93
- for the grpc channel. It is ignored if ``channel`` is provided.
96
+ for the grpc channel. It is ignored if a ``channel`` instance is provided.
94
97
client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]):
95
98
A callback to provide client certificate bytes and private key bytes,
96
99
both in PEM format. It is used to configure a mutual TLS channel. It is
97
- ignored if ``channel`` or ``ssl_channel_credentials`` is provided.
100
+ ignored if a ``channel`` instance or ``ssl_channel_credentials`` is provided.
98
101
quota_project_id (Optional[str]): An optional project to use for billing
99
102
and quota.
100
103
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
@@ -121,9 +124,10 @@ def __init__(
121
124
if client_cert_source :
122
125
warnings .warn ("client_cert_source is deprecated" , DeprecationWarning )
123
126
124
- if channel :
127
+ if isinstance ( channel , grpc . Channel ) :
125
128
# Ignore credentials if a channel was passed.
126
- credentials = False
129
+ credentials = None
130
+ self ._ignore_credentials = True
127
131
# If a channel was explicitly provided, set it.
128
132
self ._grpc_channel = channel
129
133
self ._ssl_channel_credentials = None
@@ -162,7 +166,9 @@ def __init__(
162
166
)
163
167
164
168
if not self ._grpc_channel :
165
- self ._grpc_channel = type (self ).create_channel (
169
+ # initialize with the provided callable or the default channel
170
+ channel_init = channel or type (self ).create_channel
171
+ self ._grpc_channel = channel_init (
166
172
self ._host ,
167
173
# use the credentials which are saved
168
174
credentials = self ._credentials ,
0 commit comments