Skip to content
This repository was archived by the owner on Dec 31, 2023. It is now read-only.

Commit 0233804

Browse files
feat: add text extraction health care option in create model (#86)
v1beta1: Breaking changes to message attributes. `type` ->`type_`, `min` -> `min_`, `max` -> `max_`
1 parent 8c7d548 commit 0233804

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+6864
-1366
lines changed

docs/automl_v1beta1/services.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ Services for Google Cloud Automl v1beta1 API
77
.. automodule:: google.cloud.automl_v1beta1.services.prediction_service
88
:members:
99
:inherited-members:
10-
.. automodule:: google.cloud.automl_v1beta1.services.tables
11-
:members:
10+
.. automodule:: google.cloud.automl_v1beta1.services.tables
11+
:members:
1212
:inherited-members:

google/cloud/automl_v1/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
__all__ = (
105105
"AnnotationPayload",
106106
"AnnotationSpec",
107+
"AutoMlClient",
107108
"BatchPredictInputConfig",
108109
"BatchPredictOperationMetadata",
109110
"BatchPredictOutputConfig",
@@ -164,7 +165,6 @@
164165
"OutputConfig",
165166
"PredictRequest",
166167
"PredictResponse",
167-
"PredictionServiceClient",
168168
"TextClassificationDatasetMetadata",
169169
"TextClassificationModelMetadata",
170170
"TextExtractionAnnotation",
@@ -185,5 +185,5 @@
185185
"UndeployModelRequest",
186186
"UpdateDatasetRequest",
187187
"UpdateModelRequest",
188-
"AutoMlClient",
188+
"PredictionServiceClient",
189189
)

google/cloud/automl_v1/services/auto_ml/async_client.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
from google.auth import credentials # type: ignore
2929
from google.oauth2 import service_account # type: ignore
3030

31-
from google.api_core import operation
32-
from google.api_core import operation_async
31+
from google.api_core import operation # type: ignore
32+
from google.api_core import operation_async # type: ignore
3333
from google.cloud.automl_v1.services.auto_ml import pagers
3434
from google.cloud.automl_v1.types import annotation_spec
3535
from google.cloud.automl_v1.types import classification
@@ -79,9 +79,10 @@ class AutoMlAsyncClient:
7979
DEFAULT_ENDPOINT = AutoMlClient.DEFAULT_ENDPOINT
8080
DEFAULT_MTLS_ENDPOINT = AutoMlClient.DEFAULT_MTLS_ENDPOINT
8181

82-
model_path = staticmethod(AutoMlClient.model_path)
83-
8482
dataset_path = staticmethod(AutoMlClient.dataset_path)
83+
parse_dataset_path = staticmethod(AutoMlClient.parse_dataset_path)
84+
model_path = staticmethod(AutoMlClient.model_path)
85+
parse_model_path = staticmethod(AutoMlClient.parse_model_path)
8586

8687
from_service_account_file = AutoMlClient.from_service_account_file
8788
from_service_account_json = from_service_account_file
@@ -112,16 +113,19 @@ def __init__(
112113
client_options (ClientOptions): Custom options for the client. It
113114
won't take effect if a ``transport`` instance is provided.
114115
(1) The ``api_endpoint`` property can be used to override the
115-
default endpoint provided by the client. GOOGLE_API_USE_MTLS
116+
default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT
116117
environment variable can also be used to override the endpoint:
117118
"always" (always use the default mTLS endpoint), "never" (always
118-
use the default regular endpoint, this is the default value for
119-
the environment variable) and "auto" (auto switch to the default
120-
mTLS endpoint if client SSL credentials is present). However,
121-
the ``api_endpoint`` property takes precedence if provided.
122-
(2) The ``client_cert_source`` property is used to provide client
123-
SSL credentials for mutual TLS transport. If not provided, the
124-
default SSL credentials will be used if present.
119+
use the default regular endpoint) and "auto" (auto switch to the
120+
default mTLS endpoint if client certificate is present, this is
121+
the default value). However, the ``api_endpoint`` property takes
122+
precedence if provided.
123+
(2) If GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable
124+
is "true", then the ``client_cert_source`` property can be used
125+
to provide client certificate for mutual TLS transport. If
126+
not provided, the default SSL client certificate will be used if
127+
present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not
128+
set, no client certificate will be used.
125129
126130
Raises:
127131
google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport

google/cloud/automl_v1/services/auto_ml/client.py

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,24 @@
1616
#
1717

1818
from collections import OrderedDict
19+
from distutils import util
1920
import os
2021
import re
21-
from typing import Callable, Dict, Sequence, Tuple, Type, Union
22+
from typing import Callable, Dict, Optional, Sequence, Tuple, Type, Union
2223
import pkg_resources
2324

24-
import google.api_core.client_options as ClientOptions # type: ignore
25+
from google.api_core import client_options as client_options_lib # type: ignore
2526
from google.api_core import exceptions # type: ignore
2627
from google.api_core import gapic_v1 # type: ignore
2728
from google.api_core import retry as retries # type: ignore
2829
from google.auth import credentials # type: ignore
2930
from google.auth.transport import mtls # type: ignore
31+
from google.auth.transport.grpc import SslCredentials # type: ignore
3032
from google.auth.exceptions import MutualTLSChannelError # type: ignore
3133
from google.oauth2 import service_account # type: ignore
3234

33-
from google.api_core import operation
34-
from google.api_core import operation_async
35+
from google.api_core import operation # type: ignore
36+
from google.api_core import operation_async # type: ignore
3537
from google.cloud.automl_v1.services.auto_ml import pagers
3638
from google.cloud.automl_v1.types import annotation_spec
3739
from google.cloud.automl_v1.types import classification
@@ -196,9 +198,9 @@ def parse_model_path(path: str) -> Dict[str, str]:
196198
def __init__(
197199
self,
198200
*,
199-
credentials: credentials.Credentials = None,
200-
transport: Union[str, AutoMlTransport] = None,
201-
client_options: ClientOptions = None,
201+
credentials: Optional[credentials.Credentials] = None,
202+
transport: Union[str, AutoMlTransport, None] = None,
203+
client_options: Optional[client_options_lib.ClientOptions] = None,
202204
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
203205
) -> None:
204206
"""Instantiate the auto ml client.
@@ -212,19 +214,22 @@ def __init__(
212214
transport (Union[str, ~.AutoMlTransport]): The
213215
transport to use. If set to None, a transport is chosen
214216
automatically.
215-
client_options (ClientOptions): Custom options for the client. It
216-
won't take effect if a ``transport`` instance is provided.
217+
client_options (client_options_lib.ClientOptions): Custom options for the
218+
client. It won't take effect if a ``transport`` instance is provided.
217219
(1) The ``api_endpoint`` property can be used to override the
218-
default endpoint provided by the client. GOOGLE_API_USE_MTLS
220+
default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT
219221
environment variable can also be used to override the endpoint:
220222
"always" (always use the default mTLS endpoint), "never" (always
221-
use the default regular endpoint, this is the default value for
222-
the environment variable) and "auto" (auto switch to the default
223-
mTLS endpoint if client SSL credentials is present). However,
224-
the ``api_endpoint`` property takes precedence if provided.
225-
(2) The ``client_cert_source`` property is used to provide client
226-
SSL credentials for mutual TLS transport. If not provided, the
227-
default SSL credentials will be used if present.
223+
use the default regular endpoint) and "auto" (auto switch to the
224+
default mTLS endpoint if client certificate is present, this is
225+
the default value). However, the ``api_endpoint`` property takes
226+
precedence if provided.
227+
(2) If GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable
228+
is "true", then the ``client_cert_source`` property can be used
229+
to provide client certificate for mutual TLS transport. If
230+
not provided, the default SSL client certificate will be used if
231+
present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not
232+
set, no client certificate will be used.
228233
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
229234
The client info used to send a user-agent string along with
230235
API requests. If ``None``, then default info will be used.
@@ -236,29 +241,47 @@ def __init__(
236241
creation failed for any reason.
237242
"""
238243
if isinstance(client_options, dict):
239-
client_options = ClientOptions.from_dict(client_options)
244+
client_options = client_options_lib.from_dict(client_options)
240245
if client_options is None:
241-
client_options = ClientOptions.ClientOptions()
246+
client_options = client_options_lib.ClientOptions()
242247

243-
if client_options.api_endpoint is None:
244-
use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS", "never")
248+
# Create SSL credentials for mutual TLS if needed.
249+
use_client_cert = bool(
250+
util.strtobool(os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false"))
251+
)
252+
253+
ssl_credentials = None
254+
is_mtls = False
255+
if use_client_cert:
256+
if client_options.client_cert_source:
257+
import grpc # type: ignore
258+
259+
cert, key = client_options.client_cert_source()
260+
ssl_credentials = grpc.ssl_channel_credentials(
261+
certificate_chain=cert, private_key=key
262+
)
263+
is_mtls = True
264+
else:
265+
creds = SslCredentials()
266+
is_mtls = creds.is_mtls
267+
ssl_credentials = creds.ssl_credentials if is_mtls else None
268+
269+
# Figure out which api endpoint to use.
270+
if client_options.api_endpoint is not None:
271+
api_endpoint = client_options.api_endpoint
272+
else:
273+
use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto")
245274
if use_mtls_env == "never":
246-
client_options.api_endpoint = self.DEFAULT_ENDPOINT
275+
api_endpoint = self.DEFAULT_ENDPOINT
247276
elif use_mtls_env == "always":
248-
client_options.api_endpoint = self.DEFAULT_MTLS_ENDPOINT
277+
api_endpoint = self.DEFAULT_MTLS_ENDPOINT
249278
elif use_mtls_env == "auto":
250-
has_client_cert_source = (
251-
client_options.client_cert_source is not None
252-
or mtls.has_default_client_cert_source()
253-
)
254-
client_options.api_endpoint = (
255-
self.DEFAULT_MTLS_ENDPOINT
256-
if has_client_cert_source
257-
else self.DEFAULT_ENDPOINT
279+
api_endpoint = (
280+
self.DEFAULT_MTLS_ENDPOINT if is_mtls else self.DEFAULT_ENDPOINT
258281
)
259282
else:
260283
raise MutualTLSChannelError(
261-
"Unsupported GOOGLE_API_USE_MTLS value. Accepted values: never, auto, always"
284+
"Unsupported GOOGLE_API_USE_MTLS_ENDPOINT value. Accepted values: never, auto, always"
262285
)
263286

264287
# Save or instantiate the transport.
@@ -282,10 +305,9 @@ def __init__(
282305
self._transport = Transport(
283306
credentials=credentials,
284307
credentials_file=client_options.credentials_file,
285-
host=client_options.api_endpoint,
308+
host=api_endpoint,
286309
scopes=client_options.scopes,
287-
api_mtls_endpoint=client_options.api_endpoint,
288-
client_cert_source=client_options.client_cert_source,
310+
ssl_channel_credentials=ssl_credentials,
289311
quota_project_id=client_options.quota_project_id,
290312
client_info=client_info,
291313
)

google/cloud/automl_v1/services/auto_ml/transports/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import typing
2020
import pkg_resources
2121

22-
from google import auth
22+
from google import auth # type: ignore
2323
from google.api_core import exceptions # type: ignore
2424
from google.api_core import gapic_v1 # type: ignore
2525
from google.api_core import retry as retries # type: ignore

google/cloud/automl_v1/services/auto_ml/transports/grpc.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# limitations under the License.
1616
#
1717

18+
import warnings
1819
from typing import Callable, Dict, Optional, Sequence, Tuple
1920

2021
from google.api_core import grpc_helpers # type: ignore
@@ -24,7 +25,6 @@
2425
from google.auth import credentials # type: ignore
2526
from google.auth.transport.grpc import SslCredentials # type: ignore
2627

27-
2828
import grpc # type: ignore
2929

3030
from google.cloud.automl_v1.types import annotation_spec
@@ -78,6 +78,7 @@ def __init__(
7878
channel: grpc.Channel = None,
7979
api_mtls_endpoint: str = None,
8080
client_cert_source: Callable[[], Tuple[bytes, bytes]] = None,
81+
ssl_channel_credentials: grpc.ChannelCredentials = None,
8182
quota_project_id: Optional[str] = None,
8283
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
8384
) -> None:
@@ -98,14 +99,16 @@ def __init__(
9899
ignored if ``channel`` is provided.
99100
channel (Optional[grpc.Channel]): A ``Channel`` instance through
100101
which to make calls.
101-
api_mtls_endpoint (Optional[str]): The mutual TLS endpoint. If
102-
provided, it overrides the ``host`` argument and tries to create
102+
api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint.
103+
If provided, it overrides the ``host`` argument and tries to create
103104
a mutual TLS channel with client SSL credentials from
104105
``client_cert_source`` or applicatin default SSL credentials.
105-
client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): A
106-
callback to provide client SSL certificate bytes and private key
107-
bytes, both in PEM format. It is ignored if ``api_mtls_endpoint``
108-
is None.
106+
client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]):
107+
Deprecated. A callback to provide client SSL certificate bytes and
108+
private key bytes, both in PEM format. It is ignored if
109+
``api_mtls_endpoint`` is None.
110+
ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials
111+
for grpc channel. It is ignored if ``channel`` is provided.
109112
quota_project_id (Optional[str]): An optional project to use for billing
110113
and quota.
111114
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
@@ -128,6 +131,11 @@ def __init__(
128131
# If a channel was explicitly provided, set it.
129132
self._grpc_channel = channel
130133
elif api_mtls_endpoint:
134+
warnings.warn(
135+
"api_mtls_endpoint and client_cert_source are deprecated",
136+
DeprecationWarning,
137+
)
138+
131139
host = (
132140
api_mtls_endpoint
133141
if ":" in api_mtls_endpoint
@@ -158,6 +166,23 @@ def __init__(
158166
scopes=scopes or self.AUTH_SCOPES,
159167
quota_project_id=quota_project_id,
160168
)
169+
else:
170+
host = host if ":" in host else host + ":443"
171+
172+
if credentials is None:
173+
credentials, _ = auth.default(
174+
scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id
175+
)
176+
177+
# create a new channel. The provided one is ignored.
178+
self._grpc_channel = type(self).create_channel(
179+
host,
180+
credentials=credentials,
181+
credentials_file=credentials_file,
182+
ssl_credentials=ssl_channel_credentials,
183+
scopes=scopes or self.AUTH_SCOPES,
184+
quota_project_id=quota_project_id,
185+
)
161186

162187
self._stubs = {} # type: Dict[str, Callable]
163188

@@ -223,13 +248,6 @@ def grpc_channel(self) -> grpc.Channel:
223248
This property caches on the instance; repeated calls return
224249
the same channel.
225250
"""
226-
# Sanity check: Only create a new channel if we do not already
227-
# have one.
228-
if not hasattr(self, "_grpc_channel"):
229-
self._grpc_channel = self.create_channel(
230-
self._host, credentials=self._credentials,
231-
)
232-
233251
# Return the channel from cache.
234252
return self._grpc_channel
235253

0 commit comments

Comments
 (0)