15
15
# limitations under the License.
16
16
#
17
17
18
- import warnings
19
- from typing import Callable , Dict , Optional , Sequence , Tuple
18
+ from typing import Callable , Dict
20
19
21
20
from google .api_core import grpc_helpers # type: ignore
22
- from google .api_core import gapic_v1 # type: ignore
23
- from google import auth # type: ignore
24
21
from google .auth import credentials # type: ignore
25
- from google .auth .transport .grpc import SslCredentials # type: ignore
26
22
27
23
import grpc # type: ignore
28
24
29
25
from google .cloud .aiplatform_v1beta1 .types import prediction_service
30
26
31
- from .base import PredictionServiceTransport , DEFAULT_CLIENT_INFO
27
+ from .base import PredictionServiceTransport
32
28
33
29
34
30
class PredictionServiceGrpcTransport (PredictionServiceTransport ):
@@ -44,21 +40,12 @@ class PredictionServiceGrpcTransport(PredictionServiceTransport):
44
40
top of HTTP/2); the ``grpcio`` package must be installed.
45
41
"""
46
42
47
- _stubs : Dict [str , Callable ]
48
-
49
43
def __init__ (
50
44
self ,
51
45
* ,
52
46
host : str = "aiplatform.googleapis.com" ,
53
47
credentials : credentials .Credentials = None ,
54
- credentials_file : str = None ,
55
- scopes : Sequence [str ] = None ,
56
- channel : grpc .Channel = None ,
57
- api_mtls_endpoint : str = None ,
58
- client_cert_source : Callable [[], Tuple [bytes , bytes ]] = None ,
59
- ssl_channel_credentials : grpc .ChannelCredentials = None ,
60
- quota_project_id : Optional [str ] = None ,
61
- client_info : gapic_v1 .client_info .ClientInfo = DEFAULT_CLIENT_INFO ,
48
+ channel : grpc .Channel = None
62
49
) -> None :
63
50
"""Instantiate the transport.
64
51
@@ -70,123 +57,28 @@ def __init__(
70
57
are specified, the client will attempt to ascertain the
71
58
credentials from the environment.
72
59
This argument is ignored if ``channel`` is provided.
73
- credentials_file (Optional[str]): A file with credentials that can
74
- be loaded with :func:`google.auth.load_credentials_from_file`.
75
- This argument is ignored if ``channel`` is provided.
76
- scopes (Optional(Sequence[str])): A list of scopes. This argument is
77
- ignored if ``channel`` is provided.
78
60
channel (Optional[grpc.Channel]): A ``Channel`` instance through
79
61
which to make calls.
80
- api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint.
81
- If provided, it overrides the ``host`` argument and tries to create
82
- a mutual TLS channel with client SSL credentials from
83
- ``client_cert_source`` or applicatin default SSL credentials.
84
- client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]):
85
- Deprecated. A callback to provide client SSL certificate bytes and
86
- private key bytes, both in PEM format. It is ignored if
87
- ``api_mtls_endpoint`` is None.
88
- ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials
89
- for grpc channel. It is ignored if ``channel`` is provided.
90
- quota_project_id (Optional[str]): An optional project to use for billing
91
- and quota.
92
- client_info (google.api_core.gapic_v1.client_info.ClientInfo):
93
- The client info used to send a user-agent string along with
94
- API requests. If ``None``, then default info will be used.
95
- Generally, you only need to set this if you're developing
96
- your own client library.
97
-
98
- Raises:
99
- google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport
100
- creation failed for any reason.
101
- google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials``
102
- and ``credentials_file`` are passed.
103
62
"""
104
- self . _ssl_channel_credentials = ssl_channel_credentials
105
-
63
+ # Sanity check: Ensure that channel and credentials are not both
64
+ # provided.
106
65
if channel :
107
- # Sanity check: Ensure that channel and credentials are not both
108
- # provided.
109
66
credentials = False
110
67
111
- # If a channel was explicitly provided, set it.
112
- self ._grpc_channel = channel
113
- self ._ssl_channel_credentials = None
114
- elif api_mtls_endpoint :
115
- warnings .warn (
116
- "api_mtls_endpoint and client_cert_source are deprecated" ,
117
- DeprecationWarning ,
118
- )
119
-
120
- host = (
121
- api_mtls_endpoint
122
- if ":" in api_mtls_endpoint
123
- else api_mtls_endpoint + ":443"
124
- )
125
-
126
- if credentials is None :
127
- credentials , _ = auth .default (
128
- scopes = self .AUTH_SCOPES , quota_project_id = quota_project_id
129
- )
130
-
131
- # Create SSL credentials with client_cert_source or application
132
- # default SSL credentials.
133
- if client_cert_source :
134
- cert , key = client_cert_source ()
135
- ssl_credentials = grpc .ssl_channel_credentials (
136
- certificate_chain = cert , private_key = key
137
- )
138
- else :
139
- ssl_credentials = SslCredentials ().ssl_credentials
140
-
141
- # create a new channel. The provided one is ignored.
142
- self ._grpc_channel = type (self ).create_channel (
143
- host ,
144
- credentials = credentials ,
145
- credentials_file = credentials_file ,
146
- ssl_credentials = ssl_credentials ,
147
- scopes = scopes or self .AUTH_SCOPES ,
148
- quota_project_id = quota_project_id ,
149
- )
150
- self ._ssl_channel_credentials = ssl_credentials
151
- else :
152
- host = host if ":" in host else host + ":443"
153
-
154
- if credentials is None :
155
- credentials , _ = auth .default (
156
- scopes = self .AUTH_SCOPES , quota_project_id = quota_project_id
157
- )
158
-
159
- # create a new channel. The provided one is ignored.
160
- self ._grpc_channel = type (self ).create_channel (
161
- host ,
162
- credentials = credentials ,
163
- credentials_file = credentials_file ,
164
- ssl_credentials = ssl_channel_credentials ,
165
- scopes = scopes or self .AUTH_SCOPES ,
166
- quota_project_id = quota_project_id ,
167
- )
168
-
68
+ # Run the base constructor.
69
+ super ().__init__ (host = host , credentials = credentials )
169
70
self ._stubs = {} # type: Dict[str, Callable]
170
71
171
- # Run the base constructor.
172
- super ().__init__ (
173
- host = host ,
174
- credentials = credentials ,
175
- credentials_file = credentials_file ,
176
- scopes = scopes or self .AUTH_SCOPES ,
177
- quota_project_id = quota_project_id ,
178
- client_info = client_info ,
179
- )
72
+ # If a channel was explicitly provided, set it.
73
+ if channel :
74
+ self ._grpc_channel = channel
180
75
181
76
@classmethod
182
77
def create_channel (
183
78
cls ,
184
79
host : str = "aiplatform.googleapis.com" ,
185
80
credentials : credentials .Credentials = None ,
186
- credentials_file : str = None ,
187
- scopes : Optional [Sequence [str ]] = None ,
188
- quota_project_id : Optional [str ] = None ,
189
- ** kwargs ,
81
+ ** kwargs
190
82
) -> grpc .Channel :
191
83
"""Create and return a gRPC channel object.
192
84
Args:
@@ -196,37 +88,30 @@ def create_channel(
196
88
credentials identify this application to the service. If
197
89
none are specified, the client will attempt to ascertain
198
90
the credentials from the environment.
199
- credentials_file (Optional[str]): A file with credentials that can
200
- be loaded with :func:`google.auth.load_credentials_from_file`.
201
- This argument is mutually exclusive with credentials.
202
- scopes (Optional[Sequence[str]]): A optional list of scopes needed for this
203
- service. These are only used when credentials are not specified and
204
- are passed to :func:`google.auth.default`.
205
- quota_project_id (Optional[str]): An optional project to use for billing
206
- and quota.
207
91
kwargs (Optional[dict]): Keyword arguments, which are passed to the
208
92
channel creation.
209
93
Returns:
210
94
grpc.Channel: A gRPC channel object.
211
-
212
- Raises:
213
- google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials``
214
- and ``credentials_file`` are passed.
215
95
"""
216
- scopes = scopes or cls .AUTH_SCOPES
217
96
return grpc_helpers .create_channel (
218
- host ,
219
- credentials = credentials ,
220
- credentials_file = credentials_file ,
221
- scopes = scopes ,
222
- quota_project_id = quota_project_id ,
223
- ** kwargs ,
97
+ host , credentials = credentials , scopes = cls .AUTH_SCOPES , ** kwargs
224
98
)
225
99
226
100
@property
227
101
def grpc_channel (self ) -> grpc .Channel :
228
- """Return the channel designed to connect to this service.
102
+ """Create the channel designed to connect to this service.
103
+
104
+ This property caches on the instance; repeated calls return
105
+ the same channel.
229
106
"""
107
+ # Sanity check: Only create a new channel if we do not already
108
+ # have one.
109
+ if not hasattr (self , "_grpc_channel" ):
110
+ self ._grpc_channel = self .create_channel (
111
+ self ._host , credentials = self ._credentials ,
112
+ )
113
+
114
+ # Return the channel from cache.
230
115
return self ._grpc_channel
231
116
232
117
@property
@@ -267,13 +152,11 @@ def explain(
267
152
268
153
Perform an online explanation.
269
154
270
- If
271
- ``deployed_model_id``
272
- is specified, the corresponding DeployModel must have
155
+ If [ExplainRequest.deployed_model_id] is specified, the
156
+ corresponding DeployModel must have
273
157
``explanation_spec``
274
- populated. If
275
- ``deployed_model_id``
276
- is not specified, all DeployedModels must have
158
+ populated. If [ExplainRequest.deployed_model_id] is not
159
+ specified, all DeployedModels must have
277
160
``explanation_spec``
278
161
populated. Only deployed AutoML tabular Models have
279
162
explanation_spec.
0 commit comments