@@ -110,79 +110,60 @@ def __init__(
110110 google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials``
111111 and ``credentials_file`` are passed.
112112 """
113+ self ._grpc_channel = None
113114 self ._ssl_channel_credentials = ssl_channel_credentials
115+ self ._stubs : Dict [str , Callable ] = {}
114116
115117 if api_mtls_endpoint :
116118 warnings .warn ("api_mtls_endpoint is deprecated" , DeprecationWarning )
117119 if client_cert_source :
118120 warnings .warn ("client_cert_source is deprecated" , DeprecationWarning )
119121
120122 if channel :
121- # Sanity check: Ensure that channel and credentials are not both
122- # provided.
123+ # Ignore credentials if a channel was passed.
123124 credentials = False
124-
125125 # If a channel was explicitly provided, set it.
126126 self ._grpc_channel = channel
127127 self ._ssl_channel_credentials = None
128- elif api_mtls_endpoint :
129- host = (
130- api_mtls_endpoint
131- if ":" in api_mtls_endpoint
132- else api_mtls_endpoint + ":443"
133- )
134-
135- if credentials is None :
136- credentials , _ = auth .default (
137- scopes = self .AUTH_SCOPES , quota_project_id = quota_project_id
138- )
139-
140- # Create SSL credentials with client_cert_source or application
141- # default SSL credentials.
142- if client_cert_source :
143- cert , key = client_cert_source ()
144- ssl_credentials = grpc .ssl_channel_credentials (
145- certificate_chain = cert , private_key = key
146- )
147- else :
148- ssl_credentials = SslCredentials ().ssl_credentials
149128
150- # create a new channel. The provided one is ignored.
151- self ._grpc_channel = type (self ).create_channel (
152- host ,
153- credentials = credentials ,
154- credentials_file = credentials_file ,
155- ssl_credentials = ssl_credentials ,
156- scopes = scopes or self .AUTH_SCOPES ,
157- quota_project_id = quota_project_id ,
158- options = [
159- ("grpc.max_send_message_length" , - 1 ),
160- ("grpc.max_receive_message_length" , - 1 ),
161- ("grpc.keepalive_time_ms" , 30000 ),
162- ],
163- )
164- self ._ssl_channel_credentials = ssl_credentials
165129 else :
166- host = host if ":" in host else host + ":443"
130+ if api_mtls_endpoint :
131+ host = api_mtls_endpoint
132+
133+ # Create SSL credentials with client_cert_source or application
134+ # default SSL credentials.
135+ if client_cert_source :
136+ cert , key = client_cert_source ()
137+ self ._ssl_channel_credentials = grpc .ssl_channel_credentials (
138+ certificate_chain = cert , private_key = key
139+ )
140+ else :
141+ self ._ssl_channel_credentials = SslCredentials ().ssl_credentials
167142
168- if credentials is None :
169- credentials , _ = auth .default (
170- scopes = self .AUTH_SCOPES , quota_project_id = quota_project_id
171- )
143+ else :
144+ if client_cert_source_for_mtls and not ssl_channel_credentials :
145+ cert , key = client_cert_source_for_mtls ()
146+ self ._ssl_channel_credentials = grpc .ssl_channel_credentials (
147+ certificate_chain = cert , private_key = key
148+ )
172149
173- if client_cert_source_for_mtls and not ssl_channel_credentials :
174- cert , key = client_cert_source_for_mtls ()
175- self ._ssl_channel_credentials = grpc .ssl_channel_credentials (
176- certificate_chain = cert , private_key = key
177- )
150+ # The base transport sets the host, credentials and scopes
151+ super ().__init__ (
152+ host = host ,
153+ credentials = credentials ,
154+ credentials_file = credentials_file ,
155+ scopes = scopes ,
156+ quota_project_id = quota_project_id ,
157+ client_info = client_info ,
158+ )
178159
179- # create a new channel. The provided one is ignored.
160+ if not self . _grpc_channel :
180161 self ._grpc_channel = type (self ).create_channel (
181- host ,
182- credentials = credentials ,
162+ self . _host ,
163+ credentials = self . _credentials ,
183164 credentials_file = credentials_file ,
165+ scopes = self ._scopes ,
184166 ssl_credentials = self ._ssl_channel_credentials ,
185- scopes = scopes or self .AUTH_SCOPES ,
186167 quota_project_id = quota_project_id ,
187168 options = [
188169 ("grpc.max_send_message_length" , - 1 ),
@@ -191,17 +172,8 @@ def __init__(
191172 ],
192173 )
193174
194- self ._stubs = {} # type: Dict[str, Callable]
195-
196- # Run the base constructor.
197- super ().__init__ (
198- host = host ,
199- credentials = credentials ,
200- credentials_file = credentials_file ,
201- scopes = scopes or self .AUTH_SCOPES ,
202- quota_project_id = quota_project_id ,
203- client_info = client_info ,
204- )
175+ # Wrap messages. This must be done after self._grpc_channel exists
176+ self ._prep_wrapped_messages (client_info )
205177
206178 @classmethod
207179 def create_channel (
@@ -215,7 +187,7 @@ def create_channel(
215187 ) -> grpc .Channel :
216188 """Create and return a gRPC channel object.
217189 Args:
218- address (Optional[str]): The host for the channel to use.
190+ host (Optional[str]): The host for the channel to use.
219191 credentials (Optional[~.Credentials]): The
220192 authorization credentials to attach to requests. These
221193 credentials identify this application to the service. If
0 commit comments