diff --git a/.coveragerc b/.coveragerc index c58aa373..8705cefd 100644 --- a/.coveragerc +++ b/.coveragerc @@ -5,6 +5,7 @@ branch = True show_missing = True omit = google/cloud/automl/__init__.py + google/cloud/automl/gapic_version.py exclude_lines = # Re-enable the standard pragma pragma: NO COVER diff --git a/.github/.OwlBot.lock.yaml b/.github/.OwlBot.lock.yaml index 889f77df..5fc5daa3 100644 --- a/.github/.OwlBot.lock.yaml +++ b/.github/.OwlBot.lock.yaml @@ -13,4 +13,4 @@ # limitations under the License. docker: image: gcr.io/cloud-devrel-public-resources/owlbot-python:latest - digest: sha256:c43f1d918bcf817d337aa29ff833439494a158a0831508fda4ec75dc4c0d0320 + digest: sha256:8555f0e37e6261408f792bfd6635102d2da5ad73f8f09bcb24f25e6afb5fac97 diff --git a/.kokoro/requirements.in b/.kokoro/requirements.in index cbd7e77f..882178ce 100644 --- a/.kokoro/requirements.in +++ b/.kokoro/requirements.in @@ -1,5 +1,5 @@ gcp-docuploader -gcp-releasetool +gcp-releasetool>=1.10.5 # required for compatibility with cryptography>=39.x importlib-metadata typing-extensions twine diff --git a/.kokoro/requirements.txt b/.kokoro/requirements.txt index 05dc4672..fa99c129 100644 --- a/.kokoro/requirements.txt +++ b/.kokoro/requirements.txt @@ -113,33 +113,28 @@ commonmark==0.9.1 \ --hash=sha256:452f9dc859be7f06631ddcb328b6919c67984aca654e5fefb3914d54691aed60 \ --hash=sha256:da2f38c92590f83de410ba1a3cbceafbc74fee9def35f9251ba9a971d6d66fd9 # via rich -cryptography==38.0.3 \ - --hash=sha256:068147f32fa662c81aebab95c74679b401b12b57494872886eb5c1139250ec5d \ - --hash=sha256:06fc3cc7b6f6cca87bd56ec80a580c88f1da5306f505876a71c8cfa7050257dd \ - --hash=sha256:25c1d1f19729fb09d42e06b4bf9895212292cb27bb50229f5aa64d039ab29146 \ - --hash=sha256:402852a0aea73833d982cabb6d0c3bb582c15483d29fb7085ef2c42bfa7e38d7 \ - --hash=sha256:4e269dcd9b102c5a3d72be3c45d8ce20377b8076a43cbed6f660a1afe365e436 \ - --hash=sha256:5419a127426084933076132d317911e3c6eb77568a1ce23c3ac1e12d111e61e0 \ - --hash=sha256:554bec92ee7d1e9d10ded2f7e92a5d70c1f74ba9524947c0ba0c850c7b011828 \ - --hash=sha256:5e89468fbd2fcd733b5899333bc54d0d06c80e04cd23d8c6f3e0542358c6060b \ - --hash=sha256:65535bc550b70bd6271984d9863a37741352b4aad6fb1b3344a54e6950249b55 \ - --hash=sha256:6ab9516b85bebe7aa83f309bacc5f44a61eeb90d0b4ec125d2d003ce41932d36 \ - --hash=sha256:6addc3b6d593cd980989261dc1cce38263c76954d758c3c94de51f1e010c9a50 \ - --hash=sha256:728f2694fa743a996d7784a6194da430f197d5c58e2f4e278612b359f455e4a2 \ - --hash=sha256:785e4056b5a8b28f05a533fab69febf5004458e20dad7e2e13a3120d8ecec75a \ - --hash=sha256:78cf5eefac2b52c10398a42765bfa981ce2372cbc0457e6bf9658f41ec3c41d8 \ - --hash=sha256:7f836217000342d448e1c9a342e9163149e45d5b5eca76a30e84503a5a96cab0 \ - --hash=sha256:8d41a46251bf0634e21fac50ffd643216ccecfaf3701a063257fe0b2be1b6548 \ - --hash=sha256:984fe150f350a3c91e84de405fe49e688aa6092b3525f407a18b9646f6612320 \ - --hash=sha256:9b24bcff7853ed18a63cfb0c2b008936a9554af24af2fb146e16d8e1aed75748 \ - --hash=sha256:b1b35d9d3a65542ed2e9d90115dfd16bbc027b3f07ee3304fc83580f26e43249 \ - --hash=sha256:b1b52c9e5f8aa2b802d48bd693190341fae201ea51c7a167d69fc48b60e8a959 \ - --hash=sha256:bbf203f1a814007ce24bd4d51362991d5cb90ba0c177a9c08825f2cc304d871f \ - --hash=sha256:be243c7e2bfcf6cc4cb350c0d5cdf15ca6383bbcb2a8ef51d3c9411a9d4386f0 \ - --hash=sha256:bfbe6ee19615b07a98b1d2287d6a6073f734735b49ee45b11324d85efc4d5cbd \ - --hash=sha256:c46837ea467ed1efea562bbeb543994c2d1f6e800785bd5a2c98bc096f5cb220 \ - --hash=sha256:dfb4f4dd568de1b6af9f4cda334adf7d72cf5bc052516e1b2608b683375dd95c \ - --hash=sha256:ed7b00096790213e09eb11c97cc6e2b757f15f3d2f85833cd2d3ec3fe37c1722 +cryptography==39.0.1 \ + --hash=sha256:0f8da300b5c8af9f98111ffd512910bc792b4c77392a9523624680f7956a99d4 \ + --hash=sha256:35f7c7d015d474f4011e859e93e789c87d21f6f4880ebdc29896a60403328f1f \ + --hash=sha256:5aa67414fcdfa22cf052e640cb5ddc461924a045cacf325cd164e65312d99502 \ + --hash=sha256:5d2d8b87a490bfcd407ed9d49093793d0f75198a35e6eb1a923ce1ee86c62b41 \ + --hash=sha256:6687ef6d0a6497e2b58e7c5b852b53f62142cfa7cd1555795758934da363a965 \ + --hash=sha256:6f8ba7f0328b79f08bdacc3e4e66fb4d7aab0c3584e0bd41328dce5262e26b2e \ + --hash=sha256:706843b48f9a3f9b9911979761c91541e3d90db1ca905fd63fee540a217698bc \ + --hash=sha256:807ce09d4434881ca3a7594733669bd834f5b2c6d5c7e36f8c00f691887042ad \ + --hash=sha256:83e17b26de248c33f3acffb922748151d71827d6021d98c70e6c1a25ddd78505 \ + --hash=sha256:96f1157a7c08b5b189b16b47bc9db2332269d6680a196341bf30046330d15388 \ + --hash=sha256:aec5a6c9864be7df2240c382740fcf3b96928c46604eaa7f3091f58b878c0bb6 \ + --hash=sha256:b0afd054cd42f3d213bf82c629efb1ee5f22eba35bf0eec88ea9ea7304f511a2 \ + --hash=sha256:ced4e447ae29ca194449a3f1ce132ded8fcab06971ef5f618605aacaa612beac \ + --hash=sha256:d1f6198ee6d9148405e49887803907fe8962a23e6c6f83ea7d98f1c0de375695 \ + --hash=sha256:e124352fd3db36a9d4a21c1aa27fd5d051e621845cb87fb851c08f4f75ce8be6 \ + --hash=sha256:e422abdec8b5fa8462aa016786680720d78bdce7a30c652b7fadf83a4ba35336 \ + --hash=sha256:ef8b72fa70b348724ff1218267e7f7375b8de4e8194d1636ee60510aae104cd0 \ + --hash=sha256:f0c64d1bd842ca2633e74a1a28033d139368ad959872533b1bab8c80e8240a0c \ + --hash=sha256:f24077a3b5298a5a06a8e0536e3ea9ec60e4c7ac486755e5fb6e6ea9b3500106 \ + --hash=sha256:fdd188c8a6ef8769f148f88f859884507b954cc64db6b52f66ef199bb9ad660a \ + --hash=sha256:fe913f20024eb2cb2f323e42a64bdf2911bb9738a15dba7d3cce48151034e3a8 # via # gcp-releasetool # secretstorage @@ -159,9 +154,9 @@ gcp-docuploader==0.6.4 \ --hash=sha256:01486419e24633af78fd0167db74a2763974765ee8078ca6eb6964d0ebd388af \ --hash=sha256:70861190c123d907b3b067da896265ead2eeb9263969d6955c9e0bb091b5ccbf # via -r requirements.in -gcp-releasetool==1.10.0 \ - --hash=sha256:72a38ca91b59c24f7e699e9227c90cbe4dd71b789383cb0164b088abae294c83 \ - --hash=sha256:8c7c99320208383d4bb2b808c6880eb7a81424afe7cdba3c8d84b25f4f0e097d +gcp-releasetool==1.10.5 \ + --hash=sha256:174b7b102d704b254f2a26a3eda2c684fd3543320ec239baf771542a2e58e109 \ + --hash=sha256:e29d29927fe2ca493105a82958c6873bb2b90d503acac56be2c229e74de0eec9 # via -r requirements.in google-api-core==2.10.2 \ --hash=sha256:10c06f7739fe57781f87523375e8e1a3a4674bf6392cd6131a3222182b971320 \ diff --git a/.release-please-manifest.json b/.release-please-manifest.json index 8e7567c0..afe4b0fb 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,4 +1,4 @@ { - ".": "2.10.1" + ".": "2.11.0" } \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 61f99dad..c2ba2a86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ [1]: https://pypi.org/project/google-cloud-automl/#history +## [2.11.0](https://github.com/googleapis/python-automl/compare/v2.10.1...v2.11.0) (2023-02-28) + + +### Features + +* Enable "rest" transport in Python for services supporting numeric enums ([#472](https://github.com/googleapis/python-automl/issues/472)) ([8d76d92](https://github.com/googleapis/python-automl/commit/8d76d922c48430cb0709cc35e9c10b92536f57c4)) + ## [2.10.1](https://github.com/googleapis/python-automl/compare/v2.10.0...v2.10.1) (2023-01-20) diff --git a/google/cloud/automl/gapic_version.py b/google/cloud/automl/gapic_version.py index 3dded207..bb74f811 100644 --- a/google/cloud/automl/gapic_version.py +++ b/google/cloud/automl/gapic_version.py @@ -13,4 +13,4 @@ # See the License for the specific language governing permissions and # limitations under the License. # -__version__ = "2.10.1" # {x-release-please-version} +__version__ = "2.11.0" # {x-release-please-version} diff --git a/google/cloud/automl_v1/__init__.py b/google/cloud/automl_v1/__init__.py index 0dc2c630..653064ef 100644 --- a/google/cloud/automl_v1/__init__.py +++ b/google/cloud/automl_v1/__init__.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # -from google.cloud.automl import gapic_version as package_version +from google.cloud.automl_v1 import gapic_version as package_version __version__ = package_version.__version__ diff --git a/google/cloud/automl_v1/gapic_metadata.json b/google/cloud/automl_v1/gapic_metadata.json index 44cf25ba..7d017052 100644 --- a/google/cloud/automl_v1/gapic_metadata.json +++ b/google/cloud/automl_v1/gapic_metadata.json @@ -196,6 +196,101 @@ ] } } + }, + "rest": { + "libraryClient": "AutoMlClient", + "rpcs": { + "CreateDataset": { + "methods": [ + "create_dataset" + ] + }, + "CreateModel": { + "methods": [ + "create_model" + ] + }, + "DeleteDataset": { + "methods": [ + "delete_dataset" + ] + }, + "DeleteModel": { + "methods": [ + "delete_model" + ] + }, + "DeployModel": { + "methods": [ + "deploy_model" + ] + }, + "ExportData": { + "methods": [ + "export_data" + ] + }, + "ExportModel": { + "methods": [ + "export_model" + ] + }, + "GetAnnotationSpec": { + "methods": [ + "get_annotation_spec" + ] + }, + "GetDataset": { + "methods": [ + "get_dataset" + ] + }, + "GetModel": { + "methods": [ + "get_model" + ] + }, + "GetModelEvaluation": { + "methods": [ + "get_model_evaluation" + ] + }, + "ImportData": { + "methods": [ + "import_data" + ] + }, + "ListDatasets": { + "methods": [ + "list_datasets" + ] + }, + "ListModelEvaluations": { + "methods": [ + "list_model_evaluations" + ] + }, + "ListModels": { + "methods": [ + "list_models" + ] + }, + "UndeployModel": { + "methods": [ + "undeploy_model" + ] + }, + "UpdateDataset": { + "methods": [ + "update_dataset" + ] + }, + "UpdateModel": { + "methods": [ + "update_model" + ] + } + } } } }, @@ -230,6 +325,21 @@ ] } } + }, + "rest": { + "libraryClient": "PredictionServiceClient", + "rpcs": { + "BatchPredict": { + "methods": [ + "batch_predict" + ] + }, + "Predict": { + "methods": [ + "predict" + ] + } + } } } } diff --git a/google/cloud/automl_v1/gapic_version.py b/google/cloud/automl_v1/gapic_version.py index 3dded207..bb74f811 100644 --- a/google/cloud/automl_v1/gapic_version.py +++ b/google/cloud/automl_v1/gapic_version.py @@ -13,4 +13,4 @@ # See the License for the specific language governing permissions and # limitations under the License. # -__version__ = "2.10.1" # {x-release-please-version} +__version__ = "2.11.0" # {x-release-please-version} diff --git a/google/cloud/automl_v1/services/auto_ml/client.py b/google/cloud/automl_v1/services/auto_ml/client.py index cc10ef51..8fbea129 100644 --- a/google/cloud/automl_v1/services/auto_ml/client.py +++ b/google/cloud/automl_v1/services/auto_ml/client.py @@ -71,6 +71,7 @@ from .transports.base import AutoMlTransport, DEFAULT_CLIENT_INFO from .transports.grpc import AutoMlGrpcTransport from .transports.grpc_asyncio import AutoMlGrpcAsyncIOTransport +from .transports.rest import AutoMlRestTransport class AutoMlClientMeta(type): @@ -84,6 +85,7 @@ class AutoMlClientMeta(type): _transport_registry = OrderedDict() # type: Dict[str, Type[AutoMlTransport]] _transport_registry["grpc"] = AutoMlGrpcTransport _transport_registry["grpc_asyncio"] = AutoMlGrpcAsyncIOTransport + _transport_registry["rest"] = AutoMlRestTransport def get_transport_class( cls, diff --git a/google/cloud/automl_v1/services/auto_ml/transports/__init__.py b/google/cloud/automl_v1/services/auto_ml/transports/__init__.py index e3196b27..f47b6bf6 100644 --- a/google/cloud/automl_v1/services/auto_ml/transports/__init__.py +++ b/google/cloud/automl_v1/services/auto_ml/transports/__init__.py @@ -19,15 +19,20 @@ from .base import AutoMlTransport from .grpc import AutoMlGrpcTransport from .grpc_asyncio import AutoMlGrpcAsyncIOTransport +from .rest import AutoMlRestTransport +from .rest import AutoMlRestInterceptor # Compile a registry of transports. _transport_registry = OrderedDict() # type: Dict[str, Type[AutoMlTransport]] _transport_registry["grpc"] = AutoMlGrpcTransport _transport_registry["grpc_asyncio"] = AutoMlGrpcAsyncIOTransport +_transport_registry["rest"] = AutoMlRestTransport __all__ = ( "AutoMlTransport", "AutoMlGrpcTransport", "AutoMlGrpcAsyncIOTransport", + "AutoMlRestTransport", + "AutoMlRestInterceptor", ) diff --git a/google/cloud/automl_v1/services/auto_ml/transports/rest.py b/google/cloud/automl_v1/services/auto_ml/transports/rest.py new file mode 100644 index 00000000..32a0ee5c --- /dev/null +++ b/google/cloud/automl_v1/services/auto_ml/transports/rest.py @@ -0,0 +1,2631 @@ +# -*- coding: utf-8 -*- +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from google.auth.transport.requests import AuthorizedSession # type: ignore +import json # type: ignore +import grpc # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore +from google.auth import credentials as ga_credentials # type: ignore +from google.api_core import exceptions as core_exceptions +from google.api_core import retry as retries +from google.api_core import rest_helpers +from google.api_core import rest_streaming +from google.api_core import path_template +from google.api_core import gapic_v1 + +from google.protobuf import json_format +from google.api_core import operations_v1 +from requests import __version__ as requests_version +import dataclasses +import re +from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Union +import warnings + +try: + OptionalRetry = Union[retries.Retry, gapic_v1.method._MethodDefault] +except AttributeError: # pragma: NO COVER + OptionalRetry = Union[retries.Retry, object] # type: ignore + + +from google.cloud.automl_v1.types import annotation_spec +from google.cloud.automl_v1.types import dataset +from google.cloud.automl_v1.types import dataset as gca_dataset +from google.cloud.automl_v1.types import model +from google.cloud.automl_v1.types import model as gca_model +from google.cloud.automl_v1.types import model_evaluation +from google.cloud.automl_v1.types import service +from google.longrunning import operations_pb2 # type: ignore + +from .base import AutoMlTransport, DEFAULT_CLIENT_INFO as BASE_DEFAULT_CLIENT_INFO + + +DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=BASE_DEFAULT_CLIENT_INFO.gapic_version, + grpc_version=None, + rest_version=requests_version, +) + + +class AutoMlRestInterceptor: + """Interceptor for AutoMl. + + Interceptors are used to manipulate requests, request metadata, and responses + in arbitrary ways. + Example use cases include: + * Logging + * Verifying requests according to service or custom semantics + * Stripping extraneous information from responses + + These use cases and more can be enabled by injecting an + instance of a custom subclass when constructing the AutoMlRestTransport. + + .. code-block:: python + class MyCustomAutoMlInterceptor(AutoMlRestInterceptor): + def pre_create_dataset(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_create_dataset(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_create_model(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_create_model(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_delete_dataset(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_delete_dataset(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_delete_model(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_delete_model(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_deploy_model(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_deploy_model(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_export_data(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_export_data(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_export_model(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_export_model(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_get_annotation_spec(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_annotation_spec(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_get_dataset(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_dataset(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_get_model(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_model(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_get_model_evaluation(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_model_evaluation(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_import_data(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_import_data(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_list_datasets(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_datasets(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_list_model_evaluations(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_model_evaluations(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_list_models(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_models(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_undeploy_model(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_undeploy_model(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_update_dataset(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_update_dataset(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_update_model(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_update_model(self, response): + logging.log(f"Received response: {response}") + return response + + transport = AutoMlRestTransport(interceptor=MyCustomAutoMlInterceptor()) + client = AutoMlClient(transport=transport) + + + """ + + def pre_create_dataset( + self, request: service.CreateDatasetRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.CreateDatasetRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for create_dataset + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_create_dataset( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for create_dataset + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_create_model( + self, request: service.CreateModelRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.CreateModelRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for create_model + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_create_model( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for create_model + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_delete_dataset( + self, request: service.DeleteDatasetRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.DeleteDatasetRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for delete_dataset + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_delete_dataset( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for delete_dataset + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_delete_model( + self, request: service.DeleteModelRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.DeleteModelRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for delete_model + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_delete_model( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for delete_model + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_deploy_model( + self, request: service.DeployModelRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.DeployModelRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for deploy_model + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_deploy_model( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for deploy_model + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_export_data( + self, request: service.ExportDataRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.ExportDataRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for export_data + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_export_data( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for export_data + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_export_model( + self, request: service.ExportModelRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.ExportModelRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for export_model + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_export_model( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for export_model + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_get_annotation_spec( + self, + request: service.GetAnnotationSpecRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[service.GetAnnotationSpecRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_annotation_spec + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_get_annotation_spec( + self, response: annotation_spec.AnnotationSpec + ) -> annotation_spec.AnnotationSpec: + """Post-rpc interceptor for get_annotation_spec + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_get_dataset( + self, request: service.GetDatasetRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.GetDatasetRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_dataset + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_get_dataset(self, response: dataset.Dataset) -> dataset.Dataset: + """Post-rpc interceptor for get_dataset + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_get_model( + self, request: service.GetModelRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.GetModelRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_model + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_get_model(self, response: model.Model) -> model.Model: + """Post-rpc interceptor for get_model + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_get_model_evaluation( + self, + request: service.GetModelEvaluationRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[service.GetModelEvaluationRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_model_evaluation + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_get_model_evaluation( + self, response: model_evaluation.ModelEvaluation + ) -> model_evaluation.ModelEvaluation: + """Post-rpc interceptor for get_model_evaluation + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_import_data( + self, request: service.ImportDataRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.ImportDataRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for import_data + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_import_data( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for import_data + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_list_datasets( + self, request: service.ListDatasetsRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.ListDatasetsRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for list_datasets + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_list_datasets( + self, response: service.ListDatasetsResponse + ) -> service.ListDatasetsResponse: + """Post-rpc interceptor for list_datasets + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_list_model_evaluations( + self, + request: service.ListModelEvaluationsRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[service.ListModelEvaluationsRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for list_model_evaluations + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_list_model_evaluations( + self, response: service.ListModelEvaluationsResponse + ) -> service.ListModelEvaluationsResponse: + """Post-rpc interceptor for list_model_evaluations + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_list_models( + self, request: service.ListModelsRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.ListModelsRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for list_models + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_list_models( + self, response: service.ListModelsResponse + ) -> service.ListModelsResponse: + """Post-rpc interceptor for list_models + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_undeploy_model( + self, request: service.UndeployModelRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.UndeployModelRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for undeploy_model + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_undeploy_model( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for undeploy_model + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_update_dataset( + self, request: service.UpdateDatasetRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.UpdateDatasetRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for update_dataset + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_update_dataset(self, response: gca_dataset.Dataset) -> gca_dataset.Dataset: + """Post-rpc interceptor for update_dataset + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_update_model( + self, request: service.UpdateModelRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.UpdateModelRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for update_model + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_update_model(self, response: gca_model.Model) -> gca_model.Model: + """Post-rpc interceptor for update_model + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + +@dataclasses.dataclass +class AutoMlRestStub: + _session: AuthorizedSession + _host: str + _interceptor: AutoMlRestInterceptor + + +class AutoMlRestTransport(AutoMlTransport): + """REST backend transport for AutoMl. + + AutoML Server API. + + The resource names are assigned by the server. The server never + reuses names that it has created after the resources with those + names are deleted. + + An ID of a resource is the last element of the item's resource name. + For + ``projects/{project_id}/locations/{location_id}/datasets/{dataset_id}``, + then the id for the item is ``{dataset_id}``. + + Currently the only supported ``location_id`` is "us-central1". + + On any input that is documented to expect a string parameter in + snake_case or dash-case, either of those cases is accepted. + + This class defines the same methods as the primary client, so the + primary client can load the underlying transport implementation + and call it. + + It sends JSON representations of protocol buffers over HTTP/1.1 + + """ + + def __init__( + self, + *, + host: str = "automl.googleapis.com", + credentials: Optional[ga_credentials.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + client_cert_source_for_mtls: Optional[Callable[[], Tuple[bytes, bytes]]] = None, + quota_project_id: Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + always_use_jwt_access: Optional[bool] = False, + url_scheme: str = "https", + interceptor: Optional[AutoMlRestInterceptor] = None, + api_audience: Optional[str] = None, + ) -> None: + """Instantiate the transport. + + Args: + host (Optional[str]): + The hostname to connect to. + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is ignored if ``channel`` is provided. + scopes (Optional(Sequence[str])): A list of scopes. This argument is + ignored if ``channel`` is provided. + client_cert_source_for_mtls (Callable[[], Tuple[bytes, bytes]]): Client + certificate to configure mutual TLS HTTP channel. It is ignored + if ``channel`` is provided. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you are developing + your own client library. + always_use_jwt_access (Optional[bool]): Whether self signed JWT should + be used for service account credentials. + url_scheme: the protocol scheme for the API endpoint. Normally + "https", but for testing or local servers, + "http" can be specified. + """ + # Run the base constructor + # TODO(yon-mg): resolve other ctor params i.e. scopes, quota, etc. + # TODO: When custom host (api_endpoint) is set, `scopes` must *also* be set on the + # credentials object + maybe_url_match = re.match("^(?Phttp(?:s)?://)?(?P.*)$", host) + if maybe_url_match is None: + raise ValueError( + f"Unexpected hostname structure: {host}" + ) # pragma: NO COVER + + url_match_items = maybe_url_match.groupdict() + + host = f"{url_scheme}://{host}" if not url_match_items["scheme"] else host + + super().__init__( + host=host, + credentials=credentials, + client_info=client_info, + always_use_jwt_access=always_use_jwt_access, + api_audience=api_audience, + ) + self._session = AuthorizedSession( + self._credentials, default_host=self.DEFAULT_HOST + ) + self._operations_client: Optional[operations_v1.AbstractOperationsClient] = None + if client_cert_source_for_mtls: + self._session.configure_mtls_channel(client_cert_source_for_mtls) + self._interceptor = interceptor or AutoMlRestInterceptor() + self._prep_wrapped_messages(client_info) + + @property + def operations_client(self) -> operations_v1.AbstractOperationsClient: + """Create the client designed to process long-running operations. + + This property caches on the instance; repeated calls return the same + client. + """ + # Only create a new client if we do not already have one. + if self._operations_client is None: + http_options: Dict[str, List[Dict[str, str]]] = { + "google.longrunning.Operations.CancelOperation": [ + { + "method": "post", + "uri": "/v1/{name=projects/*/locations/*/operations/*}:cancel", + "body": "*", + }, + ], + "google.longrunning.Operations.DeleteOperation": [ + { + "method": "delete", + "uri": "/v1/{name=projects/*/locations/*/operations/*}", + }, + ], + "google.longrunning.Operations.GetOperation": [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/operations/*}", + }, + ], + "google.longrunning.Operations.ListOperations": [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*}/operations", + }, + ], + "google.longrunning.Operations.WaitOperation": [ + { + "method": "post", + "uri": "/v1/{name=projects/*/locations/*/operations/*}:wait", + "body": "*", + }, + ], + } + + rest_transport = operations_v1.OperationsRestTransport( + host=self._host, + # use the credentials which are saved + credentials=self._credentials, + scopes=self._scopes, + http_options=http_options, + path_prefix="v1", + ) + + self._operations_client = operations_v1.AbstractOperationsClient( + transport=rest_transport + ) + + # Return the client from cache. + return self._operations_client + + class _CreateDataset(AutoMlRestStub): + def __hash__(self): + return hash("CreateDataset") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.CreateDatasetRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the create dataset method over HTTP. + + Args: + request (~.service.CreateDatasetRequest): + The request object. Request message for + [AutoMl.CreateDataset][google.cloud.automl.v1.AutoMl.CreateDataset]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{parent=projects/*/locations/*}/datasets", + "body": "dataset", + }, + ] + request, metadata = self._interceptor.pre_create_dataset(request, metadata) + pb_request = service.CreateDatasetRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_create_dataset(resp) + return resp + + class _CreateModel(AutoMlRestStub): + def __hash__(self): + return hash("CreateModel") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.CreateModelRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the create model method over HTTP. + + Args: + request (~.service.CreateModelRequest): + The request object. Request message for + [AutoMl.CreateModel][google.cloud.automl.v1.AutoMl.CreateModel]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{parent=projects/*/locations/*}/models", + "body": "model", + }, + ] + request, metadata = self._interceptor.pre_create_model(request, metadata) + pb_request = service.CreateModelRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_create_model(resp) + return resp + + class _DeleteDataset(AutoMlRestStub): + def __hash__(self): + return hash("DeleteDataset") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.DeleteDatasetRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the delete dataset method over HTTP. + + Args: + request (~.service.DeleteDatasetRequest): + The request object. Request message for + [AutoMl.DeleteDataset][google.cloud.automl.v1.AutoMl.DeleteDataset]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "delete", + "uri": "/v1/{name=projects/*/locations/*/datasets/*}", + }, + ] + request, metadata = self._interceptor.pre_delete_dataset(request, metadata) + pb_request = service.DeleteDatasetRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_delete_dataset(resp) + return resp + + class _DeleteModel(AutoMlRestStub): + def __hash__(self): + return hash("DeleteModel") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.DeleteModelRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the delete model method over HTTP. + + Args: + request (~.service.DeleteModelRequest): + The request object. Request message for + [AutoMl.DeleteModel][google.cloud.automl.v1.AutoMl.DeleteModel]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "delete", + "uri": "/v1/{name=projects/*/locations/*/models/*}", + }, + ] + request, metadata = self._interceptor.pre_delete_model(request, metadata) + pb_request = service.DeleteModelRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_delete_model(resp) + return resp + + class _DeployModel(AutoMlRestStub): + def __hash__(self): + return hash("DeployModel") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.DeployModelRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the deploy model method over HTTP. + + Args: + request (~.service.DeployModelRequest): + The request object. Request message for + [AutoMl.DeployModel][google.cloud.automl.v1.AutoMl.DeployModel]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{name=projects/*/locations/*/models/*}:deploy", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_deploy_model(request, metadata) + pb_request = service.DeployModelRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_deploy_model(resp) + return resp + + class _ExportData(AutoMlRestStub): + def __hash__(self): + return hash("ExportData") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.ExportDataRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the export data method over HTTP. + + Args: + request (~.service.ExportDataRequest): + The request object. Request message for + [AutoMl.ExportData][google.cloud.automl.v1.AutoMl.ExportData]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{name=projects/*/locations/*/datasets/*}:exportData", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_export_data(request, metadata) + pb_request = service.ExportDataRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_export_data(resp) + return resp + + class _ExportModel(AutoMlRestStub): + def __hash__(self): + return hash("ExportModel") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.ExportModelRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the export model method over HTTP. + + Args: + request (~.service.ExportModelRequest): + The request object. Request message for + [AutoMl.ExportModel][google.cloud.automl.v1.AutoMl.ExportModel]. + Models need to be enabled for exporting, otherwise an + error code will be returned. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{name=projects/*/locations/*/models/*}:export", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_export_model(request, metadata) + pb_request = service.ExportModelRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_export_model(resp) + return resp + + class _GetAnnotationSpec(AutoMlRestStub): + def __hash__(self): + return hash("GetAnnotationSpec") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.GetAnnotationSpecRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> annotation_spec.AnnotationSpec: + r"""Call the get annotation spec method over HTTP. + + Args: + request (~.service.GetAnnotationSpecRequest): + The request object. Request message for + [AutoMl.GetAnnotationSpec][google.cloud.automl.v1.AutoMl.GetAnnotationSpec]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.annotation_spec.AnnotationSpec: + A definition of an annotation spec. + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/datasets/*/annotationSpecs/*}", + }, + ] + request, metadata = self._interceptor.pre_get_annotation_spec( + request, metadata + ) + pb_request = service.GetAnnotationSpecRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = annotation_spec.AnnotationSpec() + pb_resp = annotation_spec.AnnotationSpec.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_annotation_spec(resp) + return resp + + class _GetDataset(AutoMlRestStub): + def __hash__(self): + return hash("GetDataset") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.GetDatasetRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> dataset.Dataset: + r"""Call the get dataset method over HTTP. + + Args: + request (~.service.GetDatasetRequest): + The request object. Request message for + [AutoMl.GetDataset][google.cloud.automl.v1.AutoMl.GetDataset]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.dataset.Dataset: + A workspace for solving a single, + particular machine learning (ML) + problem. A workspace contains examples + that may be annotated. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/datasets/*}", + }, + ] + request, metadata = self._interceptor.pre_get_dataset(request, metadata) + pb_request = service.GetDatasetRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = dataset.Dataset() + pb_resp = dataset.Dataset.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_dataset(resp) + return resp + + class _GetModel(AutoMlRestStub): + def __hash__(self): + return hash("GetModel") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.GetModelRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> model.Model: + r"""Call the get model method over HTTP. + + Args: + request (~.service.GetModelRequest): + The request object. Request message for + [AutoMl.GetModel][google.cloud.automl.v1.AutoMl.GetModel]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.model.Model: + API proto representing a trained + machine learning model. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/models/*}", + }, + ] + request, metadata = self._interceptor.pre_get_model(request, metadata) + pb_request = service.GetModelRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = model.Model() + pb_resp = model.Model.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_model(resp) + return resp + + class _GetModelEvaluation(AutoMlRestStub): + def __hash__(self): + return hash("GetModelEvaluation") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.GetModelEvaluationRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> model_evaluation.ModelEvaluation: + r"""Call the get model evaluation method over HTTP. + + Args: + request (~.service.GetModelEvaluationRequest): + The request object. Request message for + [AutoMl.GetModelEvaluation][google.cloud.automl.v1.AutoMl.GetModelEvaluation]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.model_evaluation.ModelEvaluation: + Evaluation results of a model. + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/models/*/modelEvaluations/*}", + }, + ] + request, metadata = self._interceptor.pre_get_model_evaluation( + request, metadata + ) + pb_request = service.GetModelEvaluationRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = model_evaluation.ModelEvaluation() + pb_resp = model_evaluation.ModelEvaluation.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_model_evaluation(resp) + return resp + + class _ImportData(AutoMlRestStub): + def __hash__(self): + return hash("ImportData") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.ImportDataRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the import data method over HTTP. + + Args: + request (~.service.ImportDataRequest): + The request object. Request message for + [AutoMl.ImportData][google.cloud.automl.v1.AutoMl.ImportData]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{name=projects/*/locations/*/datasets/*}:importData", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_import_data(request, metadata) + pb_request = service.ImportDataRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_import_data(resp) + return resp + + class _ListDatasets(AutoMlRestStub): + def __hash__(self): + return hash("ListDatasets") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.ListDatasetsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> service.ListDatasetsResponse: + r"""Call the list datasets method over HTTP. + + Args: + request (~.service.ListDatasetsRequest): + The request object. Request message for + [AutoMl.ListDatasets][google.cloud.automl.v1.AutoMl.ListDatasets]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.service.ListDatasetsResponse: + Response message for + [AutoMl.ListDatasets][google.cloud.automl.v1.AutoMl.ListDatasets]. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{parent=projects/*/locations/*}/datasets", + }, + ] + request, metadata = self._interceptor.pre_list_datasets(request, metadata) + pb_request = service.ListDatasetsRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = service.ListDatasetsResponse() + pb_resp = service.ListDatasetsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_datasets(resp) + return resp + + class _ListModelEvaluations(AutoMlRestStub): + def __hash__(self): + return hash("ListModelEvaluations") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = { + "filter": "", + } + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.ListModelEvaluationsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> service.ListModelEvaluationsResponse: + r"""Call the list model evaluations method over HTTP. + + Args: + request (~.service.ListModelEvaluationsRequest): + The request object. Request message for + [AutoMl.ListModelEvaluations][google.cloud.automl.v1.AutoMl.ListModelEvaluations]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.service.ListModelEvaluationsResponse: + Response message for + [AutoMl.ListModelEvaluations][google.cloud.automl.v1.AutoMl.ListModelEvaluations]. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{parent=projects/*/locations/*/models/*}/modelEvaluations", + }, + ] + request, metadata = self._interceptor.pre_list_model_evaluations( + request, metadata + ) + pb_request = service.ListModelEvaluationsRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = service.ListModelEvaluationsResponse() + pb_resp = service.ListModelEvaluationsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_model_evaluations(resp) + return resp + + class _ListModels(AutoMlRestStub): + def __hash__(self): + return hash("ListModels") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.ListModelsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> service.ListModelsResponse: + r"""Call the list models method over HTTP. + + Args: + request (~.service.ListModelsRequest): + The request object. Request message for + [AutoMl.ListModels][google.cloud.automl.v1.AutoMl.ListModels]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.service.ListModelsResponse: + Response message for + [AutoMl.ListModels][google.cloud.automl.v1.AutoMl.ListModels]. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{parent=projects/*/locations/*}/models", + }, + ] + request, metadata = self._interceptor.pre_list_models(request, metadata) + pb_request = service.ListModelsRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = service.ListModelsResponse() + pb_resp = service.ListModelsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_models(resp) + return resp + + class _UndeployModel(AutoMlRestStub): + def __hash__(self): + return hash("UndeployModel") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.UndeployModelRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the undeploy model method over HTTP. + + Args: + request (~.service.UndeployModelRequest): + The request object. Request message for + [AutoMl.UndeployModel][google.cloud.automl.v1.AutoMl.UndeployModel]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{name=projects/*/locations/*/models/*}:undeploy", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_undeploy_model(request, metadata) + pb_request = service.UndeployModelRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_undeploy_model(resp) + return resp + + class _UpdateDataset(AutoMlRestStub): + def __hash__(self): + return hash("UpdateDataset") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = { + "updateMask": {}, + } + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.UpdateDatasetRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> gca_dataset.Dataset: + r"""Call the update dataset method over HTTP. + + Args: + request (~.service.UpdateDatasetRequest): + The request object. Request message for + [AutoMl.UpdateDataset][google.cloud.automl.v1.AutoMl.UpdateDataset] + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.gca_dataset.Dataset: + A workspace for solving a single, + particular machine learning (ML) + problem. A workspace contains examples + that may be annotated. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "patch", + "uri": "/v1/{dataset.name=projects/*/locations/*/datasets/*}", + "body": "dataset", + }, + ] + request, metadata = self._interceptor.pre_update_dataset(request, metadata) + pb_request = service.UpdateDatasetRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = gca_dataset.Dataset() + pb_resp = gca_dataset.Dataset.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_update_dataset(resp) + return resp + + class _UpdateModel(AutoMlRestStub): + def __hash__(self): + return hash("UpdateModel") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = { + "updateMask": {}, + } + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.UpdateModelRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> gca_model.Model: + r"""Call the update model method over HTTP. + + Args: + request (~.service.UpdateModelRequest): + The request object. Request message for + [AutoMl.UpdateModel][google.cloud.automl.v1.AutoMl.UpdateModel] + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.gca_model.Model: + API proto representing a trained + machine learning model. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "patch", + "uri": "/v1/{model.name=projects/*/locations/*/models/*}", + "body": "model", + }, + ] + request, metadata = self._interceptor.pre_update_model(request, metadata) + pb_request = service.UpdateModelRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = gca_model.Model() + pb_resp = gca_model.Model.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_update_model(resp) + return resp + + @property + def create_dataset( + self, + ) -> Callable[[service.CreateDatasetRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._CreateDataset(self._session, self._host, self._interceptor) # type: ignore + + @property + def create_model( + self, + ) -> Callable[[service.CreateModelRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._CreateModel(self._session, self._host, self._interceptor) # type: ignore + + @property + def delete_dataset( + self, + ) -> Callable[[service.DeleteDatasetRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._DeleteDataset(self._session, self._host, self._interceptor) # type: ignore + + @property + def delete_model( + self, + ) -> Callable[[service.DeleteModelRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._DeleteModel(self._session, self._host, self._interceptor) # type: ignore + + @property + def deploy_model( + self, + ) -> Callable[[service.DeployModelRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._DeployModel(self._session, self._host, self._interceptor) # type: ignore + + @property + def export_data( + self, + ) -> Callable[[service.ExportDataRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ExportData(self._session, self._host, self._interceptor) # type: ignore + + @property + def export_model( + self, + ) -> Callable[[service.ExportModelRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ExportModel(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_annotation_spec( + self, + ) -> Callable[[service.GetAnnotationSpecRequest], annotation_spec.AnnotationSpec]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._GetAnnotationSpec(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_dataset(self) -> Callable[[service.GetDatasetRequest], dataset.Dataset]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._GetDataset(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_model(self) -> Callable[[service.GetModelRequest], model.Model]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._GetModel(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_model_evaluation( + self, + ) -> Callable[ + [service.GetModelEvaluationRequest], model_evaluation.ModelEvaluation + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._GetModelEvaluation(self._session, self._host, self._interceptor) # type: ignore + + @property + def import_data( + self, + ) -> Callable[[service.ImportDataRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ImportData(self._session, self._host, self._interceptor) # type: ignore + + @property + def list_datasets( + self, + ) -> Callable[[service.ListDatasetsRequest], service.ListDatasetsResponse]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ListDatasets(self._session, self._host, self._interceptor) # type: ignore + + @property + def list_model_evaluations( + self, + ) -> Callable[ + [service.ListModelEvaluationsRequest], service.ListModelEvaluationsResponse + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ListModelEvaluations(self._session, self._host, self._interceptor) # type: ignore + + @property + def list_models( + self, + ) -> Callable[[service.ListModelsRequest], service.ListModelsResponse]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ListModels(self._session, self._host, self._interceptor) # type: ignore + + @property + def undeploy_model( + self, + ) -> Callable[[service.UndeployModelRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._UndeployModel(self._session, self._host, self._interceptor) # type: ignore + + @property + def update_dataset( + self, + ) -> Callable[[service.UpdateDatasetRequest], gca_dataset.Dataset]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._UpdateDataset(self._session, self._host, self._interceptor) # type: ignore + + @property + def update_model(self) -> Callable[[service.UpdateModelRequest], gca_model.Model]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._UpdateModel(self._session, self._host, self._interceptor) # type: ignore + + @property + def kind(self) -> str: + return "rest" + + def close(self): + self._session.close() + + +__all__ = ("AutoMlRestTransport",) diff --git a/google/cloud/automl_v1/services/prediction_service/client.py b/google/cloud/automl_v1/services/prediction_service/client.py index 5239062e..40dc27ae 100644 --- a/google/cloud/automl_v1/services/prediction_service/client.py +++ b/google/cloud/automl_v1/services/prediction_service/client.py @@ -56,6 +56,7 @@ from .transports.base import PredictionServiceTransport, DEFAULT_CLIENT_INFO from .transports.grpc import PredictionServiceGrpcTransport from .transports.grpc_asyncio import PredictionServiceGrpcAsyncIOTransport +from .transports.rest import PredictionServiceRestTransport class PredictionServiceClientMeta(type): @@ -71,6 +72,7 @@ class PredictionServiceClientMeta(type): ) # type: Dict[str, Type[PredictionServiceTransport]] _transport_registry["grpc"] = PredictionServiceGrpcTransport _transport_registry["grpc_asyncio"] = PredictionServiceGrpcAsyncIOTransport + _transport_registry["rest"] = PredictionServiceRestTransport def get_transport_class( cls, diff --git a/google/cloud/automl_v1/services/prediction_service/transports/__init__.py b/google/cloud/automl_v1/services/prediction_service/transports/__init__.py index 3b81984d..c3ca80df 100644 --- a/google/cloud/automl_v1/services/prediction_service/transports/__init__.py +++ b/google/cloud/automl_v1/services/prediction_service/transports/__init__.py @@ -19,15 +19,20 @@ from .base import PredictionServiceTransport from .grpc import PredictionServiceGrpcTransport from .grpc_asyncio import PredictionServiceGrpcAsyncIOTransport +from .rest import PredictionServiceRestTransport +from .rest import PredictionServiceRestInterceptor # Compile a registry of transports. _transport_registry = OrderedDict() # type: Dict[str, Type[PredictionServiceTransport]] _transport_registry["grpc"] = PredictionServiceGrpcTransport _transport_registry["grpc_asyncio"] = PredictionServiceGrpcAsyncIOTransport +_transport_registry["rest"] = PredictionServiceRestTransport __all__ = ( "PredictionServiceTransport", "PredictionServiceGrpcTransport", "PredictionServiceGrpcAsyncIOTransport", + "PredictionServiceRestTransport", + "PredictionServiceRestInterceptor", ) diff --git a/google/cloud/automl_v1/services/prediction_service/transports/rest.py b/google/cloud/automl_v1/services/prediction_service/transports/rest.py new file mode 100644 index 00000000..63ea5d91 --- /dev/null +++ b/google/cloud/automl_v1/services/prediction_service/transports/rest.py @@ -0,0 +1,526 @@ +# -*- coding: utf-8 -*- +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from google.auth.transport.requests import AuthorizedSession # type: ignore +import json # type: ignore +import grpc # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore +from google.auth import credentials as ga_credentials # type: ignore +from google.api_core import exceptions as core_exceptions +from google.api_core import retry as retries +from google.api_core import rest_helpers +from google.api_core import rest_streaming +from google.api_core import path_template +from google.api_core import gapic_v1 + +from google.protobuf import json_format +from google.api_core import operations_v1 +from requests import __version__ as requests_version +import dataclasses +import re +from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Union +import warnings + +try: + OptionalRetry = Union[retries.Retry, gapic_v1.method._MethodDefault] +except AttributeError: # pragma: NO COVER + OptionalRetry = Union[retries.Retry, object] # type: ignore + + +from google.cloud.automl_v1.types import prediction_service +from google.longrunning import operations_pb2 # type: ignore + +from .base import ( + PredictionServiceTransport, + DEFAULT_CLIENT_INFO as BASE_DEFAULT_CLIENT_INFO, +) + + +DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=BASE_DEFAULT_CLIENT_INFO.gapic_version, + grpc_version=None, + rest_version=requests_version, +) + + +class PredictionServiceRestInterceptor: + """Interceptor for PredictionService. + + Interceptors are used to manipulate requests, request metadata, and responses + in arbitrary ways. + Example use cases include: + * Logging + * Verifying requests according to service or custom semantics + * Stripping extraneous information from responses + + These use cases and more can be enabled by injecting an + instance of a custom subclass when constructing the PredictionServiceRestTransport. + + .. code-block:: python + class MyCustomPredictionServiceInterceptor(PredictionServiceRestInterceptor): + def pre_batch_predict(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_batch_predict(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_predict(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_predict(self, response): + logging.log(f"Received response: {response}") + return response + + transport = PredictionServiceRestTransport(interceptor=MyCustomPredictionServiceInterceptor()) + client = PredictionServiceClient(transport=transport) + + + """ + + def pre_batch_predict( + self, + request: prediction_service.BatchPredictRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[prediction_service.BatchPredictRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for batch_predict + + Override in a subclass to manipulate the request or metadata + before they are sent to the PredictionService server. + """ + return request, metadata + + def post_batch_predict( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for batch_predict + + Override in a subclass to manipulate the response + after it is returned by the PredictionService server but before + it is returned to user code. + """ + return response + + def pre_predict( + self, + request: prediction_service.PredictRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[prediction_service.PredictRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for predict + + Override in a subclass to manipulate the request or metadata + before they are sent to the PredictionService server. + """ + return request, metadata + + def post_predict( + self, response: prediction_service.PredictResponse + ) -> prediction_service.PredictResponse: + """Post-rpc interceptor for predict + + Override in a subclass to manipulate the response + after it is returned by the PredictionService server but before + it is returned to user code. + """ + return response + + +@dataclasses.dataclass +class PredictionServiceRestStub: + _session: AuthorizedSession + _host: str + _interceptor: PredictionServiceRestInterceptor + + +class PredictionServiceRestTransport(PredictionServiceTransport): + """REST backend transport for PredictionService. + + AutoML Prediction API. + + On any input that is documented to expect a string parameter in + snake_case or dash-case, either of those cases is accepted. + + This class defines the same methods as the primary client, so the + primary client can load the underlying transport implementation + and call it. + + It sends JSON representations of protocol buffers over HTTP/1.1 + + """ + + def __init__( + self, + *, + host: str = "automl.googleapis.com", + credentials: Optional[ga_credentials.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + client_cert_source_for_mtls: Optional[Callable[[], Tuple[bytes, bytes]]] = None, + quota_project_id: Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + always_use_jwt_access: Optional[bool] = False, + url_scheme: str = "https", + interceptor: Optional[PredictionServiceRestInterceptor] = None, + api_audience: Optional[str] = None, + ) -> None: + """Instantiate the transport. + + Args: + host (Optional[str]): + The hostname to connect to. + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is ignored if ``channel`` is provided. + scopes (Optional(Sequence[str])): A list of scopes. This argument is + ignored if ``channel`` is provided. + client_cert_source_for_mtls (Callable[[], Tuple[bytes, bytes]]): Client + certificate to configure mutual TLS HTTP channel. It is ignored + if ``channel`` is provided. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you are developing + your own client library. + always_use_jwt_access (Optional[bool]): Whether self signed JWT should + be used for service account credentials. + url_scheme: the protocol scheme for the API endpoint. Normally + "https", but for testing or local servers, + "http" can be specified. + """ + # Run the base constructor + # TODO(yon-mg): resolve other ctor params i.e. scopes, quota, etc. + # TODO: When custom host (api_endpoint) is set, `scopes` must *also* be set on the + # credentials object + maybe_url_match = re.match("^(?Phttp(?:s)?://)?(?P.*)$", host) + if maybe_url_match is None: + raise ValueError( + f"Unexpected hostname structure: {host}" + ) # pragma: NO COVER + + url_match_items = maybe_url_match.groupdict() + + host = f"{url_scheme}://{host}" if not url_match_items["scheme"] else host + + super().__init__( + host=host, + credentials=credentials, + client_info=client_info, + always_use_jwt_access=always_use_jwt_access, + api_audience=api_audience, + ) + self._session = AuthorizedSession( + self._credentials, default_host=self.DEFAULT_HOST + ) + self._operations_client: Optional[operations_v1.AbstractOperationsClient] = None + if client_cert_source_for_mtls: + self._session.configure_mtls_channel(client_cert_source_for_mtls) + self._interceptor = interceptor or PredictionServiceRestInterceptor() + self._prep_wrapped_messages(client_info) + + @property + def operations_client(self) -> operations_v1.AbstractOperationsClient: + """Create the client designed to process long-running operations. + + This property caches on the instance; repeated calls return the same + client. + """ + # Only create a new client if we do not already have one. + if self._operations_client is None: + http_options: Dict[str, List[Dict[str, str]]] = { + "google.longrunning.Operations.CancelOperation": [ + { + "method": "post", + "uri": "/v1/{name=projects/*/locations/*/operations/*}:cancel", + "body": "*", + }, + ], + "google.longrunning.Operations.DeleteOperation": [ + { + "method": "delete", + "uri": "/v1/{name=projects/*/locations/*/operations/*}", + }, + ], + "google.longrunning.Operations.GetOperation": [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/operations/*}", + }, + ], + "google.longrunning.Operations.ListOperations": [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*}/operations", + }, + ], + "google.longrunning.Operations.WaitOperation": [ + { + "method": "post", + "uri": "/v1/{name=projects/*/locations/*/operations/*}:wait", + "body": "*", + }, + ], + } + + rest_transport = operations_v1.OperationsRestTransport( + host=self._host, + # use the credentials which are saved + credentials=self._credentials, + scopes=self._scopes, + http_options=http_options, + path_prefix="v1", + ) + + self._operations_client = operations_v1.AbstractOperationsClient( + transport=rest_transport + ) + + # Return the client from cache. + return self._operations_client + + class _BatchPredict(PredictionServiceRestStub): + def __hash__(self): + return hash("BatchPredict") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: prediction_service.BatchPredictRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the batch predict method over HTTP. + + Args: + request (~.prediction_service.BatchPredictRequest): + The request object. Request message for + [PredictionService.BatchPredict][google.cloud.automl.v1.PredictionService.BatchPredict]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{name=projects/*/locations/*/models/*}:batchPredict", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_batch_predict(request, metadata) + pb_request = prediction_service.BatchPredictRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_batch_predict(resp) + return resp + + class _Predict(PredictionServiceRestStub): + def __hash__(self): + return hash("Predict") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: prediction_service.PredictRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> prediction_service.PredictResponse: + r"""Call the predict method over HTTP. + + Args: + request (~.prediction_service.PredictRequest): + The request object. Request message for + [PredictionService.Predict][google.cloud.automl.v1.PredictionService.Predict]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.prediction_service.PredictResponse: + Response message for + [PredictionService.Predict][google.cloud.automl.v1.PredictionService.Predict]. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{name=projects/*/locations/*/models/*}:predict", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_predict(request, metadata) + pb_request = prediction_service.PredictRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = prediction_service.PredictResponse() + pb_resp = prediction_service.PredictResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_predict(resp) + return resp + + @property + def batch_predict( + self, + ) -> Callable[[prediction_service.BatchPredictRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._BatchPredict(self._session, self._host, self._interceptor) # type: ignore + + @property + def predict( + self, + ) -> Callable[ + [prediction_service.PredictRequest], prediction_service.PredictResponse + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._Predict(self._session, self._host, self._interceptor) # type: ignore + + @property + def kind(self) -> str: + return "rest" + + def close(self): + self._session.close() + + +__all__ = ("PredictionServiceRestTransport",) diff --git a/google/cloud/automl_v1/types/annotation_payload.py b/google/cloud/automl_v1/types/annotation_payload.py index 326d38d2..6196b342 100644 --- a/google/cloud/automl_v1/types/annotation_payload.py +++ b/google/cloud/automl_v1/types/annotation_payload.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1/types/annotation_spec.py b/google/cloud/automl_v1/types/annotation_spec.py index 21ade566..cc2b0793 100644 --- a/google/cloud/automl_v1/types/annotation_spec.py +++ b/google/cloud/automl_v1/types/annotation_spec.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1/types/classification.py b/google/cloud/automl_v1/types/classification.py index f334310a..ce0d865e 100644 --- a/google/cloud/automl_v1/types/classification.py +++ b/google/cloud/automl_v1/types/classification.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1/types/data_items.py b/google/cloud/automl_v1/types/data_items.py index 4af76f79..38a9154e 100644 --- a/google/cloud/automl_v1/types/data_items.py +++ b/google/cloud/automl_v1/types/data_items.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1/types/dataset.py b/google/cloud/automl_v1/types/dataset.py index 34b7c8bf..cf3363b4 100644 --- a/google/cloud/automl_v1/types/dataset.py +++ b/google/cloud/automl_v1/types/dataset.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1/types/detection.py b/google/cloud/automl_v1/types/detection.py index 90736c29..c481cf49 100644 --- a/google/cloud/automl_v1/types/detection.py +++ b/google/cloud/automl_v1/types/detection.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1/types/geometry.py b/google/cloud/automl_v1/types/geometry.py index fe46e374..b87ea5c9 100644 --- a/google/cloud/automl_v1/types/geometry.py +++ b/google/cloud/automl_v1/types/geometry.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1/types/image.py b/google/cloud/automl_v1/types/image.py index cbdd9f40..5cf51652 100644 --- a/google/cloud/automl_v1/types/image.py +++ b/google/cloud/automl_v1/types/image.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1/types/io.py b/google/cloud/automl_v1/types/io.py index 13164f48..c160bc27 100644 --- a/google/cloud/automl_v1/types/io.py +++ b/google/cloud/automl_v1/types/io.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1/types/model.py b/google/cloud/automl_v1/types/model.py index 41f25a25..906440c8 100644 --- a/google/cloud/automl_v1/types/model.py +++ b/google/cloud/automl_v1/types/model.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1/types/model_evaluation.py b/google/cloud/automl_v1/types/model_evaluation.py index 16848344..8c424c3e 100644 --- a/google/cloud/automl_v1/types/model_evaluation.py +++ b/google/cloud/automl_v1/types/model_evaluation.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1/types/operations.py b/google/cloud/automl_v1/types/operations.py index 319a9f58..7dc327a2 100644 --- a/google/cloud/automl_v1/types/operations.py +++ b/google/cloud/automl_v1/types/operations.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1/types/prediction_service.py b/google/cloud/automl_v1/types/prediction_service.py index 8f03a8b2..acbfebb7 100644 --- a/google/cloud/automl_v1/types/prediction_service.py +++ b/google/cloud/automl_v1/types/prediction_service.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1/types/service.py b/google/cloud/automl_v1/types/service.py index e77586f3..7cb9d965 100644 --- a/google/cloud/automl_v1/types/service.py +++ b/google/cloud/automl_v1/types/service.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1/types/text.py b/google/cloud/automl_v1/types/text.py index 5fd7c350..d449e1d4 100644 --- a/google/cloud/automl_v1/types/text.py +++ b/google/cloud/automl_v1/types/text.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1/types/text_extraction.py b/google/cloud/automl_v1/types/text_extraction.py index f4c278cb..821f1989 100644 --- a/google/cloud/automl_v1/types/text_extraction.py +++ b/google/cloud/automl_v1/types/text_extraction.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1/types/text_segment.py b/google/cloud/automl_v1/types/text_segment.py index e90140a4..201ba20e 100644 --- a/google/cloud/automl_v1/types/text_segment.py +++ b/google/cloud/automl_v1/types/text_segment.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1/types/text_sentiment.py b/google/cloud/automl_v1/types/text_sentiment.py index 49f7c474..f3b2fd97 100644 --- a/google/cloud/automl_v1/types/text_sentiment.py +++ b/google/cloud/automl_v1/types/text_sentiment.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1/types/translation.py b/google/cloud/automl_v1/types/translation.py index b9334130..631b3c70 100644 --- a/google/cloud/automl_v1/types/translation.py +++ b/google/cloud/automl_v1/types/translation.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/__init__.py b/google/cloud/automl_v1beta1/__init__.py index bd642d12..7a34cf3f 100644 --- a/google/cloud/automl_v1beta1/__init__.py +++ b/google/cloud/automl_v1beta1/__init__.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # -from google.cloud.automl import gapic_version as package_version +from google.cloud.automl_v1beta1 import gapic_version as package_version __version__ = package_version.__version__ diff --git a/google/cloud/automl_v1beta1/gapic_metadata.json b/google/cloud/automl_v1beta1/gapic_metadata.json index 11f3faaa..74e85289 100644 --- a/google/cloud/automl_v1beta1/gapic_metadata.json +++ b/google/cloud/automl_v1beta1/gapic_metadata.json @@ -256,6 +256,131 @@ ] } } + }, + "rest": { + "libraryClient": "AutoMlClient", + "rpcs": { + "CreateDataset": { + "methods": [ + "create_dataset" + ] + }, + "CreateModel": { + "methods": [ + "create_model" + ] + }, + "DeleteDataset": { + "methods": [ + "delete_dataset" + ] + }, + "DeleteModel": { + "methods": [ + "delete_model" + ] + }, + "DeployModel": { + "methods": [ + "deploy_model" + ] + }, + "ExportData": { + "methods": [ + "export_data" + ] + }, + "ExportEvaluatedExamples": { + "methods": [ + "export_evaluated_examples" + ] + }, + "ExportModel": { + "methods": [ + "export_model" + ] + }, + "GetAnnotationSpec": { + "methods": [ + "get_annotation_spec" + ] + }, + "GetColumnSpec": { + "methods": [ + "get_column_spec" + ] + }, + "GetDataset": { + "methods": [ + "get_dataset" + ] + }, + "GetModel": { + "methods": [ + "get_model" + ] + }, + "GetModelEvaluation": { + "methods": [ + "get_model_evaluation" + ] + }, + "GetTableSpec": { + "methods": [ + "get_table_spec" + ] + }, + "ImportData": { + "methods": [ + "import_data" + ] + }, + "ListColumnSpecs": { + "methods": [ + "list_column_specs" + ] + }, + "ListDatasets": { + "methods": [ + "list_datasets" + ] + }, + "ListModelEvaluations": { + "methods": [ + "list_model_evaluations" + ] + }, + "ListModels": { + "methods": [ + "list_models" + ] + }, + "ListTableSpecs": { + "methods": [ + "list_table_specs" + ] + }, + "UndeployModel": { + "methods": [ + "undeploy_model" + ] + }, + "UpdateColumnSpec": { + "methods": [ + "update_column_spec" + ] + }, + "UpdateDataset": { + "methods": [ + "update_dataset" + ] + }, + "UpdateTableSpec": { + "methods": [ + "update_table_spec" + ] + } + } } } }, @@ -290,6 +415,21 @@ ] } } + }, + "rest": { + "libraryClient": "PredictionServiceClient", + "rpcs": { + "BatchPredict": { + "methods": [ + "batch_predict" + ] + }, + "Predict": { + "methods": [ + "predict" + ] + } + } } } } diff --git a/google/cloud/automl_v1beta1/gapic_version.py b/google/cloud/automl_v1beta1/gapic_version.py index 3dded207..bb74f811 100644 --- a/google/cloud/automl_v1beta1/gapic_version.py +++ b/google/cloud/automl_v1beta1/gapic_version.py @@ -13,4 +13,4 @@ # See the License for the specific language governing permissions and # limitations under the License. # -__version__ = "2.10.1" # {x-release-please-version} +__version__ = "2.11.0" # {x-release-please-version} diff --git a/google/cloud/automl_v1beta1/services/auto_ml/client.py b/google/cloud/automl_v1beta1/services/auto_ml/client.py index d5fd7af8..0c18399a 100644 --- a/google/cloud/automl_v1beta1/services/auto_ml/client.py +++ b/google/cloud/automl_v1beta1/services/auto_ml/client.py @@ -79,6 +79,7 @@ from .transports.base import AutoMlTransport, DEFAULT_CLIENT_INFO from .transports.grpc import AutoMlGrpcTransport from .transports.grpc_asyncio import AutoMlGrpcAsyncIOTransport +from .transports.rest import AutoMlRestTransport class AutoMlClientMeta(type): @@ -92,6 +93,7 @@ class AutoMlClientMeta(type): _transport_registry = OrderedDict() # type: Dict[str, Type[AutoMlTransport]] _transport_registry["grpc"] = AutoMlGrpcTransport _transport_registry["grpc_asyncio"] = AutoMlGrpcAsyncIOTransport + _transport_registry["rest"] = AutoMlRestTransport def get_transport_class( cls, diff --git a/google/cloud/automl_v1beta1/services/auto_ml/transports/__init__.py b/google/cloud/automl_v1beta1/services/auto_ml/transports/__init__.py index e3196b27..f47b6bf6 100644 --- a/google/cloud/automl_v1beta1/services/auto_ml/transports/__init__.py +++ b/google/cloud/automl_v1beta1/services/auto_ml/transports/__init__.py @@ -19,15 +19,20 @@ from .base import AutoMlTransport from .grpc import AutoMlGrpcTransport from .grpc_asyncio import AutoMlGrpcAsyncIOTransport +from .rest import AutoMlRestTransport +from .rest import AutoMlRestInterceptor # Compile a registry of transports. _transport_registry = OrderedDict() # type: Dict[str, Type[AutoMlTransport]] _transport_registry["grpc"] = AutoMlGrpcTransport _transport_registry["grpc_asyncio"] = AutoMlGrpcAsyncIOTransport +_transport_registry["rest"] = AutoMlRestTransport __all__ = ( "AutoMlTransport", "AutoMlGrpcTransport", "AutoMlGrpcAsyncIOTransport", + "AutoMlRestTransport", + "AutoMlRestInterceptor", ) diff --git a/google/cloud/automl_v1beta1/services/auto_ml/transports/rest.py b/google/cloud/automl_v1beta1/services/auto_ml/transports/rest.py new file mode 100644 index 00000000..6d7de69a --- /dev/null +++ b/google/cloud/automl_v1beta1/services/auto_ml/transports/rest.py @@ -0,0 +1,3456 @@ +# -*- coding: utf-8 -*- +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from google.auth.transport.requests import AuthorizedSession # type: ignore +import json # type: ignore +import grpc # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore +from google.auth import credentials as ga_credentials # type: ignore +from google.api_core import exceptions as core_exceptions +from google.api_core import retry as retries +from google.api_core import rest_helpers +from google.api_core import rest_streaming +from google.api_core import path_template +from google.api_core import gapic_v1 + +from google.protobuf import json_format +from google.api_core import operations_v1 +from requests import __version__ as requests_version +import dataclasses +import re +from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Union +import warnings + +try: + OptionalRetry = Union[retries.Retry, gapic_v1.method._MethodDefault] +except AttributeError: # pragma: NO COVER + OptionalRetry = Union[retries.Retry, object] # type: ignore + + +from google.cloud.automl_v1beta1.types import annotation_spec +from google.cloud.automl_v1beta1.types import column_spec +from google.cloud.automl_v1beta1.types import column_spec as gca_column_spec +from google.cloud.automl_v1beta1.types import dataset +from google.cloud.automl_v1beta1.types import dataset as gca_dataset +from google.cloud.automl_v1beta1.types import model +from google.cloud.automl_v1beta1.types import model_evaluation +from google.cloud.automl_v1beta1.types import service +from google.cloud.automl_v1beta1.types import table_spec +from google.cloud.automl_v1beta1.types import table_spec as gca_table_spec +from google.longrunning import operations_pb2 # type: ignore + +from .base import AutoMlTransport, DEFAULT_CLIENT_INFO as BASE_DEFAULT_CLIENT_INFO + + +DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=BASE_DEFAULT_CLIENT_INFO.gapic_version, + grpc_version=None, + rest_version=requests_version, +) + + +class AutoMlRestInterceptor: + """Interceptor for AutoMl. + + Interceptors are used to manipulate requests, request metadata, and responses + in arbitrary ways. + Example use cases include: + * Logging + * Verifying requests according to service or custom semantics + * Stripping extraneous information from responses + + These use cases and more can be enabled by injecting an + instance of a custom subclass when constructing the AutoMlRestTransport. + + .. code-block:: python + class MyCustomAutoMlInterceptor(AutoMlRestInterceptor): + def pre_create_dataset(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_create_dataset(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_create_model(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_create_model(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_delete_dataset(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_delete_dataset(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_delete_model(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_delete_model(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_deploy_model(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_deploy_model(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_export_data(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_export_data(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_export_evaluated_examples(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_export_evaluated_examples(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_export_model(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_export_model(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_get_annotation_spec(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_annotation_spec(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_get_column_spec(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_column_spec(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_get_dataset(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_dataset(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_get_model(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_model(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_get_model_evaluation(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_model_evaluation(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_get_table_spec(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_table_spec(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_import_data(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_import_data(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_list_column_specs(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_column_specs(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_list_datasets(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_datasets(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_list_model_evaluations(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_model_evaluations(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_list_models(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_models(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_list_table_specs(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_table_specs(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_undeploy_model(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_undeploy_model(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_update_column_spec(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_update_column_spec(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_update_dataset(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_update_dataset(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_update_table_spec(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_update_table_spec(self, response): + logging.log(f"Received response: {response}") + return response + + transport = AutoMlRestTransport(interceptor=MyCustomAutoMlInterceptor()) + client = AutoMlClient(transport=transport) + + + """ + + def pre_create_dataset( + self, request: service.CreateDatasetRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.CreateDatasetRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for create_dataset + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_create_dataset(self, response: gca_dataset.Dataset) -> gca_dataset.Dataset: + """Post-rpc interceptor for create_dataset + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_create_model( + self, request: service.CreateModelRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.CreateModelRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for create_model + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_create_model( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for create_model + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_delete_dataset( + self, request: service.DeleteDatasetRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.DeleteDatasetRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for delete_dataset + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_delete_dataset( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for delete_dataset + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_delete_model( + self, request: service.DeleteModelRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.DeleteModelRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for delete_model + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_delete_model( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for delete_model + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_deploy_model( + self, request: service.DeployModelRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.DeployModelRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for deploy_model + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_deploy_model( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for deploy_model + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_export_data( + self, request: service.ExportDataRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.ExportDataRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for export_data + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_export_data( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for export_data + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_export_evaluated_examples( + self, + request: service.ExportEvaluatedExamplesRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[service.ExportEvaluatedExamplesRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for export_evaluated_examples + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_export_evaluated_examples( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for export_evaluated_examples + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_export_model( + self, request: service.ExportModelRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.ExportModelRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for export_model + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_export_model( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for export_model + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_get_annotation_spec( + self, + request: service.GetAnnotationSpecRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[service.GetAnnotationSpecRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_annotation_spec + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_get_annotation_spec( + self, response: annotation_spec.AnnotationSpec + ) -> annotation_spec.AnnotationSpec: + """Post-rpc interceptor for get_annotation_spec + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_get_column_spec( + self, request: service.GetColumnSpecRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.GetColumnSpecRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_column_spec + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_get_column_spec( + self, response: column_spec.ColumnSpec + ) -> column_spec.ColumnSpec: + """Post-rpc interceptor for get_column_spec + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_get_dataset( + self, request: service.GetDatasetRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.GetDatasetRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_dataset + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_get_dataset(self, response: dataset.Dataset) -> dataset.Dataset: + """Post-rpc interceptor for get_dataset + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_get_model( + self, request: service.GetModelRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.GetModelRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_model + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_get_model(self, response: model.Model) -> model.Model: + """Post-rpc interceptor for get_model + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_get_model_evaluation( + self, + request: service.GetModelEvaluationRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[service.GetModelEvaluationRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_model_evaluation + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_get_model_evaluation( + self, response: model_evaluation.ModelEvaluation + ) -> model_evaluation.ModelEvaluation: + """Post-rpc interceptor for get_model_evaluation + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_get_table_spec( + self, request: service.GetTableSpecRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.GetTableSpecRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_table_spec + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_get_table_spec( + self, response: table_spec.TableSpec + ) -> table_spec.TableSpec: + """Post-rpc interceptor for get_table_spec + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_import_data( + self, request: service.ImportDataRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.ImportDataRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for import_data + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_import_data( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for import_data + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_list_column_specs( + self, + request: service.ListColumnSpecsRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[service.ListColumnSpecsRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for list_column_specs + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_list_column_specs( + self, response: service.ListColumnSpecsResponse + ) -> service.ListColumnSpecsResponse: + """Post-rpc interceptor for list_column_specs + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_list_datasets( + self, request: service.ListDatasetsRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.ListDatasetsRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for list_datasets + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_list_datasets( + self, response: service.ListDatasetsResponse + ) -> service.ListDatasetsResponse: + """Post-rpc interceptor for list_datasets + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_list_model_evaluations( + self, + request: service.ListModelEvaluationsRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[service.ListModelEvaluationsRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for list_model_evaluations + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_list_model_evaluations( + self, response: service.ListModelEvaluationsResponse + ) -> service.ListModelEvaluationsResponse: + """Post-rpc interceptor for list_model_evaluations + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_list_models( + self, request: service.ListModelsRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.ListModelsRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for list_models + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_list_models( + self, response: service.ListModelsResponse + ) -> service.ListModelsResponse: + """Post-rpc interceptor for list_models + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_list_table_specs( + self, + request: service.ListTableSpecsRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[service.ListTableSpecsRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for list_table_specs + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_list_table_specs( + self, response: service.ListTableSpecsResponse + ) -> service.ListTableSpecsResponse: + """Post-rpc interceptor for list_table_specs + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_undeploy_model( + self, request: service.UndeployModelRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.UndeployModelRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for undeploy_model + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_undeploy_model( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for undeploy_model + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_update_column_spec( + self, + request: service.UpdateColumnSpecRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[service.UpdateColumnSpecRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for update_column_spec + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_update_column_spec( + self, response: gca_column_spec.ColumnSpec + ) -> gca_column_spec.ColumnSpec: + """Post-rpc interceptor for update_column_spec + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_update_dataset( + self, request: service.UpdateDatasetRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[service.UpdateDatasetRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for update_dataset + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_update_dataset(self, response: gca_dataset.Dataset) -> gca_dataset.Dataset: + """Post-rpc interceptor for update_dataset + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + def pre_update_table_spec( + self, + request: service.UpdateTableSpecRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[service.UpdateTableSpecRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for update_table_spec + + Override in a subclass to manipulate the request or metadata + before they are sent to the AutoMl server. + """ + return request, metadata + + def post_update_table_spec( + self, response: gca_table_spec.TableSpec + ) -> gca_table_spec.TableSpec: + """Post-rpc interceptor for update_table_spec + + Override in a subclass to manipulate the response + after it is returned by the AutoMl server but before + it is returned to user code. + """ + return response + + +@dataclasses.dataclass +class AutoMlRestStub: + _session: AuthorizedSession + _host: str + _interceptor: AutoMlRestInterceptor + + +class AutoMlRestTransport(AutoMlTransport): + """REST backend transport for AutoMl. + + AutoML Server API. + + The resource names are assigned by the server. The server never + reuses names that it has created after the resources with those + names are deleted. + + An ID of a resource is the last element of the item's resource name. + For + ``projects/{project_id}/locations/{location_id}/datasets/{dataset_id}``, + then the id for the item is ``{dataset_id}``. + + Currently the only supported ``location_id`` is "us-central1". + + On any input that is documented to expect a string parameter in + snake_case or kebab-case, either of those cases is accepted. + + This class defines the same methods as the primary client, so the + primary client can load the underlying transport implementation + and call it. + + It sends JSON representations of protocol buffers over HTTP/1.1 + + """ + + def __init__( + self, + *, + host: str = "automl.googleapis.com", + credentials: Optional[ga_credentials.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + client_cert_source_for_mtls: Optional[Callable[[], Tuple[bytes, bytes]]] = None, + quota_project_id: Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + always_use_jwt_access: Optional[bool] = False, + url_scheme: str = "https", + interceptor: Optional[AutoMlRestInterceptor] = None, + api_audience: Optional[str] = None, + ) -> None: + """Instantiate the transport. + + Args: + host (Optional[str]): + The hostname to connect to. + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is ignored if ``channel`` is provided. + scopes (Optional(Sequence[str])): A list of scopes. This argument is + ignored if ``channel`` is provided. + client_cert_source_for_mtls (Callable[[], Tuple[bytes, bytes]]): Client + certificate to configure mutual TLS HTTP channel. It is ignored + if ``channel`` is provided. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you are developing + your own client library. + always_use_jwt_access (Optional[bool]): Whether self signed JWT should + be used for service account credentials. + url_scheme: the protocol scheme for the API endpoint. Normally + "https", but for testing or local servers, + "http" can be specified. + """ + # Run the base constructor + # TODO(yon-mg): resolve other ctor params i.e. scopes, quota, etc. + # TODO: When custom host (api_endpoint) is set, `scopes` must *also* be set on the + # credentials object + maybe_url_match = re.match("^(?Phttp(?:s)?://)?(?P.*)$", host) + if maybe_url_match is None: + raise ValueError( + f"Unexpected hostname structure: {host}" + ) # pragma: NO COVER + + url_match_items = maybe_url_match.groupdict() + + host = f"{url_scheme}://{host}" if not url_match_items["scheme"] else host + + super().__init__( + host=host, + credentials=credentials, + client_info=client_info, + always_use_jwt_access=always_use_jwt_access, + api_audience=api_audience, + ) + self._session = AuthorizedSession( + self._credentials, default_host=self.DEFAULT_HOST + ) + self._operations_client: Optional[operations_v1.AbstractOperationsClient] = None + if client_cert_source_for_mtls: + self._session.configure_mtls_channel(client_cert_source_for_mtls) + self._interceptor = interceptor or AutoMlRestInterceptor() + self._prep_wrapped_messages(client_info) + + @property + def operations_client(self) -> operations_v1.AbstractOperationsClient: + """Create the client designed to process long-running operations. + + This property caches on the instance; repeated calls return the same + client. + """ + # Only create a new client if we do not already have one. + if self._operations_client is None: + http_options: Dict[str, List[Dict[str, str]]] = { + "google.longrunning.Operations.CancelOperation": [ + { + "method": "post", + "uri": "/v1beta1/{name=projects/*/locations/*/operations/*}:cancel", + "body": "*", + }, + ], + "google.longrunning.Operations.DeleteOperation": [ + { + "method": "delete", + "uri": "/v1beta1/{name=projects/*/locations/*/operations/*}", + }, + ], + "google.longrunning.Operations.GetOperation": [ + { + "method": "get", + "uri": "/v1beta1/{name=projects/*/locations/*/operations/*}", + }, + ], + "google.longrunning.Operations.ListOperations": [ + { + "method": "get", + "uri": "/v1beta1/{name=projects/*/locations/*}/operations", + }, + ], + "google.longrunning.Operations.WaitOperation": [ + { + "method": "post", + "uri": "/v1beta1/{name=projects/*/locations/*/operations/*}:wait", + "body": "*", + }, + ], + } + + rest_transport = operations_v1.OperationsRestTransport( + host=self._host, + # use the credentials which are saved + credentials=self._credentials, + scopes=self._scopes, + http_options=http_options, + path_prefix="v1beta1", + ) + + self._operations_client = operations_v1.AbstractOperationsClient( + transport=rest_transport + ) + + # Return the client from cache. + return self._operations_client + + class _CreateDataset(AutoMlRestStub): + def __hash__(self): + return hash("CreateDataset") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.CreateDatasetRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> gca_dataset.Dataset: + r"""Call the create dataset method over HTTP. + + Args: + request (~.service.CreateDatasetRequest): + The request object. Request message for + [AutoMl.CreateDataset][google.cloud.automl.v1beta1.AutoMl.CreateDataset]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.gca_dataset.Dataset: + A workspace for solving a single, + particular machine learning (ML) + problem. A workspace contains examples + that may be annotated. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1beta1/{parent=projects/*/locations/*}/datasets", + "body": "dataset", + }, + ] + request, metadata = self._interceptor.pre_create_dataset(request, metadata) + pb_request = service.CreateDatasetRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = gca_dataset.Dataset() + pb_resp = gca_dataset.Dataset.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_create_dataset(resp) + return resp + + class _CreateModel(AutoMlRestStub): + def __hash__(self): + return hash("CreateModel") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.CreateModelRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the create model method over HTTP. + + Args: + request (~.service.CreateModelRequest): + The request object. Request message for + [AutoMl.CreateModel][google.cloud.automl.v1beta1.AutoMl.CreateModel]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1beta1/{parent=projects/*/locations/*}/models", + "body": "model", + }, + ] + request, metadata = self._interceptor.pre_create_model(request, metadata) + pb_request = service.CreateModelRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_create_model(resp) + return resp + + class _DeleteDataset(AutoMlRestStub): + def __hash__(self): + return hash("DeleteDataset") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.DeleteDatasetRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the delete dataset method over HTTP. + + Args: + request (~.service.DeleteDatasetRequest): + The request object. Request message for + [AutoMl.DeleteDataset][google.cloud.automl.v1beta1.AutoMl.DeleteDataset]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "delete", + "uri": "/v1beta1/{name=projects/*/locations/*/datasets/*}", + }, + ] + request, metadata = self._interceptor.pre_delete_dataset(request, metadata) + pb_request = service.DeleteDatasetRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_delete_dataset(resp) + return resp + + class _DeleteModel(AutoMlRestStub): + def __hash__(self): + return hash("DeleteModel") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.DeleteModelRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the delete model method over HTTP. + + Args: + request (~.service.DeleteModelRequest): + The request object. Request message for + [AutoMl.DeleteModel][google.cloud.automl.v1beta1.AutoMl.DeleteModel]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "delete", + "uri": "/v1beta1/{name=projects/*/locations/*/models/*}", + }, + ] + request, metadata = self._interceptor.pre_delete_model(request, metadata) + pb_request = service.DeleteModelRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_delete_model(resp) + return resp + + class _DeployModel(AutoMlRestStub): + def __hash__(self): + return hash("DeployModel") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.DeployModelRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the deploy model method over HTTP. + + Args: + request (~.service.DeployModelRequest): + The request object. Request message for + [AutoMl.DeployModel][google.cloud.automl.v1beta1.AutoMl.DeployModel]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1beta1/{name=projects/*/locations/*/models/*}:deploy", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_deploy_model(request, metadata) + pb_request = service.DeployModelRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_deploy_model(resp) + return resp + + class _ExportData(AutoMlRestStub): + def __hash__(self): + return hash("ExportData") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.ExportDataRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the export data method over HTTP. + + Args: + request (~.service.ExportDataRequest): + The request object. Request message for + [AutoMl.ExportData][google.cloud.automl.v1beta1.AutoMl.ExportData]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1beta1/{name=projects/*/locations/*/datasets/*}:exportData", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_export_data(request, metadata) + pb_request = service.ExportDataRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_export_data(resp) + return resp + + class _ExportEvaluatedExamples(AutoMlRestStub): + def __hash__(self): + return hash("ExportEvaluatedExamples") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.ExportEvaluatedExamplesRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the export evaluated examples method over HTTP. + + Args: + request (~.service.ExportEvaluatedExamplesRequest): + The request object. Request message for + [AutoMl.ExportEvaluatedExamples][google.cloud.automl.v1beta1.AutoMl.ExportEvaluatedExamples]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1beta1/{name=projects/*/locations/*/models/*}:exportEvaluatedExamples", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_export_evaluated_examples( + request, metadata + ) + pb_request = service.ExportEvaluatedExamplesRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_export_evaluated_examples(resp) + return resp + + class _ExportModel(AutoMlRestStub): + def __hash__(self): + return hash("ExportModel") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.ExportModelRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the export model method over HTTP. + + Args: + request (~.service.ExportModelRequest): + The request object. Request message for + [AutoMl.ExportModel][google.cloud.automl.v1beta1.AutoMl.ExportModel]. + Models need to be enabled for exporting, otherwise an + error code will be returned. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1beta1/{name=projects/*/locations/*/models/*}:export", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_export_model(request, metadata) + pb_request = service.ExportModelRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_export_model(resp) + return resp + + class _GetAnnotationSpec(AutoMlRestStub): + def __hash__(self): + return hash("GetAnnotationSpec") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.GetAnnotationSpecRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> annotation_spec.AnnotationSpec: + r"""Call the get annotation spec method over HTTP. + + Args: + request (~.service.GetAnnotationSpecRequest): + The request object. Request message for + [AutoMl.GetAnnotationSpec][google.cloud.automl.v1beta1.AutoMl.GetAnnotationSpec]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.annotation_spec.AnnotationSpec: + A definition of an annotation spec. + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1beta1/{name=projects/*/locations/*/datasets/*/annotationSpecs/*}", + }, + ] + request, metadata = self._interceptor.pre_get_annotation_spec( + request, metadata + ) + pb_request = service.GetAnnotationSpecRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = annotation_spec.AnnotationSpec() + pb_resp = annotation_spec.AnnotationSpec.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_annotation_spec(resp) + return resp + + class _GetColumnSpec(AutoMlRestStub): + def __hash__(self): + return hash("GetColumnSpec") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.GetColumnSpecRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> column_spec.ColumnSpec: + r"""Call the get column spec method over HTTP. + + Args: + request (~.service.GetColumnSpecRequest): + The request object. Request message for + [AutoMl.GetColumnSpec][google.cloud.automl.v1beta1.AutoMl.GetColumnSpec]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.column_spec.ColumnSpec: + A representation of a column in a relational table. When + listing them, column specs are returned in the same + order in which they were given on import . Used by: + + - Tables + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1beta1/{name=projects/*/locations/*/datasets/*/tableSpecs/*/columnSpecs/*}", + }, + ] + request, metadata = self._interceptor.pre_get_column_spec(request, metadata) + pb_request = service.GetColumnSpecRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = column_spec.ColumnSpec() + pb_resp = column_spec.ColumnSpec.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_column_spec(resp) + return resp + + class _GetDataset(AutoMlRestStub): + def __hash__(self): + return hash("GetDataset") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.GetDatasetRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> dataset.Dataset: + r"""Call the get dataset method over HTTP. + + Args: + request (~.service.GetDatasetRequest): + The request object. Request message for + [AutoMl.GetDataset][google.cloud.automl.v1beta1.AutoMl.GetDataset]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.dataset.Dataset: + A workspace for solving a single, + particular machine learning (ML) + problem. A workspace contains examples + that may be annotated. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1beta1/{name=projects/*/locations/*/datasets/*}", + }, + ] + request, metadata = self._interceptor.pre_get_dataset(request, metadata) + pb_request = service.GetDatasetRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = dataset.Dataset() + pb_resp = dataset.Dataset.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_dataset(resp) + return resp + + class _GetModel(AutoMlRestStub): + def __hash__(self): + return hash("GetModel") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.GetModelRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> model.Model: + r"""Call the get model method over HTTP. + + Args: + request (~.service.GetModelRequest): + The request object. Request message for + [AutoMl.GetModel][google.cloud.automl.v1beta1.AutoMl.GetModel]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.model.Model: + API proto representing a trained + machine learning model. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1beta1/{name=projects/*/locations/*/models/*}", + }, + ] + request, metadata = self._interceptor.pre_get_model(request, metadata) + pb_request = service.GetModelRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = model.Model() + pb_resp = model.Model.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_model(resp) + return resp + + class _GetModelEvaluation(AutoMlRestStub): + def __hash__(self): + return hash("GetModelEvaluation") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.GetModelEvaluationRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> model_evaluation.ModelEvaluation: + r"""Call the get model evaluation method over HTTP. + + Args: + request (~.service.GetModelEvaluationRequest): + The request object. Request message for + [AutoMl.GetModelEvaluation][google.cloud.automl.v1beta1.AutoMl.GetModelEvaluation]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.model_evaluation.ModelEvaluation: + Evaluation results of a model. + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1beta1/{name=projects/*/locations/*/models/*/modelEvaluations/*}", + }, + ] + request, metadata = self._interceptor.pre_get_model_evaluation( + request, metadata + ) + pb_request = service.GetModelEvaluationRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = model_evaluation.ModelEvaluation() + pb_resp = model_evaluation.ModelEvaluation.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_model_evaluation(resp) + return resp + + class _GetTableSpec(AutoMlRestStub): + def __hash__(self): + return hash("GetTableSpec") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.GetTableSpecRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> table_spec.TableSpec: + r"""Call the get table spec method over HTTP. + + Args: + request (~.service.GetTableSpecRequest): + The request object. Request message for + [AutoMl.GetTableSpec][google.cloud.automl.v1beta1.AutoMl.GetTableSpec]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.table_spec.TableSpec: + A specification of a relational table. The table's + schema is represented via its child column specs. It is + pre-populated as part of ImportData by schema inference + algorithm, the version of which is a required parameter + of ImportData InputConfig. Note: While working with a + table, at times the schema may be inconsistent with the + data in the table (e.g. string in a FLOAT64 column). The + consistency validation is done upon creation of a model. + Used by: + + - Tables + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1beta1/{name=projects/*/locations/*/datasets/*/tableSpecs/*}", + }, + ] + request, metadata = self._interceptor.pre_get_table_spec(request, metadata) + pb_request = service.GetTableSpecRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = table_spec.TableSpec() + pb_resp = table_spec.TableSpec.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_table_spec(resp) + return resp + + class _ImportData(AutoMlRestStub): + def __hash__(self): + return hash("ImportData") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.ImportDataRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the import data method over HTTP. + + Args: + request (~.service.ImportDataRequest): + The request object. Request message for + [AutoMl.ImportData][google.cloud.automl.v1beta1.AutoMl.ImportData]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1beta1/{name=projects/*/locations/*/datasets/*}:importData", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_import_data(request, metadata) + pb_request = service.ImportDataRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_import_data(resp) + return resp + + class _ListColumnSpecs(AutoMlRestStub): + def __hash__(self): + return hash("ListColumnSpecs") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.ListColumnSpecsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> service.ListColumnSpecsResponse: + r"""Call the list column specs method over HTTP. + + Args: + request (~.service.ListColumnSpecsRequest): + The request object. Request message for + [AutoMl.ListColumnSpecs][google.cloud.automl.v1beta1.AutoMl.ListColumnSpecs]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.service.ListColumnSpecsResponse: + Response message for + [AutoMl.ListColumnSpecs][google.cloud.automl.v1beta1.AutoMl.ListColumnSpecs]. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1beta1/{parent=projects/*/locations/*/datasets/*/tableSpecs/*}/columnSpecs", + }, + ] + request, metadata = self._interceptor.pre_list_column_specs( + request, metadata + ) + pb_request = service.ListColumnSpecsRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = service.ListColumnSpecsResponse() + pb_resp = service.ListColumnSpecsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_column_specs(resp) + return resp + + class _ListDatasets(AutoMlRestStub): + def __hash__(self): + return hash("ListDatasets") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.ListDatasetsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> service.ListDatasetsResponse: + r"""Call the list datasets method over HTTP. + + Args: + request (~.service.ListDatasetsRequest): + The request object. Request message for + [AutoMl.ListDatasets][google.cloud.automl.v1beta1.AutoMl.ListDatasets]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.service.ListDatasetsResponse: + Response message for + [AutoMl.ListDatasets][google.cloud.automl.v1beta1.AutoMl.ListDatasets]. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1beta1/{parent=projects/*/locations/*}/datasets", + }, + ] + request, metadata = self._interceptor.pre_list_datasets(request, metadata) + pb_request = service.ListDatasetsRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = service.ListDatasetsResponse() + pb_resp = service.ListDatasetsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_datasets(resp) + return resp + + class _ListModelEvaluations(AutoMlRestStub): + def __hash__(self): + return hash("ListModelEvaluations") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.ListModelEvaluationsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> service.ListModelEvaluationsResponse: + r"""Call the list model evaluations method over HTTP. + + Args: + request (~.service.ListModelEvaluationsRequest): + The request object. Request message for + [AutoMl.ListModelEvaluations][google.cloud.automl.v1beta1.AutoMl.ListModelEvaluations]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.service.ListModelEvaluationsResponse: + Response message for + [AutoMl.ListModelEvaluations][google.cloud.automl.v1beta1.AutoMl.ListModelEvaluations]. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1beta1/{parent=projects/*/locations/*/models/*}/modelEvaluations", + }, + ] + request, metadata = self._interceptor.pre_list_model_evaluations( + request, metadata + ) + pb_request = service.ListModelEvaluationsRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = service.ListModelEvaluationsResponse() + pb_resp = service.ListModelEvaluationsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_model_evaluations(resp) + return resp + + class _ListModels(AutoMlRestStub): + def __hash__(self): + return hash("ListModels") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.ListModelsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> service.ListModelsResponse: + r"""Call the list models method over HTTP. + + Args: + request (~.service.ListModelsRequest): + The request object. Request message for + [AutoMl.ListModels][google.cloud.automl.v1beta1.AutoMl.ListModels]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.service.ListModelsResponse: + Response message for + [AutoMl.ListModels][google.cloud.automl.v1beta1.AutoMl.ListModels]. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1beta1/{parent=projects/*/locations/*}/models", + }, + ] + request, metadata = self._interceptor.pre_list_models(request, metadata) + pb_request = service.ListModelsRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = service.ListModelsResponse() + pb_resp = service.ListModelsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_models(resp) + return resp + + class _ListTableSpecs(AutoMlRestStub): + def __hash__(self): + return hash("ListTableSpecs") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.ListTableSpecsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> service.ListTableSpecsResponse: + r"""Call the list table specs method over HTTP. + + Args: + request (~.service.ListTableSpecsRequest): + The request object. Request message for + [AutoMl.ListTableSpecs][google.cloud.automl.v1beta1.AutoMl.ListTableSpecs]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.service.ListTableSpecsResponse: + Response message for + [AutoMl.ListTableSpecs][google.cloud.automl.v1beta1.AutoMl.ListTableSpecs]. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1beta1/{parent=projects/*/locations/*/datasets/*}/tableSpecs", + }, + ] + request, metadata = self._interceptor.pre_list_table_specs( + request, metadata + ) + pb_request = service.ListTableSpecsRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = service.ListTableSpecsResponse() + pb_resp = service.ListTableSpecsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_table_specs(resp) + return resp + + class _UndeployModel(AutoMlRestStub): + def __hash__(self): + return hash("UndeployModel") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.UndeployModelRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the undeploy model method over HTTP. + + Args: + request (~.service.UndeployModelRequest): + The request object. Request message for + [AutoMl.UndeployModel][google.cloud.automl.v1beta1.AutoMl.UndeployModel]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1beta1/{name=projects/*/locations/*/models/*}:undeploy", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_undeploy_model(request, metadata) + pb_request = service.UndeployModelRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_undeploy_model(resp) + return resp + + class _UpdateColumnSpec(AutoMlRestStub): + def __hash__(self): + return hash("UpdateColumnSpec") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.UpdateColumnSpecRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> gca_column_spec.ColumnSpec: + r"""Call the update column spec method over HTTP. + + Args: + request (~.service.UpdateColumnSpecRequest): + The request object. Request message for + [AutoMl.UpdateColumnSpec][google.cloud.automl.v1beta1.AutoMl.UpdateColumnSpec] + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.gca_column_spec.ColumnSpec: + A representation of a column in a relational table. When + listing them, column specs are returned in the same + order in which they were given on import . Used by: + + - Tables + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "patch", + "uri": "/v1beta1/{column_spec.name=projects/*/locations/*/datasets/*/tableSpecs/*/columnSpecs/*}", + "body": "column_spec", + }, + ] + request, metadata = self._interceptor.pre_update_column_spec( + request, metadata + ) + pb_request = service.UpdateColumnSpecRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = gca_column_spec.ColumnSpec() + pb_resp = gca_column_spec.ColumnSpec.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_update_column_spec(resp) + return resp + + class _UpdateDataset(AutoMlRestStub): + def __hash__(self): + return hash("UpdateDataset") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.UpdateDatasetRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> gca_dataset.Dataset: + r"""Call the update dataset method over HTTP. + + Args: + request (~.service.UpdateDatasetRequest): + The request object. Request message for + [AutoMl.UpdateDataset][google.cloud.automl.v1beta1.AutoMl.UpdateDataset] + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.gca_dataset.Dataset: + A workspace for solving a single, + particular machine learning (ML) + problem. A workspace contains examples + that may be annotated. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "patch", + "uri": "/v1beta1/{dataset.name=projects/*/locations/*/datasets/*}", + "body": "dataset", + }, + ] + request, metadata = self._interceptor.pre_update_dataset(request, metadata) + pb_request = service.UpdateDatasetRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = gca_dataset.Dataset() + pb_resp = gca_dataset.Dataset.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_update_dataset(resp) + return resp + + class _UpdateTableSpec(AutoMlRestStub): + def __hash__(self): + return hash("UpdateTableSpec") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: service.UpdateTableSpecRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> gca_table_spec.TableSpec: + r"""Call the update table spec method over HTTP. + + Args: + request (~.service.UpdateTableSpecRequest): + The request object. Request message for + [AutoMl.UpdateTableSpec][google.cloud.automl.v1beta1.AutoMl.UpdateTableSpec] + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.gca_table_spec.TableSpec: + A specification of a relational table. The table's + schema is represented via its child column specs. It is + pre-populated as part of ImportData by schema inference + algorithm, the version of which is a required parameter + of ImportData InputConfig. Note: While working with a + table, at times the schema may be inconsistent with the + data in the table (e.g. string in a FLOAT64 column). The + consistency validation is done upon creation of a model. + Used by: + + - Tables + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "patch", + "uri": "/v1beta1/{table_spec.name=projects/*/locations/*/datasets/*/tableSpecs/*}", + "body": "table_spec", + }, + ] + request, metadata = self._interceptor.pre_update_table_spec( + request, metadata + ) + pb_request = service.UpdateTableSpecRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = gca_table_spec.TableSpec() + pb_resp = gca_table_spec.TableSpec.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_update_table_spec(resp) + return resp + + @property + def create_dataset( + self, + ) -> Callable[[service.CreateDatasetRequest], gca_dataset.Dataset]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._CreateDataset(self._session, self._host, self._interceptor) # type: ignore + + @property + def create_model( + self, + ) -> Callable[[service.CreateModelRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._CreateModel(self._session, self._host, self._interceptor) # type: ignore + + @property + def delete_dataset( + self, + ) -> Callable[[service.DeleteDatasetRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._DeleteDataset(self._session, self._host, self._interceptor) # type: ignore + + @property + def delete_model( + self, + ) -> Callable[[service.DeleteModelRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._DeleteModel(self._session, self._host, self._interceptor) # type: ignore + + @property + def deploy_model( + self, + ) -> Callable[[service.DeployModelRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._DeployModel(self._session, self._host, self._interceptor) # type: ignore + + @property + def export_data( + self, + ) -> Callable[[service.ExportDataRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ExportData(self._session, self._host, self._interceptor) # type: ignore + + @property + def export_evaluated_examples( + self, + ) -> Callable[[service.ExportEvaluatedExamplesRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ExportEvaluatedExamples(self._session, self._host, self._interceptor) # type: ignore + + @property + def export_model( + self, + ) -> Callable[[service.ExportModelRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ExportModel(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_annotation_spec( + self, + ) -> Callable[[service.GetAnnotationSpecRequest], annotation_spec.AnnotationSpec]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._GetAnnotationSpec(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_column_spec( + self, + ) -> Callable[[service.GetColumnSpecRequest], column_spec.ColumnSpec]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._GetColumnSpec(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_dataset(self) -> Callable[[service.GetDatasetRequest], dataset.Dataset]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._GetDataset(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_model(self) -> Callable[[service.GetModelRequest], model.Model]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._GetModel(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_model_evaluation( + self, + ) -> Callable[ + [service.GetModelEvaluationRequest], model_evaluation.ModelEvaluation + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._GetModelEvaluation(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_table_spec( + self, + ) -> Callable[[service.GetTableSpecRequest], table_spec.TableSpec]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._GetTableSpec(self._session, self._host, self._interceptor) # type: ignore + + @property + def import_data( + self, + ) -> Callable[[service.ImportDataRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ImportData(self._session, self._host, self._interceptor) # type: ignore + + @property + def list_column_specs( + self, + ) -> Callable[[service.ListColumnSpecsRequest], service.ListColumnSpecsResponse]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ListColumnSpecs(self._session, self._host, self._interceptor) # type: ignore + + @property + def list_datasets( + self, + ) -> Callable[[service.ListDatasetsRequest], service.ListDatasetsResponse]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ListDatasets(self._session, self._host, self._interceptor) # type: ignore + + @property + def list_model_evaluations( + self, + ) -> Callable[ + [service.ListModelEvaluationsRequest], service.ListModelEvaluationsResponse + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ListModelEvaluations(self._session, self._host, self._interceptor) # type: ignore + + @property + def list_models( + self, + ) -> Callable[[service.ListModelsRequest], service.ListModelsResponse]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ListModels(self._session, self._host, self._interceptor) # type: ignore + + @property + def list_table_specs( + self, + ) -> Callable[[service.ListTableSpecsRequest], service.ListTableSpecsResponse]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ListTableSpecs(self._session, self._host, self._interceptor) # type: ignore + + @property + def undeploy_model( + self, + ) -> Callable[[service.UndeployModelRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._UndeployModel(self._session, self._host, self._interceptor) # type: ignore + + @property + def update_column_spec( + self, + ) -> Callable[[service.UpdateColumnSpecRequest], gca_column_spec.ColumnSpec]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._UpdateColumnSpec(self._session, self._host, self._interceptor) # type: ignore + + @property + def update_dataset( + self, + ) -> Callable[[service.UpdateDatasetRequest], gca_dataset.Dataset]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._UpdateDataset(self._session, self._host, self._interceptor) # type: ignore + + @property + def update_table_spec( + self, + ) -> Callable[[service.UpdateTableSpecRequest], gca_table_spec.TableSpec]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._UpdateTableSpec(self._session, self._host, self._interceptor) # type: ignore + + @property + def kind(self) -> str: + return "rest" + + def close(self): + self._session.close() + + +__all__ = ("AutoMlRestTransport",) diff --git a/google/cloud/automl_v1beta1/services/prediction_service/client.py b/google/cloud/automl_v1beta1/services/prediction_service/client.py index 33850150..349dfcbe 100644 --- a/google/cloud/automl_v1beta1/services/prediction_service/client.py +++ b/google/cloud/automl_v1beta1/services/prediction_service/client.py @@ -56,6 +56,7 @@ from .transports.base import PredictionServiceTransport, DEFAULT_CLIENT_INFO from .transports.grpc import PredictionServiceGrpcTransport from .transports.grpc_asyncio import PredictionServiceGrpcAsyncIOTransport +from .transports.rest import PredictionServiceRestTransport class PredictionServiceClientMeta(type): @@ -71,6 +72,7 @@ class PredictionServiceClientMeta(type): ) # type: Dict[str, Type[PredictionServiceTransport]] _transport_registry["grpc"] = PredictionServiceGrpcTransport _transport_registry["grpc_asyncio"] = PredictionServiceGrpcAsyncIOTransport + _transport_registry["rest"] = PredictionServiceRestTransport def get_transport_class( cls, diff --git a/google/cloud/automl_v1beta1/services/prediction_service/transports/__init__.py b/google/cloud/automl_v1beta1/services/prediction_service/transports/__init__.py index 3b81984d..c3ca80df 100644 --- a/google/cloud/automl_v1beta1/services/prediction_service/transports/__init__.py +++ b/google/cloud/automl_v1beta1/services/prediction_service/transports/__init__.py @@ -19,15 +19,20 @@ from .base import PredictionServiceTransport from .grpc import PredictionServiceGrpcTransport from .grpc_asyncio import PredictionServiceGrpcAsyncIOTransport +from .rest import PredictionServiceRestTransport +from .rest import PredictionServiceRestInterceptor # Compile a registry of transports. _transport_registry = OrderedDict() # type: Dict[str, Type[PredictionServiceTransport]] _transport_registry["grpc"] = PredictionServiceGrpcTransport _transport_registry["grpc_asyncio"] = PredictionServiceGrpcAsyncIOTransport +_transport_registry["rest"] = PredictionServiceRestTransport __all__ = ( "PredictionServiceTransport", "PredictionServiceGrpcTransport", "PredictionServiceGrpcAsyncIOTransport", + "PredictionServiceRestTransport", + "PredictionServiceRestInterceptor", ) diff --git a/google/cloud/automl_v1beta1/services/prediction_service/transports/rest.py b/google/cloud/automl_v1beta1/services/prediction_service/transports/rest.py new file mode 100644 index 00000000..e02a933d --- /dev/null +++ b/google/cloud/automl_v1beta1/services/prediction_service/transports/rest.py @@ -0,0 +1,526 @@ +# -*- coding: utf-8 -*- +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from google.auth.transport.requests import AuthorizedSession # type: ignore +import json # type: ignore +import grpc # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore +from google.auth import credentials as ga_credentials # type: ignore +from google.api_core import exceptions as core_exceptions +from google.api_core import retry as retries +from google.api_core import rest_helpers +from google.api_core import rest_streaming +from google.api_core import path_template +from google.api_core import gapic_v1 + +from google.protobuf import json_format +from google.api_core import operations_v1 +from requests import __version__ as requests_version +import dataclasses +import re +from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Union +import warnings + +try: + OptionalRetry = Union[retries.Retry, gapic_v1.method._MethodDefault] +except AttributeError: # pragma: NO COVER + OptionalRetry = Union[retries.Retry, object] # type: ignore + + +from google.cloud.automl_v1beta1.types import prediction_service +from google.longrunning import operations_pb2 # type: ignore + +from .base import ( + PredictionServiceTransport, + DEFAULT_CLIENT_INFO as BASE_DEFAULT_CLIENT_INFO, +) + + +DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=BASE_DEFAULT_CLIENT_INFO.gapic_version, + grpc_version=None, + rest_version=requests_version, +) + + +class PredictionServiceRestInterceptor: + """Interceptor for PredictionService. + + Interceptors are used to manipulate requests, request metadata, and responses + in arbitrary ways. + Example use cases include: + * Logging + * Verifying requests according to service or custom semantics + * Stripping extraneous information from responses + + These use cases and more can be enabled by injecting an + instance of a custom subclass when constructing the PredictionServiceRestTransport. + + .. code-block:: python + class MyCustomPredictionServiceInterceptor(PredictionServiceRestInterceptor): + def pre_batch_predict(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_batch_predict(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_predict(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_predict(self, response): + logging.log(f"Received response: {response}") + return response + + transport = PredictionServiceRestTransport(interceptor=MyCustomPredictionServiceInterceptor()) + client = PredictionServiceClient(transport=transport) + + + """ + + def pre_batch_predict( + self, + request: prediction_service.BatchPredictRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[prediction_service.BatchPredictRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for batch_predict + + Override in a subclass to manipulate the request or metadata + before they are sent to the PredictionService server. + """ + return request, metadata + + def post_batch_predict( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for batch_predict + + Override in a subclass to manipulate the response + after it is returned by the PredictionService server but before + it is returned to user code. + """ + return response + + def pre_predict( + self, + request: prediction_service.PredictRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[prediction_service.PredictRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for predict + + Override in a subclass to manipulate the request or metadata + before they are sent to the PredictionService server. + """ + return request, metadata + + def post_predict( + self, response: prediction_service.PredictResponse + ) -> prediction_service.PredictResponse: + """Post-rpc interceptor for predict + + Override in a subclass to manipulate the response + after it is returned by the PredictionService server but before + it is returned to user code. + """ + return response + + +@dataclasses.dataclass +class PredictionServiceRestStub: + _session: AuthorizedSession + _host: str + _interceptor: PredictionServiceRestInterceptor + + +class PredictionServiceRestTransport(PredictionServiceTransport): + """REST backend transport for PredictionService. + + AutoML Prediction API. + + On any input that is documented to expect a string parameter in + snake_case or kebab-case, either of those cases is accepted. + + This class defines the same methods as the primary client, so the + primary client can load the underlying transport implementation + and call it. + + It sends JSON representations of protocol buffers over HTTP/1.1 + + """ + + def __init__( + self, + *, + host: str = "automl.googleapis.com", + credentials: Optional[ga_credentials.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + client_cert_source_for_mtls: Optional[Callable[[], Tuple[bytes, bytes]]] = None, + quota_project_id: Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + always_use_jwt_access: Optional[bool] = False, + url_scheme: str = "https", + interceptor: Optional[PredictionServiceRestInterceptor] = None, + api_audience: Optional[str] = None, + ) -> None: + """Instantiate the transport. + + Args: + host (Optional[str]): + The hostname to connect to. + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is ignored if ``channel`` is provided. + scopes (Optional(Sequence[str])): A list of scopes. This argument is + ignored if ``channel`` is provided. + client_cert_source_for_mtls (Callable[[], Tuple[bytes, bytes]]): Client + certificate to configure mutual TLS HTTP channel. It is ignored + if ``channel`` is provided. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you are developing + your own client library. + always_use_jwt_access (Optional[bool]): Whether self signed JWT should + be used for service account credentials. + url_scheme: the protocol scheme for the API endpoint. Normally + "https", but for testing or local servers, + "http" can be specified. + """ + # Run the base constructor + # TODO(yon-mg): resolve other ctor params i.e. scopes, quota, etc. + # TODO: When custom host (api_endpoint) is set, `scopes` must *also* be set on the + # credentials object + maybe_url_match = re.match("^(?Phttp(?:s)?://)?(?P.*)$", host) + if maybe_url_match is None: + raise ValueError( + f"Unexpected hostname structure: {host}" + ) # pragma: NO COVER + + url_match_items = maybe_url_match.groupdict() + + host = f"{url_scheme}://{host}" if not url_match_items["scheme"] else host + + super().__init__( + host=host, + credentials=credentials, + client_info=client_info, + always_use_jwt_access=always_use_jwt_access, + api_audience=api_audience, + ) + self._session = AuthorizedSession( + self._credentials, default_host=self.DEFAULT_HOST + ) + self._operations_client: Optional[operations_v1.AbstractOperationsClient] = None + if client_cert_source_for_mtls: + self._session.configure_mtls_channel(client_cert_source_for_mtls) + self._interceptor = interceptor or PredictionServiceRestInterceptor() + self._prep_wrapped_messages(client_info) + + @property + def operations_client(self) -> operations_v1.AbstractOperationsClient: + """Create the client designed to process long-running operations. + + This property caches on the instance; repeated calls return the same + client. + """ + # Only create a new client if we do not already have one. + if self._operations_client is None: + http_options: Dict[str, List[Dict[str, str]]] = { + "google.longrunning.Operations.CancelOperation": [ + { + "method": "post", + "uri": "/v1beta1/{name=projects/*/locations/*/operations/*}:cancel", + "body": "*", + }, + ], + "google.longrunning.Operations.DeleteOperation": [ + { + "method": "delete", + "uri": "/v1beta1/{name=projects/*/locations/*/operations/*}", + }, + ], + "google.longrunning.Operations.GetOperation": [ + { + "method": "get", + "uri": "/v1beta1/{name=projects/*/locations/*/operations/*}", + }, + ], + "google.longrunning.Operations.ListOperations": [ + { + "method": "get", + "uri": "/v1beta1/{name=projects/*/locations/*}/operations", + }, + ], + "google.longrunning.Operations.WaitOperation": [ + { + "method": "post", + "uri": "/v1beta1/{name=projects/*/locations/*/operations/*}:wait", + "body": "*", + }, + ], + } + + rest_transport = operations_v1.OperationsRestTransport( + host=self._host, + # use the credentials which are saved + credentials=self._credentials, + scopes=self._scopes, + http_options=http_options, + path_prefix="v1beta1", + ) + + self._operations_client = operations_v1.AbstractOperationsClient( + transport=rest_transport + ) + + # Return the client from cache. + return self._operations_client + + class _BatchPredict(PredictionServiceRestStub): + def __hash__(self): + return hash("BatchPredict") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: prediction_service.BatchPredictRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the batch predict method over HTTP. + + Args: + request (~.prediction_service.BatchPredictRequest): + The request object. Request message for + [PredictionService.BatchPredict][google.cloud.automl.v1beta1.PredictionService.BatchPredict]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1beta1/{name=projects/*/locations/*/models/*}:batchPredict", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_batch_predict(request, metadata) + pb_request = prediction_service.BatchPredictRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_batch_predict(resp) + return resp + + class _Predict(PredictionServiceRestStub): + def __hash__(self): + return hash("Predict") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: prediction_service.PredictRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> prediction_service.PredictResponse: + r"""Call the predict method over HTTP. + + Args: + request (~.prediction_service.PredictRequest): + The request object. Request message for + [PredictionService.Predict][google.cloud.automl.v1beta1.PredictionService.Predict]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.prediction_service.PredictResponse: + Response message for + [PredictionService.Predict][google.cloud.automl.v1beta1.PredictionService.Predict]. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1beta1/{name=projects/*/locations/*/models/*}:predict", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_predict(request, metadata) + pb_request = prediction_service.PredictRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = prediction_service.PredictResponse() + pb_resp = prediction_service.PredictResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_predict(resp) + return resp + + @property + def batch_predict( + self, + ) -> Callable[[prediction_service.BatchPredictRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._BatchPredict(self._session, self._host, self._interceptor) # type: ignore + + @property + def predict( + self, + ) -> Callable[ + [prediction_service.PredictRequest], prediction_service.PredictResponse + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._Predict(self._session, self._host, self._interceptor) # type: ignore + + @property + def kind(self) -> str: + return "rest" + + def close(self): + self._session.close() + + +__all__ = ("PredictionServiceRestTransport",) diff --git a/google/cloud/automl_v1beta1/types/annotation_payload.py b/google/cloud/automl_v1beta1/types/annotation_payload.py index 67e862e4..f135057f 100644 --- a/google/cloud/automl_v1beta1/types/annotation_payload.py +++ b/google/cloud/automl_v1beta1/types/annotation_payload.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/annotation_spec.py b/google/cloud/automl_v1beta1/types/annotation_spec.py index b03f7b6d..56b0ed2a 100644 --- a/google/cloud/automl_v1beta1/types/annotation_spec.py +++ b/google/cloud/automl_v1beta1/types/annotation_spec.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/classification.py b/google/cloud/automl_v1beta1/types/classification.py index c81c1b57..9d3c4d75 100644 --- a/google/cloud/automl_v1beta1/types/classification.py +++ b/google/cloud/automl_v1beta1/types/classification.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/column_spec.py b/google/cloud/automl_v1beta1/types/column_spec.py index dca9fab9..c6c08e98 100644 --- a/google/cloud/automl_v1beta1/types/column_spec.py +++ b/google/cloud/automl_v1beta1/types/column_spec.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/data_items.py b/google/cloud/automl_v1beta1/types/data_items.py index d4ab20db..868d6c94 100644 --- a/google/cloud/automl_v1beta1/types/data_items.py +++ b/google/cloud/automl_v1beta1/types/data_items.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/data_stats.py b/google/cloud/automl_v1beta1/types/data_stats.py index dd8663b6..81f6db93 100644 --- a/google/cloud/automl_v1beta1/types/data_stats.py +++ b/google/cloud/automl_v1beta1/types/data_stats.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/data_types.py b/google/cloud/automl_v1beta1/types/data_types.py index 4dc85fbe..61eeb8d7 100644 --- a/google/cloud/automl_v1beta1/types/data_types.py +++ b/google/cloud/automl_v1beta1/types/data_types.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/dataset.py b/google/cloud/automl_v1beta1/types/dataset.py index 83cbed2a..d3d07a7a 100644 --- a/google/cloud/automl_v1beta1/types/dataset.py +++ b/google/cloud/automl_v1beta1/types/dataset.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/detection.py b/google/cloud/automl_v1beta1/types/detection.py index 90865b36..097cefd3 100644 --- a/google/cloud/automl_v1beta1/types/detection.py +++ b/google/cloud/automl_v1beta1/types/detection.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/geometry.py b/google/cloud/automl_v1beta1/types/geometry.py index 422556df..747d9d0d 100644 --- a/google/cloud/automl_v1beta1/types/geometry.py +++ b/google/cloud/automl_v1beta1/types/geometry.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/image.py b/google/cloud/automl_v1beta1/types/image.py index 9c2423eb..b0641832 100644 --- a/google/cloud/automl_v1beta1/types/image.py +++ b/google/cloud/automl_v1beta1/types/image.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/io.py b/google/cloud/automl_v1beta1/types/io.py index f6bb73d5..d544ec03 100644 --- a/google/cloud/automl_v1beta1/types/io.py +++ b/google/cloud/automl_v1beta1/types/io.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/model.py b/google/cloud/automl_v1beta1/types/model.py index 851902c2..7d945375 100644 --- a/google/cloud/automl_v1beta1/types/model.py +++ b/google/cloud/automl_v1beta1/types/model.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/model_evaluation.py b/google/cloud/automl_v1beta1/types/model_evaluation.py index 1bd2a1c0..431fa72f 100644 --- a/google/cloud/automl_v1beta1/types/model_evaluation.py +++ b/google/cloud/automl_v1beta1/types/model_evaluation.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/operations.py b/google/cloud/automl_v1beta1/types/operations.py index 15da7b90..48098d16 100644 --- a/google/cloud/automl_v1beta1/types/operations.py +++ b/google/cloud/automl_v1beta1/types/operations.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/prediction_service.py b/google/cloud/automl_v1beta1/types/prediction_service.py index d02c2799..201d4df9 100644 --- a/google/cloud/automl_v1beta1/types/prediction_service.py +++ b/google/cloud/automl_v1beta1/types/prediction_service.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/ranges.py b/google/cloud/automl_v1beta1/types/ranges.py index e06cd53b..d41d0f1a 100644 --- a/google/cloud/automl_v1beta1/types/ranges.py +++ b/google/cloud/automl_v1beta1/types/ranges.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/regression.py b/google/cloud/automl_v1beta1/types/regression.py index a781425e..c62ac88d 100644 --- a/google/cloud/automl_v1beta1/types/regression.py +++ b/google/cloud/automl_v1beta1/types/regression.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/service.py b/google/cloud/automl_v1beta1/types/service.py index a06c60dc..40a2d4f6 100644 --- a/google/cloud/automl_v1beta1/types/service.py +++ b/google/cloud/automl_v1beta1/types/service.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/table_spec.py b/google/cloud/automl_v1beta1/types/table_spec.py index 6632cebe..1a5432f1 100644 --- a/google/cloud/automl_v1beta1/types/table_spec.py +++ b/google/cloud/automl_v1beta1/types/table_spec.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/tables.py b/google/cloud/automl_v1beta1/types/tables.py index 172bf775..97bdea66 100644 --- a/google/cloud/automl_v1beta1/types/tables.py +++ b/google/cloud/automl_v1beta1/types/tables.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/temporal.py b/google/cloud/automl_v1beta1/types/temporal.py index e8619f89..24b3adf6 100644 --- a/google/cloud/automl_v1beta1/types/temporal.py +++ b/google/cloud/automl_v1beta1/types/temporal.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/text.py b/google/cloud/automl_v1beta1/types/text.py index 28c9b4d5..6fff15ea 100644 --- a/google/cloud/automl_v1beta1/types/text.py +++ b/google/cloud/automl_v1beta1/types/text.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/text_extraction.py b/google/cloud/automl_v1beta1/types/text_extraction.py index ee924308..1619f282 100644 --- a/google/cloud/automl_v1beta1/types/text_extraction.py +++ b/google/cloud/automl_v1beta1/types/text_extraction.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/text_segment.py b/google/cloud/automl_v1beta1/types/text_segment.py index dc1b6a9a..c9521298 100644 --- a/google/cloud/automl_v1beta1/types/text_segment.py +++ b/google/cloud/automl_v1beta1/types/text_segment.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/text_sentiment.py b/google/cloud/automl_v1beta1/types/text_sentiment.py index 7b570fff..9bf99b5e 100644 --- a/google/cloud/automl_v1beta1/types/text_sentiment.py +++ b/google/cloud/automl_v1beta1/types/text_sentiment.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/translation.py b/google/cloud/automl_v1beta1/types/translation.py index 9fa7a300..3bb0e1d0 100644 --- a/google/cloud/automl_v1beta1/types/translation.py +++ b/google/cloud/automl_v1beta1/types/translation.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/google/cloud/automl_v1beta1/types/video.py b/google/cloud/automl_v1beta1/types/video.py index b0c6ce8f..00074ba2 100644 --- a/google/cloud/automl_v1beta1/types/video.py +++ b/google/cloud/automl_v1beta1/types/video.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from __future__ import annotations + from typing import MutableMapping, MutableSequence import proto # type: ignore diff --git a/noxfile.py b/noxfile.py index 9a75835f..3fdd6bdd 100644 --- a/noxfile.py +++ b/noxfile.py @@ -194,9 +194,9 @@ def unit(session): def install_systemtest_dependencies(session, *constraints): # Use pre-release gRPC for system tests. - # Exclude version 1.49.0rc1 which has a known issue. - # See https://github.com/grpc/grpc/pull/30642 - session.install("--pre", "grpcio!=1.49.0rc1") + # Exclude version 1.52.0rc1 which has a known issue. + # See https://github.com/grpc/grpc/issues/32163 + session.install("--pre", "grpcio!=1.52.0rc1") session.install(*SYSTEM_TEST_STANDARD_DEPENDENCIES, *constraints) @@ -351,9 +351,7 @@ def prerelease_deps(session): unit_deps_all = UNIT_TEST_STANDARD_DEPENDENCIES + UNIT_TEST_EXTERNAL_DEPENDENCIES session.install(*unit_deps_all) system_deps_all = ( - SYSTEM_TEST_STANDARD_DEPENDENCIES - + SYSTEM_TEST_EXTERNAL_DEPENDENCIES - + SYSTEM_TEST_EXTRAS + SYSTEM_TEST_STANDARD_DEPENDENCIES + SYSTEM_TEST_EXTERNAL_DEPENDENCIES ) session.install(*system_deps_all) @@ -383,8 +381,8 @@ def prerelease_deps(session): # dependency of grpc "six", "googleapis-common-protos", - # Exclude version 1.49.0rc1 which has a known issue. See https://github.com/grpc/grpc/pull/30642 - "grpcio!=1.49.0rc1", + # Exclude version 1.52.0rc1 which has a known issue. See https://github.com/grpc/grpc/issues/32163 + "grpcio!=1.52.0rc1", "grpcio-status", "google-api-core", "proto-plus", diff --git a/samples/generated_samples/snippet_metadata_google.cloud.automl.v1.json b/samples/generated_samples/snippet_metadata_google.cloud.automl.v1.json index cc1fa9bc..f71bf41c 100644 --- a/samples/generated_samples/snippet_metadata_google.cloud.automl.v1.json +++ b/samples/generated_samples/snippet_metadata_google.cloud.automl.v1.json @@ -8,7 +8,7 @@ ], "language": "PYTHON", "name": "google-cloud-automl", - "version": "2.10.1" + "version": "2.11.0" }, "snippets": [ { diff --git a/samples/generated_samples/snippet_metadata_google.cloud.automl.v1beta1.json b/samples/generated_samples/snippet_metadata_google.cloud.automl.v1beta1.json index 242523ea..ad23a76e 100644 --- a/samples/generated_samples/snippet_metadata_google.cloud.automl.v1beta1.json +++ b/samples/generated_samples/snippet_metadata_google.cloud.automl.v1beta1.json @@ -8,7 +8,7 @@ ], "language": "PYTHON", "name": "google-cloud-automl", - "version": "2.10.1" + "version": "2.11.0" }, "snippets": [ { diff --git a/tests/system/gapic/v1beta1/test_system_tables_client_v1.py b/tests/system/gapic/v1beta1/test_system_tables_client_v1.py index 6092c43a..e74c6e7c 100644 --- a/tests/system/gapic/v1beta1/test_system_tables_client_v1.py +++ b/tests/system/gapic/v1beta1/test_system_tables_client_v1.py @@ -59,8 +59,11 @@ def cancel_and_wait(self, op): assert op.cancelled() @vpcsc_config.skip_if_inside_vpcsc - def test_list_datasets(self): - client = automl_v1beta1.TablesClient(project=PROJECT, region=REGION) + @pytest.mark.parametrize("transport", ["grpc", "rest"]) + def test_list_datasets(self, transport): + client = automl_v1beta1.TablesClient( + project=PROJECT, region=REGION, transport=transport + ) dataset = self.ensure_dataset_ready(client) # will raise if not found next( @@ -70,14 +73,20 @@ def test_list_datasets(self): ) @vpcsc_config.skip_if_inside_vpcsc - def test_list_models(self): - client = automl_v1beta1.TablesClient(project=PROJECT, region=REGION) + @pytest.mark.parametrize("transport", ["grpc", "rest"]) + def test_list_models(self, transport): + client = automl_v1beta1.TablesClient( + project=PROJECT, region=REGION, transport=transport + ) model = self.ensure_model_ready(client) # will raise if not found next(iter([m for m in client.list_models(timeout=10) if m.name == model.name])) - def test_create_delete_dataset(self): - client = automl_v1beta1.TablesClient(project=PROJECT, region=REGION) + @pytest.mark.parametrize("transport", ["grpc", "rest"]) + def test_create_delete_dataset(self, transport): + client = automl_v1beta1.TablesClient( + project=PROJECT, region=REGION, transport=transport + ) display_name = _id("t_cr_dl") dataset = client.create_dataset(display_name) assert dataset is not None @@ -87,8 +96,11 @@ def test_create_delete_dataset(self): client.delete_dataset(dataset=dataset) @vpcsc_config.skip_if_inside_vpcsc - def test_import_data(self): - client = automl_v1beta1.TablesClient(project=PROJECT, region=REGION) + @pytest.mark.parametrize("transport", ["grpc", "rest"]) + def test_import_data(self, transport): + client = automl_v1beta1.TablesClient( + project=PROJECT, region=REGION, transport=transport + ) display_name = _id("t_import") dataset = client.create_dataset(display_name) op = client.import_data( @@ -99,8 +111,11 @@ def test_import_data(self): client.delete_dataset(dataset=dataset) @vpcsc_config.skip_if_inside_vpcsc - def test_import_pandas_dataframe(self): - client = automl_v1beta1.TablesClient(project=PROJECT, region=REGION) + @pytest.mark.parametrize("transport", ["grpc", "rest"]) + def test_import_pandas_dataframe(self, transport): + client = automl_v1beta1.TablesClient( + project=PROJECT, region=REGION, transport=transport + ) display_name = _id("t_import_pandas") dataset = client.create_dataset(display_name) dataframe = pandas.DataFrame({"test-col1": [1, 2], "test-col2": [3, 4]}) @@ -128,8 +143,11 @@ def ensure_dataset_ready(self, client): return dataset @vpcsc_config.skip_if_inside_vpcsc - def test_list_column_specs(self): - client = automl_v1beta1.TablesClient(project=PROJECT, region=REGION) + @pytest.mark.parametrize("transport", ["grpc", "rest"]) + def test_list_column_specs(self, transport): + client = automl_v1beta1.TablesClient( + project=PROJECT, region=REGION, transport=transport + ) dataset = self.ensure_dataset_ready(client) # will raise if not found next( @@ -143,22 +161,31 @@ def test_list_column_specs(self): ) @vpcsc_config.skip_if_inside_vpcsc - def test_get_column_spec(self): - client = automl_v1beta1.TablesClient(project=PROJECT, region=REGION) + @pytest.mark.parametrize("transport", ["grpc", "rest"]) + def test_get_column_spec(self, transport): + client = automl_v1beta1.TablesClient( + project=PROJECT, region=REGION, transport=transport + ) dataset = self.ensure_dataset_ready(client) name = [d for d in client.list_column_specs(dataset=dataset)][0].name assert client.get_column_spec(name).name == name @vpcsc_config.skip_if_inside_vpcsc - def test_list_table_specs(self): - client = automl_v1beta1.TablesClient(project=PROJECT, region=REGION) + @pytest.mark.parametrize("transport", ["grpc", "rest"]) + def test_list_table_specs(self, transport): + client = automl_v1beta1.TablesClient( + project=PROJECT, region=REGION, transport=transport + ) dataset = self.ensure_dataset_ready(client) name = [d for d in client.list_table_specs(dataset=dataset)][0].name assert client.get_table_spec(name).name == name @vpcsc_config.skip_if_inside_vpcsc - def test_set_column_nullable(self): - client = automl_v1beta1.TablesClient(project=PROJECT, region=REGION) + @pytest.mark.parametrize("transport", ["grpc", "rest"]) + def test_set_column_nullable(self, transport): + client = automl_v1beta1.TablesClient( + project=PROJECT, region=REGION, transport=transport + ) dataset = self.ensure_dataset_ready(client) client.update_column_spec( dataset=dataset, column_spec_display_name="POutcome", nullable=True @@ -167,8 +194,11 @@ def test_set_column_nullable(self): assert columns["POutcome"].data_type.nullable == True @vpcsc_config.skip_if_inside_vpcsc - def test_set_target_column(self): - client = automl_v1beta1.TablesClient(project=PROJECT, region=REGION) + @pytest.mark.parametrize("transport", ["grpc", "rest"]) + def test_set_target_column(self, transport): + client = automl_v1beta1.TablesClient( + project=PROJECT, region=REGION, transport=transport + ) dataset = self.ensure_dataset_ready(client) client.set_target_column(dataset=dataset, column_spec_display_name="Age") columns = {c.display_name: c for c in client.list_column_specs(dataset=dataset)} @@ -179,8 +209,11 @@ def test_set_target_column(self): ) @vpcsc_config.skip_if_inside_vpcsc - def test_set_weight_column(self): - client = automl_v1beta1.TablesClient(project=PROJECT, region=REGION) + @pytest.mark.parametrize("transport", ["grpc", "rest"]) + def test_set_weight_column(self, transport): + client = automl_v1beta1.TablesClient( + project=PROJECT, region=REGION, transport=transport + ) dataset = self.ensure_dataset_ready(client) client.set_weight_column(dataset=dataset, column_spec_display_name="Duration") columns = {c.display_name: c for c in client.list_column_specs(dataset=dataset)} @@ -191,8 +224,11 @@ def test_set_weight_column(self): ) @vpcsc_config.skip_if_inside_vpcsc - def test_set_weight_and_target_column(self): - client = automl_v1beta1.TablesClient(project=PROJECT, region=REGION) + @pytest.mark.parametrize("transport", ["grpc", "rest"]) + def test_set_weight_and_target_column(self, transport): + client = automl_v1beta1.TablesClient( + project=PROJECT, region=REGION, transport=transport + ) dataset = self.ensure_dataset_ready(client) client.set_weight_column(dataset=dataset, column_spec_display_name="Day") client.set_target_column(dataset=dataset, column_spec_display_name="Campaign") @@ -207,8 +243,11 @@ def test_set_weight_and_target_column(self): ) @vpcsc_config.skip_if_inside_vpcsc - def test_create_delete_model(self): - client = automl_v1beta1.TablesClient(project=PROJECT, region=REGION) + @pytest.mark.parametrize("transport", ["grpc", "rest"]) + def test_create_delete_model(self, transport): + client = automl_v1beta1.TablesClient( + project=PROJECT, region=REGION, transport=transport + ) dataset = self.ensure_dataset_ready(client) client.set_target_column(dataset=dataset, column_spec_display_name="Deposit") display_name = _id("t_cr_dl") @@ -219,8 +258,11 @@ def test_create_delete_model(self): client.delete_model(model_display_name=display_name) @vpcsc_config.skip_if_inside_vpcsc - def test_list_model_evaluations(self): - client = automl_v1beta1.TablesClient(project=PROJECT, region=REGION) + @pytest.mark.parametrize("transport", ["grpc", "rest"]) + def test_list_model_evaluations(self, transport): + client = automl_v1beta1.TablesClient( + project=PROJECT, region=REGION, transport=transport + ) model = self.ensure_model_online(client) # will raise if not found next( @@ -234,15 +276,21 @@ def test_list_model_evaluations(self): ) @vpcsc_config.skip_if_inside_vpcsc - def test_get_model_evaluation(self): - client = automl_v1beta1.TablesClient(project=PROJECT, region=REGION) + @pytest.mark.parametrize("transport", ["grpc", "rest"]) + def test_get_model_evaluation(self, transport): + client = automl_v1beta1.TablesClient( + project=PROJECT, region=REGION, transport=transport + ) model = self.ensure_model_online(client) name = [m for m in client.list_model_evaluations(model=model)][0].name assert client.get_model_evaluation(model_evaluation_name=name).name == name @vpcsc_config.skip_if_inside_vpcsc - def test_online_predict(self): - client = automl_v1beta1.TablesClient(project=PROJECT, region=REGION) + @pytest.mark.parametrize("transport", ["grpc", "rest"]) + def test_online_predict(self, transport): + client = automl_v1beta1.TablesClient( + project=PROJECT, region=REGION, transport=transport + ) model = self.ensure_model_online(client) result = client.predict( inputs={ diff --git a/tests/unit/gapic/automl_v1/test_auto_ml.py b/tests/unit/gapic/automl_v1/test_auto_ml.py index 0f417f48..e30fdef4 100644 --- a/tests/unit/gapic/automl_v1/test_auto_ml.py +++ b/tests/unit/gapic/automl_v1/test_auto_ml.py @@ -24,10 +24,17 @@ import grpc from grpc.experimental import aio +from collections.abc import Iterable +from google.protobuf import json_format +import json import math import pytest from proto.marshal.rules.dates import DurationRule, TimestampRule from proto.marshal.rules import wrappers +from requests import Response +from requests import Request, PreparedRequest +from requests.sessions import Session +from google.protobuf import json_format from google.api_core import client_options from google.api_core import exceptions as core_exceptions @@ -112,6 +119,7 @@ def test__get_default_mtls_endpoint(): [ (AutoMlClient, "grpc"), (AutoMlAsyncClient, "grpc_asyncio"), + (AutoMlClient, "rest"), ], ) def test_auto_ml_client_from_service_account_info(client_class, transport_name): @@ -125,7 +133,11 @@ def test_auto_ml_client_from_service_account_info(client_class, transport_name): assert client.transport._credentials == creds assert isinstance(client, client_class) - assert client.transport._host == ("automl.googleapis.com:443") + assert client.transport._host == ( + "automl.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://automl.googleapis.com" + ) @pytest.mark.parametrize( @@ -133,6 +145,7 @@ def test_auto_ml_client_from_service_account_info(client_class, transport_name): [ (transports.AutoMlGrpcTransport, "grpc"), (transports.AutoMlGrpcAsyncIOTransport, "grpc_asyncio"), + (transports.AutoMlRestTransport, "rest"), ], ) def test_auto_ml_client_service_account_always_use_jwt(transport_class, transport_name): @@ -156,6 +169,7 @@ def test_auto_ml_client_service_account_always_use_jwt(transport_class, transpor [ (AutoMlClient, "grpc"), (AutoMlAsyncClient, "grpc_asyncio"), + (AutoMlClient, "rest"), ], ) def test_auto_ml_client_from_service_account_file(client_class, transport_name): @@ -176,13 +190,18 @@ def test_auto_ml_client_from_service_account_file(client_class, transport_name): assert client.transport._credentials == creds assert isinstance(client, client_class) - assert client.transport._host == ("automl.googleapis.com:443") + assert client.transport._host == ( + "automl.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://automl.googleapis.com" + ) def test_auto_ml_client_get_transport_class(): transport = AutoMlClient.get_transport_class() available_transports = [ transports.AutoMlGrpcTransport, + transports.AutoMlRestTransport, ] assert transport in available_transports @@ -195,6 +214,7 @@ def test_auto_ml_client_get_transport_class(): [ (AutoMlClient, transports.AutoMlGrpcTransport, "grpc"), (AutoMlAsyncClient, transports.AutoMlGrpcAsyncIOTransport, "grpc_asyncio"), + (AutoMlClient, transports.AutoMlRestTransport, "rest"), ], ) @mock.patch.object( @@ -334,6 +354,8 @@ def test_auto_ml_client_client_options(client_class, transport_class, transport_ "grpc_asyncio", "false", ), + (AutoMlClient, transports.AutoMlRestTransport, "rest", "true"), + (AutoMlClient, transports.AutoMlRestTransport, "rest", "false"), ], ) @mock.patch.object( @@ -519,6 +541,7 @@ def test_auto_ml_client_get_mtls_endpoint_and_cert_source(client_class): [ (AutoMlClient, transports.AutoMlGrpcTransport, "grpc"), (AutoMlAsyncClient, transports.AutoMlGrpcAsyncIOTransport, "grpc_asyncio"), + (AutoMlClient, transports.AutoMlRestTransport, "rest"), ], ) def test_auto_ml_client_client_options_scopes( @@ -554,6 +577,7 @@ def test_auto_ml_client_client_options_scopes( "grpc_asyncio", grpc_helpers_async, ), + (AutoMlClient, transports.AutoMlRestTransport, "rest", None), ], ) def test_auto_ml_client_client_options_credentials_file( @@ -5714,191 +5738,5516 @@ async def test_list_model_evaluations_async_pages(): assert page_.raw_page.next_page_token == token -def test_credentials_transport_error(): - # It is an error to provide credentials and a transport instance. - transport = transports.AutoMlGrpcTransport( +@pytest.mark.parametrize( + "request_type", + [ + service.CreateDatasetRequest, + dict, + ], +) +def test_create_dataset_rest(request_type): + client = AutoMlClient( credentials=ga_credentials.AnonymousCredentials(), - ) - with pytest.raises(ValueError): - client = AutoMlClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["dataset"] = { + "translation_dataset_metadata": { + "source_language_code": "source_language_code_value", + "target_language_code": "target_language_code_value", + }, + "image_classification_dataset_metadata": {"classification_type": 1}, + "text_classification_dataset_metadata": {"classification_type": 1}, + "image_object_detection_dataset_metadata": {}, + "text_extraction_dataset_metadata": {}, + "text_sentiment_dataset_metadata": {"sentiment_max": 1404}, + "name": "name_value", + "display_name": "display_name_value", + "description": "description_value", + "example_count": 1396, + "create_time": {"seconds": 751, "nanos": 543}, + "etag": "etag_value", + "labels": {}, + } + request = request_type(**request_init) - # It is an error to provide a credentials file and a transport instance. - transport = transports.AutoMlGrpcTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - with pytest.raises(ValueError): - client = AutoMlClient( - client_options={"credentials_file": "credentials.json"}, - transport=transport, + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.create_dataset(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_create_dataset_rest_required_fields(request_type=service.CreateDatasetRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, ) + ) - # It is an error to provide an api_key and a transport instance. - transport = transports.AutoMlGrpcTransport( + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_dataset._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_dataset._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = AutoMlClient( credentials=ga_credentials.AnonymousCredentials(), - ) - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = AutoMlClient( - client_options=options, - transport=transport, + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.create_dataset(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_dataset_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_dataset._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(()) + & set( + ( + "parent", + "dataset", + ) ) + ) - # It is an error to provide an api_key and a credential. - options = mock.Mock() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = AutoMlClient( - client_options=options, credentials=ga_credentials.AnonymousCredentials() - ) - # It is an error to provide scopes and a transport instance. - transport = transports.AutoMlGrpcTransport( +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_dataset_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), ) - with pytest.raises(ValueError): - client = AutoMlClient( - client_options={"scopes": ["1", "2"]}, - transport=transport, + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.AutoMlRestInterceptor, "post_create_dataset" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_create_dataset" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.CreateDatasetRequest.pb(service.CreateDatasetRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = service.CreateDatasetRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.create_dataset( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], ) + pre.assert_called_once() + post.assert_called_once() -def test_transport_instance(): - # A client may be instantiated with a custom transport instance. - transport = transports.AutoMlGrpcTransport( + +def test_create_dataset_rest_bad_request( + transport: str = "rest", request_type=service.CreateDatasetRequest +): + client = AutoMlClient( credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - client = AutoMlClient(transport=transport) - assert client.transport is transport + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["dataset"] = { + "translation_dataset_metadata": { + "source_language_code": "source_language_code_value", + "target_language_code": "target_language_code_value", + }, + "image_classification_dataset_metadata": {"classification_type": 1}, + "text_classification_dataset_metadata": {"classification_type": 1}, + "image_object_detection_dataset_metadata": {}, + "text_extraction_dataset_metadata": {}, + "text_sentiment_dataset_metadata": {"sentiment_max": 1404}, + "name": "name_value", + "display_name": "display_name_value", + "description": "description_value", + "example_count": 1396, + "create_time": {"seconds": 751, "nanos": 543}, + "etag": "etag_value", + "labels": {}, + } + request = request_type(**request_init) -def test_transport_get_channel(): - # A client may be instantiated with a custom transport instance. - transport = transports.AutoMlGrpcTransport( + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.create_dataset(request) + + +def test_create_dataset_rest_flattened(): + client = AutoMlClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - channel = transport.grpc_channel - assert channel - transport = transports.AutoMlGrpcAsyncIOTransport( + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + dataset=gca_dataset.Dataset( + translation_dataset_metadata=translation.TranslationDatasetMetadata( + source_language_code="source_language_code_value" + ) + ), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.create_dataset(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/datasets" % client.transport._host, + args[1], + ) + + +def test_create_dataset_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - channel = transport.grpc_channel - assert channel + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_dataset( + service.CreateDatasetRequest(), + parent="parent_value", + dataset=gca_dataset.Dataset( + translation_dataset_metadata=translation.TranslationDatasetMetadata( + source_language_code="source_language_code_value" + ) + ), + ) -@pytest.mark.parametrize( - "transport_class", - [ - transports.AutoMlGrpcTransport, - transports.AutoMlGrpcAsyncIOTransport, - ], -) -def test_transport_adc(transport_class): - # Test default credentials are used if not provided. - with mock.patch.object(google.auth, "default") as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport_class() - adc.assert_called_once() + +def test_create_dataset_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) @pytest.mark.parametrize( - "transport_name", + "request_type", [ - "grpc", + service.GetDatasetRequest, + dict, ], ) -def test_transport_kind(transport_name): - transport = AutoMlClient.get_transport_class(transport_name)( +def test_get_dataset_rest(request_type): + client = AutoMlClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - assert transport.kind == transport_name + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = dataset.Dataset( + name="name_value", + display_name="display_name_value", + description="description_value", + example_count=1396, + etag="etag_value", + translation_dataset_metadata=translation.TranslationDatasetMetadata( + source_language_code="source_language_code_value" + ), + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = dataset.Dataset.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.get_dataset(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, dataset.Dataset) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.example_count == 1396 + assert response.etag == "etag_value" + + +def test_get_dataset_rest_required_fields(request_type=service.GetDatasetRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_dataset._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_dataset._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" -def test_transport_grpc_default(): - # A client should use the gRPC transport by default. client = AutoMlClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - assert isinstance( - client.transport, - transports.AutoMlGrpcTransport, - ) + request = request_type(**request_init) + # Designate an appropriate value for the returned response. + return_value = dataset.Dataset() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result -def test_auto_ml_base_transport_error(): - # Passing both a credentials object and credentials_file should raise an error - with pytest.raises(core_exceptions.DuplicateCredentialArgs): - transport = transports.AutoMlTransport( - credentials=ga_credentials.AnonymousCredentials(), - credentials_file="credentials.json", - ) + response_value = Response() + response_value.status_code = 200 + pb_return_value = dataset.Dataset.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) -def test_auto_ml_base_transport(): - # Instantiate the base transport. - with mock.patch( - "google.cloud.automl_v1.services.auto_ml.transports.AutoMlTransport.__init__" - ) as Transport: - Transport.return_value = None - transport = transports.AutoMlTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value - # Every method on the transport should just blindly - # raise NotImplementedError. - methods = ( - "create_dataset", - "get_dataset", - "list_datasets", - "update_dataset", - "delete_dataset", - "import_data", - "export_data", - "get_annotation_spec", - "create_model", - "get_model", - "list_models", - "delete_model", - "update_model", - "deploy_model", - "undeploy_model", - "export_model", - "get_model_evaluation", - "list_model_evaluations", - ) - for method in methods: - with pytest.raises(NotImplementedError): - getattr(transport, method)(request=object()) + response = client.get_dataset(request) - with pytest.raises(NotImplementedError): - transport.close() + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params - # Additionally, the LRO client (a property) should - # also raise NotImplementedError - with pytest.raises(NotImplementedError): - transport.operations_client - # Catch all for all remaining methods and properties - remainder = [ - "kind", - ] - for r in remainder: - with pytest.raises(NotImplementedError): - getattr(transport, r)() +def test_get_dataset_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + unset_fields = transport.get_dataset._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) -def test_auto_ml_base_transport_with_credentials_file(): - # Instantiate the base transport with a credentials file + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_dataset_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) with mock.patch.object( - google.auth, "load_credentials_from_file", autospec=True - ) as load_creds, mock.patch( - "google.cloud.automl_v1.services.auto_ml.transports.AutoMlTransport._prep_wrapped_messages" - ) as Transport: - Transport.return_value = None + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_get_dataset" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_get_dataset" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.GetDatasetRequest.pb(service.GetDatasetRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = dataset.Dataset.to_json(dataset.Dataset()) + + request = service.GetDatasetRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = dataset.Dataset() + + client.get_dataset( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_get_dataset_rest_bad_request( + transport: str = "rest", request_type=service.GetDatasetRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.get_dataset(request) + + +def test_get_dataset_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = dataset.Dataset() + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = dataset.Dataset.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.get_dataset(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/datasets/*}" % client.transport._host, + args[1], + ) + + +def test_get_dataset_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_dataset( + service.GetDatasetRequest(), + name="name_value", + ) + + +def test_get_dataset_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.ListDatasetsRequest, + dict, + ], +) +def test_list_datasets_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = service.ListDatasetsResponse( + next_page_token="next_page_token_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = service.ListDatasetsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.list_datasets(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListDatasetsPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_datasets_rest_required_fields(request_type=service.ListDatasetsRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_datasets._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_datasets._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = service.ListDatasetsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = service.ListDatasetsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.list_datasets(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_datasets_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_datasets._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_datasets_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_list_datasets" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_list_datasets" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.ListDatasetsRequest.pb(service.ListDatasetsRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = service.ListDatasetsResponse.to_json( + service.ListDatasetsResponse() + ) + + request = service.ListDatasetsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = service.ListDatasetsResponse() + + client.list_datasets( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_list_datasets_rest_bad_request( + transport: str = "rest", request_type=service.ListDatasetsRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_datasets(request) + + +def test_list_datasets_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = service.ListDatasetsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = service.ListDatasetsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.list_datasets(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/datasets" % client.transport._host, + args[1], + ) + + +def test_list_datasets_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_datasets( + service.ListDatasetsRequest(), + parent="parent_value", + ) + + +def test_list_datasets_rest_pager(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + service.ListDatasetsResponse( + datasets=[ + dataset.Dataset(), + dataset.Dataset(), + dataset.Dataset(), + ], + next_page_token="abc", + ), + service.ListDatasetsResponse( + datasets=[], + next_page_token="def", + ), + service.ListDatasetsResponse( + datasets=[ + dataset.Dataset(), + ], + next_page_token="ghi", + ), + service.ListDatasetsResponse( + datasets=[ + dataset.Dataset(), + dataset.Dataset(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(service.ListDatasetsResponse.to_json(x) for x in response) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = {"parent": "projects/sample1/locations/sample2"} + + pager = client.list_datasets(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, dataset.Dataset) for i in results) + + pages = list(client.list_datasets(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + service.UpdateDatasetRequest, + dict, + ], +) +def test_update_dataset_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "dataset": {"name": "projects/sample1/locations/sample2/datasets/sample3"} + } + request_init["dataset"] = { + "translation_dataset_metadata": { + "source_language_code": "source_language_code_value", + "target_language_code": "target_language_code_value", + }, + "image_classification_dataset_metadata": {"classification_type": 1}, + "text_classification_dataset_metadata": {"classification_type": 1}, + "image_object_detection_dataset_metadata": {}, + "text_extraction_dataset_metadata": {}, + "text_sentiment_dataset_metadata": {"sentiment_max": 1404}, + "name": "projects/sample1/locations/sample2/datasets/sample3", + "display_name": "display_name_value", + "description": "description_value", + "example_count": 1396, + "create_time": {"seconds": 751, "nanos": 543}, + "etag": "etag_value", + "labels": {}, + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = gca_dataset.Dataset( + name="name_value", + display_name="display_name_value", + description="description_value", + example_count=1396, + etag="etag_value", + translation_dataset_metadata=translation.TranslationDatasetMetadata( + source_language_code="source_language_code_value" + ), + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = gca_dataset.Dataset.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.update_dataset(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, gca_dataset.Dataset) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.example_count == 1396 + assert response.etag == "etag_value" + + +def test_update_dataset_rest_required_fields(request_type=service.UpdateDatasetRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_dataset._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_dataset._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("update_mask",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = gca_dataset.Dataset() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "patch", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = gca_dataset.Dataset.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.update_dataset(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_update_dataset_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.update_dataset._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("updateMask",)) + & set( + ( + "dataset", + "updateMask", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_update_dataset_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_update_dataset" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_update_dataset" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.UpdateDatasetRequest.pb(service.UpdateDatasetRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = gca_dataset.Dataset.to_json(gca_dataset.Dataset()) + + request = service.UpdateDatasetRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = gca_dataset.Dataset() + + client.update_dataset( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_update_dataset_rest_bad_request( + transport: str = "rest", request_type=service.UpdateDatasetRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "dataset": {"name": "projects/sample1/locations/sample2/datasets/sample3"} + } + request_init["dataset"] = { + "translation_dataset_metadata": { + "source_language_code": "source_language_code_value", + "target_language_code": "target_language_code_value", + }, + "image_classification_dataset_metadata": {"classification_type": 1}, + "text_classification_dataset_metadata": {"classification_type": 1}, + "image_object_detection_dataset_metadata": {}, + "text_extraction_dataset_metadata": {}, + "text_sentiment_dataset_metadata": {"sentiment_max": 1404}, + "name": "projects/sample1/locations/sample2/datasets/sample3", + "display_name": "display_name_value", + "description": "description_value", + "example_count": 1396, + "create_time": {"seconds": 751, "nanos": 543}, + "etag": "etag_value", + "labels": {}, + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.update_dataset(request) + + +def test_update_dataset_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = gca_dataset.Dataset() + + # get arguments that satisfy an http rule for this method + sample_request = { + "dataset": {"name": "projects/sample1/locations/sample2/datasets/sample3"} + } + + # get truthy value for each flattened field + mock_args = dict( + dataset=gca_dataset.Dataset( + translation_dataset_metadata=translation.TranslationDatasetMetadata( + source_language_code="source_language_code_value" + ) + ), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = gca_dataset.Dataset.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.update_dataset(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{dataset.name=projects/*/locations/*/datasets/*}" + % client.transport._host, + args[1], + ) + + +def test_update_dataset_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_dataset( + service.UpdateDatasetRequest(), + dataset=gca_dataset.Dataset( + translation_dataset_metadata=translation.TranslationDatasetMetadata( + source_language_code="source_language_code_value" + ) + ), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + +def test_update_dataset_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.DeleteDatasetRequest, + dict, + ], +) +def test_delete_dataset_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.delete_dataset(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_delete_dataset_rest_required_fields(request_type=service.DeleteDatasetRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_dataset._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_dataset._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "delete", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.delete_dataset(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_dataset_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_dataset._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_delete_dataset_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.AutoMlRestInterceptor, "post_delete_dataset" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_delete_dataset" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.DeleteDatasetRequest.pb(service.DeleteDatasetRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = service.DeleteDatasetRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.delete_dataset( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_delete_dataset_rest_bad_request( + transport: str = "rest", request_type=service.DeleteDatasetRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.delete_dataset(request) + + +def test_delete_dataset_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.delete_dataset(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/datasets/*}" % client.transport._host, + args[1], + ) + + +def test_delete_dataset_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_dataset( + service.DeleteDatasetRequest(), + name="name_value", + ) + + +def test_delete_dataset_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.ImportDataRequest, + dict, + ], +) +def test_import_data_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.import_data(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_import_data_rest_required_fields(request_type=service.ImportDataRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).import_data._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).import_data._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.import_data(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_import_data_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.import_data._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(()) + & set( + ( + "name", + "inputConfig", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_import_data_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.AutoMlRestInterceptor, "post_import_data" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_import_data" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.ImportDataRequest.pb(service.ImportDataRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = service.ImportDataRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.import_data( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_import_data_rest_bad_request( + transport: str = "rest", request_type=service.ImportDataRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.import_data(request) + + +def test_import_data_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + input_config=io.InputConfig( + gcs_source=io.GcsSource(input_uris=["input_uris_value"]) + ), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.import_data(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/datasets/*}:importData" + % client.transport._host, + args[1], + ) + + +def test_import_data_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.import_data( + service.ImportDataRequest(), + name="name_value", + input_config=io.InputConfig( + gcs_source=io.GcsSource(input_uris=["input_uris_value"]) + ), + ) + + +def test_import_data_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.ExportDataRequest, + dict, + ], +) +def test_export_data_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.export_data(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_export_data_rest_required_fields(request_type=service.ExportDataRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).export_data._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).export_data._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.export_data(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_export_data_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.export_data._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(()) + & set( + ( + "name", + "outputConfig", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_export_data_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.AutoMlRestInterceptor, "post_export_data" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_export_data" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.ExportDataRequest.pb(service.ExportDataRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = service.ExportDataRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.export_data( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_export_data_rest_bad_request( + transport: str = "rest", request_type=service.ExportDataRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.export_data(request) + + +def test_export_data_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + output_config=io.OutputConfig( + gcs_destination=io.GcsDestination( + output_uri_prefix="output_uri_prefix_value" + ) + ), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.export_data(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/datasets/*}:exportData" + % client.transport._host, + args[1], + ) + + +def test_export_data_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.export_data( + service.ExportDataRequest(), + name="name_value", + output_config=io.OutputConfig( + gcs_destination=io.GcsDestination( + output_uri_prefix="output_uri_prefix_value" + ) + ), + ) + + +def test_export_data_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.GetAnnotationSpecRequest, + dict, + ], +) +def test_get_annotation_spec_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/datasets/sample3/annotationSpecs/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = annotation_spec.AnnotationSpec( + name="name_value", + display_name="display_name_value", + example_count=1396, + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = annotation_spec.AnnotationSpec.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.get_annotation_spec(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, annotation_spec.AnnotationSpec) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.example_count == 1396 + + +def test_get_annotation_spec_rest_required_fields( + request_type=service.GetAnnotationSpecRequest, +): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_annotation_spec._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_annotation_spec._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = annotation_spec.AnnotationSpec() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = annotation_spec.AnnotationSpec.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.get_annotation_spec(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_annotation_spec_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_annotation_spec._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_annotation_spec_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_get_annotation_spec" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_get_annotation_spec" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.GetAnnotationSpecRequest.pb( + service.GetAnnotationSpecRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = annotation_spec.AnnotationSpec.to_json( + annotation_spec.AnnotationSpec() + ) + + request = service.GetAnnotationSpecRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = annotation_spec.AnnotationSpec() + + client.get_annotation_spec( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_get_annotation_spec_rest_bad_request( + transport: str = "rest", request_type=service.GetAnnotationSpecRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/datasets/sample3/annotationSpecs/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.get_annotation_spec(request) + + +def test_get_annotation_spec_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = annotation_spec.AnnotationSpec() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/datasets/sample3/annotationSpecs/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = annotation_spec.AnnotationSpec.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.get_annotation_spec(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/datasets/*/annotationSpecs/*}" + % client.transport._host, + args[1], + ) + + +def test_get_annotation_spec_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_annotation_spec( + service.GetAnnotationSpecRequest(), + name="name_value", + ) + + +def test_get_annotation_spec_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.CreateModelRequest, + dict, + ], +) +def test_create_model_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["model"] = { + "translation_model_metadata": { + "base_model": "base_model_value", + "source_language_code": "source_language_code_value", + "target_language_code": "target_language_code_value", + }, + "image_classification_model_metadata": { + "base_model_id": "base_model_id_value", + "train_budget_milli_node_hours": 3075, + "train_cost_milli_node_hours": 2881, + "stop_reason": "stop_reason_value", + "model_type": "model_type_value", + "node_qps": 0.857, + "node_count": 1070, + }, + "text_classification_model_metadata": {"classification_type": 1}, + "image_object_detection_model_metadata": { + "model_type": "model_type_value", + "node_count": 1070, + "node_qps": 0.857, + "stop_reason": "stop_reason_value", + "train_budget_milli_node_hours": 3075, + "train_cost_milli_node_hours": 2881, + }, + "text_extraction_model_metadata": {}, + "text_sentiment_model_metadata": {}, + "name": "name_value", + "display_name": "display_name_value", + "dataset_id": "dataset_id_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "deployment_state": 1, + "etag": "etag_value", + "labels": {}, + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.create_model(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_create_model_rest_required_fields(request_type=service.CreateModelRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.create_model(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_model_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_model._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(()) + & set( + ( + "parent", + "model", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_model_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.AutoMlRestInterceptor, "post_create_model" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_create_model" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.CreateModelRequest.pb(service.CreateModelRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = service.CreateModelRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.create_model( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_create_model_rest_bad_request( + transport: str = "rest", request_type=service.CreateModelRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["model"] = { + "translation_model_metadata": { + "base_model": "base_model_value", + "source_language_code": "source_language_code_value", + "target_language_code": "target_language_code_value", + }, + "image_classification_model_metadata": { + "base_model_id": "base_model_id_value", + "train_budget_milli_node_hours": 3075, + "train_cost_milli_node_hours": 2881, + "stop_reason": "stop_reason_value", + "model_type": "model_type_value", + "node_qps": 0.857, + "node_count": 1070, + }, + "text_classification_model_metadata": {"classification_type": 1}, + "image_object_detection_model_metadata": { + "model_type": "model_type_value", + "node_count": 1070, + "node_qps": 0.857, + "stop_reason": "stop_reason_value", + "train_budget_milli_node_hours": 3075, + "train_cost_milli_node_hours": 2881, + }, + "text_extraction_model_metadata": {}, + "text_sentiment_model_metadata": {}, + "name": "name_value", + "display_name": "display_name_value", + "dataset_id": "dataset_id_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "deployment_state": 1, + "etag": "etag_value", + "labels": {}, + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.create_model(request) + + +def test_create_model_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + model=gca_model.Model( + translation_model_metadata=translation.TranslationModelMetadata( + base_model="base_model_value" + ) + ), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.create_model(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/models" % client.transport._host, + args[1], + ) + + +def test_create_model_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_model( + service.CreateModelRequest(), + parent="parent_value", + model=gca_model.Model( + translation_model_metadata=translation.TranslationModelMetadata( + base_model="base_model_value" + ) + ), + ) + + +def test_create_model_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.GetModelRequest, + dict, + ], +) +def test_get_model_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = model.Model( + name="name_value", + display_name="display_name_value", + dataset_id="dataset_id_value", + deployment_state=model.Model.DeploymentState.DEPLOYED, + etag="etag_value", + translation_model_metadata=translation.TranslationModelMetadata( + base_model="base_model_value" + ), + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = model.Model.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.get_model(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, model.Model) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.dataset_id == "dataset_id_value" + assert response.deployment_state == model.Model.DeploymentState.DEPLOYED + assert response.etag == "etag_value" + + +def test_get_model_rest_required_fields(request_type=service.GetModelRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = model.Model() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = model.Model.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.get_model(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_model_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_model._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_model_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_get_model" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_get_model" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.GetModelRequest.pb(service.GetModelRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = model.Model.to_json(model.Model()) + + request = service.GetModelRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = model.Model() + + client.get_model( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_get_model_rest_bad_request( + transport: str = "rest", request_type=service.GetModelRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.get_model(request) + + +def test_get_model_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = model.Model() + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/models/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = model.Model.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.get_model(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/models/*}" % client.transport._host, + args[1], + ) + + +def test_get_model_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_model( + service.GetModelRequest(), + name="name_value", + ) + + +def test_get_model_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.ListModelsRequest, + dict, + ], +) +def test_list_models_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = service.ListModelsResponse( + next_page_token="next_page_token_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = service.ListModelsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.list_models(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListModelsPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_models_rest_required_fields(request_type=service.ListModelsRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_models._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_models._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = service.ListModelsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = service.ListModelsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.list_models(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_models_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_models._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_models_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_list_models" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_list_models" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.ListModelsRequest.pb(service.ListModelsRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = service.ListModelsResponse.to_json( + service.ListModelsResponse() + ) + + request = service.ListModelsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = service.ListModelsResponse() + + client.list_models( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_list_models_rest_bad_request( + transport: str = "rest", request_type=service.ListModelsRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_models(request) + + +def test_list_models_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = service.ListModelsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = service.ListModelsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.list_models(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/models" % client.transport._host, + args[1], + ) + + +def test_list_models_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_models( + service.ListModelsRequest(), + parent="parent_value", + ) + + +def test_list_models_rest_pager(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + service.ListModelsResponse( + model=[ + model.Model(), + model.Model(), + model.Model(), + ], + next_page_token="abc", + ), + service.ListModelsResponse( + model=[], + next_page_token="def", + ), + service.ListModelsResponse( + model=[ + model.Model(), + ], + next_page_token="ghi", + ), + service.ListModelsResponse( + model=[ + model.Model(), + model.Model(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(service.ListModelsResponse.to_json(x) for x in response) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = {"parent": "projects/sample1/locations/sample2"} + + pager = client.list_models(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, model.Model) for i in results) + + pages = list(client.list_models(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + service.DeleteModelRequest, + dict, + ], +) +def test_delete_model_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.delete_model(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_delete_model_rest_required_fields(request_type=service.DeleteModelRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "delete", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.delete_model(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_model_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_model._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_delete_model_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.AutoMlRestInterceptor, "post_delete_model" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_delete_model" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.DeleteModelRequest.pb(service.DeleteModelRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = service.DeleteModelRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.delete_model( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_delete_model_rest_bad_request( + transport: str = "rest", request_type=service.DeleteModelRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.delete_model(request) + + +def test_delete_model_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/models/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.delete_model(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/models/*}" % client.transport._host, + args[1], + ) + + +def test_delete_model_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_model( + service.DeleteModelRequest(), + name="name_value", + ) + + +def test_delete_model_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.UpdateModelRequest, + dict, + ], +) +def test_update_model_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "model": {"name": "projects/sample1/locations/sample2/models/sample3"} + } + request_init["model"] = { + "translation_model_metadata": { + "base_model": "base_model_value", + "source_language_code": "source_language_code_value", + "target_language_code": "target_language_code_value", + }, + "image_classification_model_metadata": { + "base_model_id": "base_model_id_value", + "train_budget_milli_node_hours": 3075, + "train_cost_milli_node_hours": 2881, + "stop_reason": "stop_reason_value", + "model_type": "model_type_value", + "node_qps": 0.857, + "node_count": 1070, + }, + "text_classification_model_metadata": {"classification_type": 1}, + "image_object_detection_model_metadata": { + "model_type": "model_type_value", + "node_count": 1070, + "node_qps": 0.857, + "stop_reason": "stop_reason_value", + "train_budget_milli_node_hours": 3075, + "train_cost_milli_node_hours": 2881, + }, + "text_extraction_model_metadata": {}, + "text_sentiment_model_metadata": {}, + "name": "projects/sample1/locations/sample2/models/sample3", + "display_name": "display_name_value", + "dataset_id": "dataset_id_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "deployment_state": 1, + "etag": "etag_value", + "labels": {}, + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = gca_model.Model( + name="name_value", + display_name="display_name_value", + dataset_id="dataset_id_value", + deployment_state=gca_model.Model.DeploymentState.DEPLOYED, + etag="etag_value", + translation_model_metadata=translation.TranslationModelMetadata( + base_model="base_model_value" + ), + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = gca_model.Model.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.update_model(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, gca_model.Model) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.dataset_id == "dataset_id_value" + assert response.deployment_state == gca_model.Model.DeploymentState.DEPLOYED + assert response.etag == "etag_value" + + +def test_update_model_rest_required_fields(request_type=service.UpdateModelRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_model._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("update_mask",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = gca_model.Model() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "patch", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = gca_model.Model.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.update_model(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_update_model_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.update_model._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("updateMask",)) + & set( + ( + "model", + "updateMask", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_update_model_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_update_model" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_update_model" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.UpdateModelRequest.pb(service.UpdateModelRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = gca_model.Model.to_json(gca_model.Model()) + + request = service.UpdateModelRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = gca_model.Model() + + client.update_model( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_update_model_rest_bad_request( + transport: str = "rest", request_type=service.UpdateModelRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "model": {"name": "projects/sample1/locations/sample2/models/sample3"} + } + request_init["model"] = { + "translation_model_metadata": { + "base_model": "base_model_value", + "source_language_code": "source_language_code_value", + "target_language_code": "target_language_code_value", + }, + "image_classification_model_metadata": { + "base_model_id": "base_model_id_value", + "train_budget_milli_node_hours": 3075, + "train_cost_milli_node_hours": 2881, + "stop_reason": "stop_reason_value", + "model_type": "model_type_value", + "node_qps": 0.857, + "node_count": 1070, + }, + "text_classification_model_metadata": {"classification_type": 1}, + "image_object_detection_model_metadata": { + "model_type": "model_type_value", + "node_count": 1070, + "node_qps": 0.857, + "stop_reason": "stop_reason_value", + "train_budget_milli_node_hours": 3075, + "train_cost_milli_node_hours": 2881, + }, + "text_extraction_model_metadata": {}, + "text_sentiment_model_metadata": {}, + "name": "projects/sample1/locations/sample2/models/sample3", + "display_name": "display_name_value", + "dataset_id": "dataset_id_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "deployment_state": 1, + "etag": "etag_value", + "labels": {}, + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.update_model(request) + + +def test_update_model_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = gca_model.Model() + + # get arguments that satisfy an http rule for this method + sample_request = { + "model": {"name": "projects/sample1/locations/sample2/models/sample3"} + } + + # get truthy value for each flattened field + mock_args = dict( + model=gca_model.Model( + translation_model_metadata=translation.TranslationModelMetadata( + base_model="base_model_value" + ) + ), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = gca_model.Model.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.update_model(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{model.name=projects/*/locations/*/models/*}" + % client.transport._host, + args[1], + ) + + +def test_update_model_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_model( + service.UpdateModelRequest(), + model=gca_model.Model( + translation_model_metadata=translation.TranslationModelMetadata( + base_model="base_model_value" + ) + ), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + +def test_update_model_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.DeployModelRequest, + dict, + ], +) +def test_deploy_model_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.deploy_model(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_deploy_model_rest_required_fields(request_type=service.DeployModelRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).deploy_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).deploy_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.deploy_model(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_deploy_model_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.deploy_model._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_deploy_model_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.AutoMlRestInterceptor, "post_deploy_model" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_deploy_model" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.DeployModelRequest.pb(service.DeployModelRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = service.DeployModelRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.deploy_model( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_deploy_model_rest_bad_request( + transport: str = "rest", request_type=service.DeployModelRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.deploy_model(request) + + +def test_deploy_model_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/models/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.deploy_model(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/models/*}:deploy" + % client.transport._host, + args[1], + ) + + +def test_deploy_model_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.deploy_model( + service.DeployModelRequest(), + name="name_value", + ) + + +def test_deploy_model_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.UndeployModelRequest, + dict, + ], +) +def test_undeploy_model_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.undeploy_model(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_undeploy_model_rest_required_fields(request_type=service.UndeployModelRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).undeploy_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).undeploy_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.undeploy_model(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_undeploy_model_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.undeploy_model._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_undeploy_model_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.AutoMlRestInterceptor, "post_undeploy_model" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_undeploy_model" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.UndeployModelRequest.pb(service.UndeployModelRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = service.UndeployModelRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.undeploy_model( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_undeploy_model_rest_bad_request( + transport: str = "rest", request_type=service.UndeployModelRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.undeploy_model(request) + + +def test_undeploy_model_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/models/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.undeploy_model(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/models/*}:undeploy" + % client.transport._host, + args[1], + ) + + +def test_undeploy_model_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.undeploy_model( + service.UndeployModelRequest(), + name="name_value", + ) + + +def test_undeploy_model_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.ExportModelRequest, + dict, + ], +) +def test_export_model_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.export_model(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_export_model_rest_required_fields(request_type=service.ExportModelRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).export_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).export_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.export_model(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_export_model_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.export_model._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(()) + & set( + ( + "name", + "outputConfig", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_export_model_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.AutoMlRestInterceptor, "post_export_model" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_export_model" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.ExportModelRequest.pb(service.ExportModelRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = service.ExportModelRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.export_model( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_export_model_rest_bad_request( + transport: str = "rest", request_type=service.ExportModelRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.export_model(request) + + +def test_export_model_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/models/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + output_config=io.ModelExportOutputConfig( + gcs_destination=io.GcsDestination( + output_uri_prefix="output_uri_prefix_value" + ) + ), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.export_model(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/models/*}:export" + % client.transport._host, + args[1], + ) + + +def test_export_model_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.export_model( + service.ExportModelRequest(), + name="name_value", + output_config=io.ModelExportOutputConfig( + gcs_destination=io.GcsDestination( + output_uri_prefix="output_uri_prefix_value" + ) + ), + ) + + +def test_export_model_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.GetModelEvaluationRequest, + dict, + ], +) +def test_get_model_evaluation_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/models/sample3/modelEvaluations/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = model_evaluation.ModelEvaluation( + name="name_value", + annotation_spec_id="annotation_spec_id_value", + display_name="display_name_value", + evaluated_example_count=2446, + classification_evaluation_metrics=classification.ClassificationEvaluationMetrics( + au_prc=0.634 + ), + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = model_evaluation.ModelEvaluation.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.get_model_evaluation(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, model_evaluation.ModelEvaluation) + assert response.name == "name_value" + assert response.annotation_spec_id == "annotation_spec_id_value" + assert response.display_name == "display_name_value" + assert response.evaluated_example_count == 2446 + + +def test_get_model_evaluation_rest_required_fields( + request_type=service.GetModelEvaluationRequest, +): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_model_evaluation._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_model_evaluation._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = model_evaluation.ModelEvaluation() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = model_evaluation.ModelEvaluation.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.get_model_evaluation(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_model_evaluation_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_model_evaluation._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_model_evaluation_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_get_model_evaluation" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_get_model_evaluation" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.GetModelEvaluationRequest.pb( + service.GetModelEvaluationRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = model_evaluation.ModelEvaluation.to_json( + model_evaluation.ModelEvaluation() + ) + + request = service.GetModelEvaluationRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = model_evaluation.ModelEvaluation() + + client.get_model_evaluation( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_get_model_evaluation_rest_bad_request( + transport: str = "rest", request_type=service.GetModelEvaluationRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/models/sample3/modelEvaluations/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.get_model_evaluation(request) + + +def test_get_model_evaluation_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = model_evaluation.ModelEvaluation() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/models/sample3/modelEvaluations/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = model_evaluation.ModelEvaluation.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.get_model_evaluation(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/models/*/modelEvaluations/*}" + % client.transport._host, + args[1], + ) + + +def test_get_model_evaluation_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_model_evaluation( + service.GetModelEvaluationRequest(), + name="name_value", + ) + + +def test_get_model_evaluation_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.ListModelEvaluationsRequest, + dict, + ], +) +def test_list_model_evaluations_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = service.ListModelEvaluationsResponse( + next_page_token="next_page_token_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = service.ListModelEvaluationsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.list_model_evaluations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListModelEvaluationsPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_model_evaluations_rest_required_fields( + request_type=service.ListModelEvaluationsRequest, +): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["parent"] = "" + request_init["filter"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + assert "filter" not in jsonified_request + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_model_evaluations._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + assert "filter" in jsonified_request + assert jsonified_request["filter"] == request_init["filter"] + + jsonified_request["parent"] = "parent_value" + jsonified_request["filter"] = "filter_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_model_evaluations._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + assert "filter" in jsonified_request + assert jsonified_request["filter"] == "filter_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = service.ListModelEvaluationsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = service.ListModelEvaluationsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.list_model_evaluations(request) + + expected_params = [ + ( + "filter", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_model_evaluations_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_model_evaluations._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "pageSize", + "pageToken", + ) + ) + & set( + ( + "parent", + "filter", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_model_evaluations_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_list_model_evaluations" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_list_model_evaluations" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.ListModelEvaluationsRequest.pb( + service.ListModelEvaluationsRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = service.ListModelEvaluationsResponse.to_json( + service.ListModelEvaluationsResponse() + ) + + request = service.ListModelEvaluationsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = service.ListModelEvaluationsResponse() + + client.list_model_evaluations( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_list_model_evaluations_rest_bad_request( + transport: str = "rest", request_type=service.ListModelEvaluationsRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_model_evaluations(request) + + +def test_list_model_evaluations_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = service.ListModelEvaluationsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2/models/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + filter="filter_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = service.ListModelEvaluationsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.list_model_evaluations(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*/models/*}/modelEvaluations" + % client.transport._host, + args[1], + ) + + +def test_list_model_evaluations_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_model_evaluations( + service.ListModelEvaluationsRequest(), + parent="parent_value", + filter="filter_value", + ) + + +def test_list_model_evaluations_rest_pager(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + service.ListModelEvaluationsResponse( + model_evaluation=[ + model_evaluation.ModelEvaluation(), + model_evaluation.ModelEvaluation(), + model_evaluation.ModelEvaluation(), + ], + next_page_token="abc", + ), + service.ListModelEvaluationsResponse( + model_evaluation=[], + next_page_token="def", + ), + service.ListModelEvaluationsResponse( + model_evaluation=[ + model_evaluation.ModelEvaluation(), + ], + next_page_token="ghi", + ), + service.ListModelEvaluationsResponse( + model_evaluation=[ + model_evaluation.ModelEvaluation(), + model_evaluation.ModelEvaluation(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + service.ListModelEvaluationsResponse.to_json(x) for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = {"parent": "projects/sample1/locations/sample2/models/sample3"} + + pager = client.list_model_evaluations(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, model_evaluation.ModelEvaluation) for i in results) + + pages = list(client.list_model_evaluations(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +def test_credentials_transport_error(): + # It is an error to provide credentials and a transport instance. + transport = transports.AutoMlGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # It is an error to provide a credentials file and a transport instance. + transport = transports.AutoMlGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = AutoMlClient( + client_options={"credentials_file": "credentials.json"}, + transport=transport, + ) + + # It is an error to provide an api_key and a transport instance. + transport = transports.AutoMlGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = AutoMlClient( + client_options=options, + transport=transport, + ) + + # It is an error to provide an api_key and a credential. + options = mock.Mock() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = AutoMlClient( + client_options=options, credentials=ga_credentials.AnonymousCredentials() + ) + + # It is an error to provide scopes and a transport instance. + transport = transports.AutoMlGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = AutoMlClient( + client_options={"scopes": ["1", "2"]}, + transport=transport, + ) + + +def test_transport_instance(): + # A client may be instantiated with a custom transport instance. + transport = transports.AutoMlGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + client = AutoMlClient(transport=transport) + assert client.transport is transport + + +def test_transport_get_channel(): + # A client may be instantiated with a custom transport instance. + transport = transports.AutoMlGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + transport = transports.AutoMlGrpcAsyncIOTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.AutoMlGrpcTransport, + transports.AutoMlGrpcAsyncIOTransport, + transports.AutoMlRestTransport, + ], +) +def test_transport_adc(transport_class): + # Test default credentials are used if not provided. + with mock.patch.object(google.auth, "default") as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class() + adc.assert_called_once() + + +@pytest.mark.parametrize( + "transport_name", + [ + "grpc", + "rest", + ], +) +def test_transport_kind(transport_name): + transport = AutoMlClient.get_transport_class(transport_name)( + credentials=ga_credentials.AnonymousCredentials(), + ) + assert transport.kind == transport_name + + +def test_transport_grpc_default(): + # A client should use the gRPC transport by default. + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + assert isinstance( + client.transport, + transports.AutoMlGrpcTransport, + ) + + +def test_auto_ml_base_transport_error(): + # Passing both a credentials object and credentials_file should raise an error + with pytest.raises(core_exceptions.DuplicateCredentialArgs): + transport = transports.AutoMlTransport( + credentials=ga_credentials.AnonymousCredentials(), + credentials_file="credentials.json", + ) + + +def test_auto_ml_base_transport(): + # Instantiate the base transport. + with mock.patch( + "google.cloud.automl_v1.services.auto_ml.transports.AutoMlTransport.__init__" + ) as Transport: + Transport.return_value = None + transport = transports.AutoMlTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Every method on the transport should just blindly + # raise NotImplementedError. + methods = ( + "create_dataset", + "get_dataset", + "list_datasets", + "update_dataset", + "delete_dataset", + "import_data", + "export_data", + "get_annotation_spec", + "create_model", + "get_model", + "list_models", + "delete_model", + "update_model", + "deploy_model", + "undeploy_model", + "export_model", + "get_model_evaluation", + "list_model_evaluations", + ) + for method in methods: + with pytest.raises(NotImplementedError): + getattr(transport, method)(request=object()) + + with pytest.raises(NotImplementedError): + transport.close() + + # Additionally, the LRO client (a property) should + # also raise NotImplementedError + with pytest.raises(NotImplementedError): + transport.operations_client + + # Catch all for all remaining methods and properties + remainder = [ + "kind", + ] + for r in remainder: + with pytest.raises(NotImplementedError): + getattr(transport, r)() + + +def test_auto_ml_base_transport_with_credentials_file(): + # Instantiate the base transport with a credentials file + with mock.patch.object( + google.auth, "load_credentials_from_file", autospec=True + ) as load_creds, mock.patch( + "google.cloud.automl_v1.services.auto_ml.transports.AutoMlTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None load_creds.return_value = (ga_credentials.AnonymousCredentials(), None) transport = transports.AutoMlTransport( credentials_file="credentials.json", @@ -5960,6 +11309,7 @@ def test_auto_ml_transport_auth_adc(transport_class): [ transports.AutoMlGrpcTransport, transports.AutoMlGrpcAsyncIOTransport, + transports.AutoMlRestTransport, ], ) def test_auto_ml_transport_auth_gdch_credentials(transport_class): @@ -6054,11 +11404,40 @@ def test_auto_ml_grpc_transport_client_cert_source_for_mtls(transport_class): ) +def test_auto_ml_http_transport_client_cert_source_for_mtls(): + cred = ga_credentials.AnonymousCredentials() + with mock.patch( + "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" + ) as mock_configure_mtls_channel: + transports.AutoMlRestTransport( + credentials=cred, client_cert_source_for_mtls=client_cert_source_callback + ) + mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) + + +def test_auto_ml_rest_lro_client(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + transport = client.transport + + # Ensure that we have a api-core operations client. + assert isinstance( + transport.operations_client, + operations_v1.AbstractOperationsClient, + ) + + # Ensure that subsequent calls to the property send the exact same object. + assert transport.operations_client is transport.operations_client + + @pytest.mark.parametrize( "transport_name", [ "grpc", "grpc_asyncio", + "rest", ], ) def test_auto_ml_host_no_port(transport_name): @@ -6069,7 +11448,11 @@ def test_auto_ml_host_no_port(transport_name): ), transport=transport_name, ) - assert client.transport._host == ("automl.googleapis.com:443") + assert client.transport._host == ( + "automl.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://automl.googleapis.com" + ) @pytest.mark.parametrize( @@ -6077,6 +11460,7 @@ def test_auto_ml_host_no_port(transport_name): [ "grpc", "grpc_asyncio", + "rest", ], ) def test_auto_ml_host_with_port(transport_name): @@ -6087,7 +11471,84 @@ def test_auto_ml_host_with_port(transport_name): ), transport=transport_name, ) - assert client.transport._host == ("automl.googleapis.com:8000") + assert client.transport._host == ( + "automl.googleapis.com:8000" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://automl.googleapis.com:8000" + ) + + +@pytest.mark.parametrize( + "transport_name", + [ + "rest", + ], +) +def test_auto_ml_client_transport_session_collision(transport_name): + creds1 = ga_credentials.AnonymousCredentials() + creds2 = ga_credentials.AnonymousCredentials() + client1 = AutoMlClient( + credentials=creds1, + transport=transport_name, + ) + client2 = AutoMlClient( + credentials=creds2, + transport=transport_name, + ) + session1 = client1.transport.create_dataset._session + session2 = client2.transport.create_dataset._session + assert session1 != session2 + session1 = client1.transport.get_dataset._session + session2 = client2.transport.get_dataset._session + assert session1 != session2 + session1 = client1.transport.list_datasets._session + session2 = client2.transport.list_datasets._session + assert session1 != session2 + session1 = client1.transport.update_dataset._session + session2 = client2.transport.update_dataset._session + assert session1 != session2 + session1 = client1.transport.delete_dataset._session + session2 = client2.transport.delete_dataset._session + assert session1 != session2 + session1 = client1.transport.import_data._session + session2 = client2.transport.import_data._session + assert session1 != session2 + session1 = client1.transport.export_data._session + session2 = client2.transport.export_data._session + assert session1 != session2 + session1 = client1.transport.get_annotation_spec._session + session2 = client2.transport.get_annotation_spec._session + assert session1 != session2 + session1 = client1.transport.create_model._session + session2 = client2.transport.create_model._session + assert session1 != session2 + session1 = client1.transport.get_model._session + session2 = client2.transport.get_model._session + assert session1 != session2 + session1 = client1.transport.list_models._session + session2 = client2.transport.list_models._session + assert session1 != session2 + session1 = client1.transport.delete_model._session + session2 = client2.transport.delete_model._session + assert session1 != session2 + session1 = client1.transport.update_model._session + session2 = client2.transport.update_model._session + assert session1 != session2 + session1 = client1.transport.deploy_model._session + session2 = client2.transport.deploy_model._session + assert session1 != session2 + session1 = client1.transport.undeploy_model._session + session2 = client2.transport.undeploy_model._session + assert session1 != session2 + session1 = client1.transport.export_model._session + session2 = client2.transport.export_model._session + assert session1 != session2 + session1 = client1.transport.get_model_evaluation._session + session2 = client2.transport.get_model_evaluation._session + assert session1 != session2 + session1 = client1.transport.list_model_evaluations._session + session2 = client2.transport.list_model_evaluations._session + assert session1 != session2 def test_auto_ml_grpc_transport_channel(): @@ -6498,6 +11959,7 @@ async def test_transport_close_async(): def test_transport_close(): transports = { + "rest": "_session", "grpc": "_grpc_channel", } @@ -6515,6 +11977,7 @@ def test_transport_close(): def test_client_ctx(): transports = [ + "rest", "grpc", ] for transport in transports: diff --git a/tests/unit/gapic/automl_v1/test_prediction_service.py b/tests/unit/gapic/automl_v1/test_prediction_service.py index 8e496724..14bfc61a 100644 --- a/tests/unit/gapic/automl_v1/test_prediction_service.py +++ b/tests/unit/gapic/automl_v1/test_prediction_service.py @@ -24,10 +24,17 @@ import grpc from grpc.experimental import aio +from collections.abc import Iterable +from google.protobuf import json_format +import json import math import pytest from proto.marshal.rules.dates import DurationRule, TimestampRule from proto.marshal.rules import wrappers +from requests import Response +from requests import Request, PreparedRequest +from requests.sessions import Session +from google.protobuf import json_format from google.api_core import client_options from google.api_core import exceptions as core_exceptions @@ -108,6 +115,7 @@ def test__get_default_mtls_endpoint(): [ (PredictionServiceClient, "grpc"), (PredictionServiceAsyncClient, "grpc_asyncio"), + (PredictionServiceClient, "rest"), ], ) def test_prediction_service_client_from_service_account_info( @@ -123,7 +131,11 @@ def test_prediction_service_client_from_service_account_info( assert client.transport._credentials == creds assert isinstance(client, client_class) - assert client.transport._host == ("automl.googleapis.com:443") + assert client.transport._host == ( + "automl.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://automl.googleapis.com" + ) @pytest.mark.parametrize( @@ -131,6 +143,7 @@ def test_prediction_service_client_from_service_account_info( [ (transports.PredictionServiceGrpcTransport, "grpc"), (transports.PredictionServiceGrpcAsyncIOTransport, "grpc_asyncio"), + (transports.PredictionServiceRestTransport, "rest"), ], ) def test_prediction_service_client_service_account_always_use_jwt( @@ -156,6 +169,7 @@ def test_prediction_service_client_service_account_always_use_jwt( [ (PredictionServiceClient, "grpc"), (PredictionServiceAsyncClient, "grpc_asyncio"), + (PredictionServiceClient, "rest"), ], ) def test_prediction_service_client_from_service_account_file( @@ -178,13 +192,18 @@ def test_prediction_service_client_from_service_account_file( assert client.transport._credentials == creds assert isinstance(client, client_class) - assert client.transport._host == ("automl.googleapis.com:443") + assert client.transport._host == ( + "automl.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://automl.googleapis.com" + ) def test_prediction_service_client_get_transport_class(): transport = PredictionServiceClient.get_transport_class() available_transports = [ transports.PredictionServiceGrpcTransport, + transports.PredictionServiceRestTransport, ] assert transport in available_transports @@ -201,6 +220,7 @@ def test_prediction_service_client_get_transport_class(): transports.PredictionServiceGrpcAsyncIOTransport, "grpc_asyncio", ), + (PredictionServiceClient, transports.PredictionServiceRestTransport, "rest"), ], ) @mock.patch.object( @@ -356,6 +376,18 @@ def test_prediction_service_client_client_options( "grpc_asyncio", "false", ), + ( + PredictionServiceClient, + transports.PredictionServiceRestTransport, + "rest", + "true", + ), + ( + PredictionServiceClient, + transports.PredictionServiceRestTransport, + "rest", + "false", + ), ], ) @mock.patch.object( @@ -555,6 +587,7 @@ def test_prediction_service_client_get_mtls_endpoint_and_cert_source(client_clas transports.PredictionServiceGrpcAsyncIOTransport, "grpc_asyncio", ), + (PredictionServiceClient, transports.PredictionServiceRestTransport, "rest"), ], ) def test_prediction_service_client_client_options_scopes( @@ -595,6 +628,12 @@ def test_prediction_service_client_client_options_scopes( "grpc_asyncio", grpc_helpers_async, ), + ( + PredictionServiceClient, + transports.PredictionServiceRestTransport, + "rest", + None, + ), ], ) def test_prediction_service_client_client_options_credentials_file( @@ -1257,6 +1296,577 @@ async def test_batch_predict_flattened_error_async(): ) +@pytest.mark.parametrize( + "request_type", + [ + prediction_service.PredictRequest, + dict, + ], +) +def test_predict_rest(request_type): + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = prediction_service.PredictResponse() + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = prediction_service.PredictResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.predict(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, prediction_service.PredictResponse) + + +def test_predict_rest_required_fields(request_type=prediction_service.PredictRequest): + transport_class = transports.PredictionServiceRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).predict._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).predict._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = prediction_service.PredictResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = prediction_service.PredictResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.predict(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_predict_rest_unset_required_fields(): + transport = transports.PredictionServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.predict._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(()) + & set( + ( + "name", + "payload", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_predict_rest_interceptors(null_interceptor): + transport = transports.PredictionServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.PredictionServiceRestInterceptor(), + ) + client = PredictionServiceClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.PredictionServiceRestInterceptor, "post_predict" + ) as post, mock.patch.object( + transports.PredictionServiceRestInterceptor, "pre_predict" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = prediction_service.PredictRequest.pb( + prediction_service.PredictRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = prediction_service.PredictResponse.to_json( + prediction_service.PredictResponse() + ) + + request = prediction_service.PredictRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = prediction_service.PredictResponse() + + client.predict( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_predict_rest_bad_request( + transport: str = "rest", request_type=prediction_service.PredictRequest +): + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.predict(request) + + +def test_predict_rest_flattened(): + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = prediction_service.PredictResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/models/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + payload=data_items.ExamplePayload( + image=data_items.Image(image_bytes=b"image_bytes_blob") + ), + params={"key_value": "value_value"}, + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = prediction_service.PredictResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.predict(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/models/*}:predict" + % client.transport._host, + args[1], + ) + + +def test_predict_rest_flattened_error(transport: str = "rest"): + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.predict( + prediction_service.PredictRequest(), + name="name_value", + payload=data_items.ExamplePayload( + image=data_items.Image(image_bytes=b"image_bytes_blob") + ), + params={"key_value": "value_value"}, + ) + + +def test_predict_rest_error(): + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + prediction_service.BatchPredictRequest, + dict, + ], +) +def test_batch_predict_rest(request_type): + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.batch_predict(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_batch_predict_rest_required_fields( + request_type=prediction_service.BatchPredictRequest, +): + transport_class = transports.PredictionServiceRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).batch_predict._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).batch_predict._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.batch_predict(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_batch_predict_rest_unset_required_fields(): + transport = transports.PredictionServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.batch_predict._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(()) + & set( + ( + "name", + "inputConfig", + "outputConfig", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_batch_predict_rest_interceptors(null_interceptor): + transport = transports.PredictionServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.PredictionServiceRestInterceptor(), + ) + client = PredictionServiceClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.PredictionServiceRestInterceptor, "post_batch_predict" + ) as post, mock.patch.object( + transports.PredictionServiceRestInterceptor, "pre_batch_predict" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = prediction_service.BatchPredictRequest.pb( + prediction_service.BatchPredictRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = prediction_service.BatchPredictRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.batch_predict( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_batch_predict_rest_bad_request( + transport: str = "rest", request_type=prediction_service.BatchPredictRequest +): + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.batch_predict(request) + + +def test_batch_predict_rest_flattened(): + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/models/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + input_config=io.BatchPredictInputConfig( + gcs_source=io.GcsSource(input_uris=["input_uris_value"]) + ), + output_config=io.BatchPredictOutputConfig( + gcs_destination=io.GcsDestination( + output_uri_prefix="output_uri_prefix_value" + ) + ), + params={"key_value": "value_value"}, + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.batch_predict(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/models/*}:batchPredict" + % client.transport._host, + args[1], + ) + + +def test_batch_predict_rest_flattened_error(transport: str = "rest"): + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.batch_predict( + prediction_service.BatchPredictRequest(), + name="name_value", + input_config=io.BatchPredictInputConfig( + gcs_source=io.GcsSource(input_uris=["input_uris_value"]) + ), + output_config=io.BatchPredictOutputConfig( + gcs_destination=io.GcsDestination( + output_uri_prefix="output_uri_prefix_value" + ) + ), + params={"key_value": "value_value"}, + ) + + +def test_batch_predict_rest_error(): + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.PredictionServiceGrpcTransport( @@ -1338,6 +1948,7 @@ def test_transport_get_channel(): [ transports.PredictionServiceGrpcTransport, transports.PredictionServiceGrpcAsyncIOTransport, + transports.PredictionServiceRestTransport, ], ) def test_transport_adc(transport_class): @@ -1352,6 +1963,7 @@ def test_transport_adc(transport_class): "transport_name", [ "grpc", + "rest", ], ) def test_transport_kind(transport_name): @@ -1487,6 +2099,7 @@ def test_prediction_service_transport_auth_adc(transport_class): [ transports.PredictionServiceGrpcTransport, transports.PredictionServiceGrpcAsyncIOTransport, + transports.PredictionServiceRestTransport, ], ) def test_prediction_service_transport_auth_gdch_credentials(transport_class): @@ -1584,11 +2197,40 @@ def test_prediction_service_grpc_transport_client_cert_source_for_mtls(transport ) +def test_prediction_service_http_transport_client_cert_source_for_mtls(): + cred = ga_credentials.AnonymousCredentials() + with mock.patch( + "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" + ) as mock_configure_mtls_channel: + transports.PredictionServiceRestTransport( + credentials=cred, client_cert_source_for_mtls=client_cert_source_callback + ) + mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) + + +def test_prediction_service_rest_lro_client(): + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + transport = client.transport + + # Ensure that we have a api-core operations client. + assert isinstance( + transport.operations_client, + operations_v1.AbstractOperationsClient, + ) + + # Ensure that subsequent calls to the property send the exact same object. + assert transport.operations_client is transport.operations_client + + @pytest.mark.parametrize( "transport_name", [ "grpc", "grpc_asyncio", + "rest", ], ) def test_prediction_service_host_no_port(transport_name): @@ -1599,7 +2241,11 @@ def test_prediction_service_host_no_port(transport_name): ), transport=transport_name, ) - assert client.transport._host == ("automl.googleapis.com:443") + assert client.transport._host == ( + "automl.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://automl.googleapis.com" + ) @pytest.mark.parametrize( @@ -1607,6 +2253,7 @@ def test_prediction_service_host_no_port(transport_name): [ "grpc", "grpc_asyncio", + "rest", ], ) def test_prediction_service_host_with_port(transport_name): @@ -1617,7 +2264,36 @@ def test_prediction_service_host_with_port(transport_name): ), transport=transport_name, ) - assert client.transport._host == ("automl.googleapis.com:8000") + assert client.transport._host == ( + "automl.googleapis.com:8000" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://automl.googleapis.com:8000" + ) + + +@pytest.mark.parametrize( + "transport_name", + [ + "rest", + ], +) +def test_prediction_service_client_transport_session_collision(transport_name): + creds1 = ga_credentials.AnonymousCredentials() + creds2 = ga_credentials.AnonymousCredentials() + client1 = PredictionServiceClient( + credentials=creds1, + transport=transport_name, + ) + client2 = PredictionServiceClient( + credentials=creds2, + transport=transport_name, + ) + session1 = client1.transport.predict._session + session2 = client2.transport.predict._session + assert session1 != session2 + session1 = client1.transport.batch_predict._session + session2 = client2.transport.batch_predict._session + assert session1 != session2 def test_prediction_service_grpc_transport_channel(): @@ -1948,6 +2624,7 @@ async def test_transport_close_async(): def test_transport_close(): transports = { + "rest": "_session", "grpc": "_grpc_channel", } @@ -1965,6 +2642,7 @@ def test_transport_close(): def test_client_ctx(): transports = [ + "rest", "grpc", ] for transport in transports: diff --git a/tests/unit/gapic/automl_v1beta1/test_auto_ml.py b/tests/unit/gapic/automl_v1beta1/test_auto_ml.py index 3bc51e8b..9e56fe31 100644 --- a/tests/unit/gapic/automl_v1beta1/test_auto_ml.py +++ b/tests/unit/gapic/automl_v1beta1/test_auto_ml.py @@ -24,10 +24,17 @@ import grpc from grpc.experimental import aio +from collections.abc import Iterable +from google.protobuf import json_format +import json import math import pytest from proto.marshal.rules.dates import DurationRule, TimestampRule from proto.marshal.rules import wrappers +from requests import Response +from requests import Request, PreparedRequest +from requests.sessions import Session +from google.protobuf import json_format from google.api_core import client_options from google.api_core import exceptions as core_exceptions @@ -121,6 +128,7 @@ def test__get_default_mtls_endpoint(): [ (AutoMlClient, "grpc"), (AutoMlAsyncClient, "grpc_asyncio"), + (AutoMlClient, "rest"), ], ) def test_auto_ml_client_from_service_account_info(client_class, transport_name): @@ -134,7 +142,11 @@ def test_auto_ml_client_from_service_account_info(client_class, transport_name): assert client.transport._credentials == creds assert isinstance(client, client_class) - assert client.transport._host == ("automl.googleapis.com:443") + assert client.transport._host == ( + "automl.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://automl.googleapis.com" + ) @pytest.mark.parametrize( @@ -142,6 +154,7 @@ def test_auto_ml_client_from_service_account_info(client_class, transport_name): [ (transports.AutoMlGrpcTransport, "grpc"), (transports.AutoMlGrpcAsyncIOTransport, "grpc_asyncio"), + (transports.AutoMlRestTransport, "rest"), ], ) def test_auto_ml_client_service_account_always_use_jwt(transport_class, transport_name): @@ -165,6 +178,7 @@ def test_auto_ml_client_service_account_always_use_jwt(transport_class, transpor [ (AutoMlClient, "grpc"), (AutoMlAsyncClient, "grpc_asyncio"), + (AutoMlClient, "rest"), ], ) def test_auto_ml_client_from_service_account_file(client_class, transport_name): @@ -185,13 +199,18 @@ def test_auto_ml_client_from_service_account_file(client_class, transport_name): assert client.transport._credentials == creds assert isinstance(client, client_class) - assert client.transport._host == ("automl.googleapis.com:443") + assert client.transport._host == ( + "automl.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://automl.googleapis.com" + ) def test_auto_ml_client_get_transport_class(): transport = AutoMlClient.get_transport_class() available_transports = [ transports.AutoMlGrpcTransport, + transports.AutoMlRestTransport, ] assert transport in available_transports @@ -204,6 +223,7 @@ def test_auto_ml_client_get_transport_class(): [ (AutoMlClient, transports.AutoMlGrpcTransport, "grpc"), (AutoMlAsyncClient, transports.AutoMlGrpcAsyncIOTransport, "grpc_asyncio"), + (AutoMlClient, transports.AutoMlRestTransport, "rest"), ], ) @mock.patch.object( @@ -343,6 +363,8 @@ def test_auto_ml_client_client_options(client_class, transport_class, transport_ "grpc_asyncio", "false", ), + (AutoMlClient, transports.AutoMlRestTransport, "rest", "true"), + (AutoMlClient, transports.AutoMlRestTransport, "rest", "false"), ], ) @mock.patch.object( @@ -528,6 +550,7 @@ def test_auto_ml_client_get_mtls_endpoint_and_cert_source(client_class): [ (AutoMlClient, transports.AutoMlGrpcTransport, "grpc"), (AutoMlAsyncClient, transports.AutoMlGrpcAsyncIOTransport, "grpc_asyncio"), + (AutoMlClient, transports.AutoMlRestTransport, "rest"), ], ) def test_auto_ml_client_client_options_scopes( @@ -563,6 +586,7 @@ def test_auto_ml_client_client_options_scopes( "grpc_asyncio", grpc_helpers_async, ), + (AutoMlClient, transports.AutoMlRestTransport, "rest", None), ], ) def test_auto_ml_client_client_options_credentials_file( @@ -7583,156 +7607,7452 @@ async def test_list_model_evaluations_async_pages(): assert page_.raw_page.next_page_token == token -def test_credentials_transport_error(): - # It is an error to provide credentials and a transport instance. - transport = transports.AutoMlGrpcTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - with pytest.raises(ValueError): - client = AutoMlClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, +@pytest.mark.parametrize( + "request_type", + [ + service.CreateDatasetRequest, + dict, + ], +) +def test_create_dataset_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["dataset"] = { + "translation_dataset_metadata": { + "source_language_code": "source_language_code_value", + "target_language_code": "target_language_code_value", + }, + "image_classification_dataset_metadata": {"classification_type": 1}, + "text_classification_dataset_metadata": {"classification_type": 1}, + "image_object_detection_dataset_metadata": {}, + "video_classification_dataset_metadata": {}, + "video_object_tracking_dataset_metadata": {}, + "text_extraction_dataset_metadata": {}, + "text_sentiment_dataset_metadata": {"sentiment_max": 1404}, + "tables_dataset_metadata": { + "primary_table_spec_id": "primary_table_spec_id_value", + "target_column_spec_id": "target_column_spec_id_value", + "weight_column_spec_id": "weight_column_spec_id_value", + "ml_use_column_spec_id": "ml_use_column_spec_id_value", + "target_column_correlations": {}, + "stats_update_time": {"seconds": 751, "nanos": 543}, + }, + "name": "name_value", + "display_name": "display_name_value", + "description": "description_value", + "example_count": 1396, + "create_time": {}, + "etag": "etag_value", + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = gca_dataset.Dataset( + name="name_value", + display_name="display_name_value", + description="description_value", + example_count=1396, + etag="etag_value", + translation_dataset_metadata=translation.TranslationDatasetMetadata( + source_language_code="source_language_code_value" + ), ) - # It is an error to provide a credentials file and a transport instance. - transport = transports.AutoMlGrpcTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - with pytest.raises(ValueError): - client = AutoMlClient( - client_options={"credentials_file": "credentials.json"}, - transport=transport, + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = gca_dataset.Dataset.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.create_dataset(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, gca_dataset.Dataset) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.example_count == 1396 + assert response.etag == "etag_value" + + +def test_create_dataset_rest_required_fields(request_type=service.CreateDatasetRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, ) + ) - # It is an error to provide an api_key and a transport instance. - transport = transports.AutoMlGrpcTransport( + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_dataset._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_dataset._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = AutoMlClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = AutoMlClient( - client_options=options, - transport=transport, - ) + request = request_type(**request_init) - # It is an error to provide an api_key and a credential. - options = mock.Mock() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = AutoMlClient( - client_options=options, credentials=ga_credentials.AnonymousCredentials() + # Designate an appropriate value for the returned response. + return_value = gca_dataset.Dataset() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = gca_dataset.Dataset.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.create_dataset(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_dataset_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_dataset._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(()) + & set( + ( + "parent", + "dataset", + ) ) + ) - # It is an error to provide scopes and a transport instance. - transport = transports.AutoMlGrpcTransport( + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_dataset_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), ) - with pytest.raises(ValueError): - client = AutoMlClient( - client_options={"scopes": ["1", "2"]}, - transport=transport, + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_create_dataset" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_create_dataset" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.CreateDatasetRequest.pb(service.CreateDatasetRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = gca_dataset.Dataset.to_json(gca_dataset.Dataset()) + + request = service.CreateDatasetRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = gca_dataset.Dataset() + + client.create_dataset( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], ) + pre.assert_called_once() + post.assert_called_once() -def test_transport_instance(): - # A client may be instantiated with a custom transport instance. - transport = transports.AutoMlGrpcTransport( + +def test_create_dataset_rest_bad_request( + transport: str = "rest", request_type=service.CreateDatasetRequest +): + client = AutoMlClient( credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - client = AutoMlClient(transport=transport) - assert client.transport is transport + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["dataset"] = { + "translation_dataset_metadata": { + "source_language_code": "source_language_code_value", + "target_language_code": "target_language_code_value", + }, + "image_classification_dataset_metadata": {"classification_type": 1}, + "text_classification_dataset_metadata": {"classification_type": 1}, + "image_object_detection_dataset_metadata": {}, + "video_classification_dataset_metadata": {}, + "video_object_tracking_dataset_metadata": {}, + "text_extraction_dataset_metadata": {}, + "text_sentiment_dataset_metadata": {"sentiment_max": 1404}, + "tables_dataset_metadata": { + "primary_table_spec_id": "primary_table_spec_id_value", + "target_column_spec_id": "target_column_spec_id_value", + "weight_column_spec_id": "weight_column_spec_id_value", + "ml_use_column_spec_id": "ml_use_column_spec_id_value", + "target_column_correlations": {}, + "stats_update_time": {"seconds": 751, "nanos": 543}, + }, + "name": "name_value", + "display_name": "display_name_value", + "description": "description_value", + "example_count": 1396, + "create_time": {}, + "etag": "etag_value", + } + request = request_type(**request_init) -def test_transport_get_channel(): - # A client may be instantiated with a custom transport instance. - transport = transports.AutoMlGrpcTransport( + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.create_dataset(request) + + +def test_create_dataset_rest_flattened(): + client = AutoMlClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - channel = transport.grpc_channel - assert channel - transport = transports.AutoMlGrpcAsyncIOTransport( + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = gca_dataset.Dataset() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + dataset=gca_dataset.Dataset( + translation_dataset_metadata=translation.TranslationDatasetMetadata( + source_language_code="source_language_code_value" + ) + ), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = gca_dataset.Dataset.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.create_dataset(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{parent=projects/*/locations/*}/datasets" + % client.transport._host, + args[1], + ) + + +def test_create_dataset_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - channel = transport.grpc_channel - assert channel + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_dataset( + service.CreateDatasetRequest(), + parent="parent_value", + dataset=gca_dataset.Dataset( + translation_dataset_metadata=translation.TranslationDatasetMetadata( + source_language_code="source_language_code_value" + ) + ), + ) -@pytest.mark.parametrize( - "transport_class", - [ - transports.AutoMlGrpcTransport, - transports.AutoMlGrpcAsyncIOTransport, - ], -) -def test_transport_adc(transport_class): - # Test default credentials are used if not provided. - with mock.patch.object(google.auth, "default") as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport_class() - adc.assert_called_once() + +def test_create_dataset_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) @pytest.mark.parametrize( - "transport_name", + "request_type", [ - "grpc", + service.GetDatasetRequest, + dict, ], ) -def test_transport_kind(transport_name): - transport = AutoMlClient.get_transport_class(transport_name)( +def test_get_dataset_rest(request_type): + client = AutoMlClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - assert transport.kind == transport_name + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = dataset.Dataset( + name="name_value", + display_name="display_name_value", + description="description_value", + example_count=1396, + etag="etag_value", + translation_dataset_metadata=translation.TranslationDatasetMetadata( + source_language_code="source_language_code_value" + ), + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = dataset.Dataset.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.get_dataset(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, dataset.Dataset) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.example_count == 1396 + assert response.etag == "etag_value" + + +def test_get_dataset_rest_required_fields(request_type=service.GetDatasetRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_dataset._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_dataset._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" -def test_transport_grpc_default(): - # A client should use the gRPC transport by default. client = AutoMlClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - assert isinstance( - client.transport, - transports.AutoMlGrpcTransport, + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = dataset.Dataset() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = dataset.Dataset.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.get_dataset(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_dataset_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials ) + unset_fields = transport.get_dataset._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) -def test_auto_ml_base_transport_error(): - # Passing both a credentials object and credentials_file should raise an error - with pytest.raises(core_exceptions.DuplicateCredentialArgs): - transport = transports.AutoMlTransport( - credentials=ga_credentials.AnonymousCredentials(), - credentials_file="credentials.json", - ) +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_dataset_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_get_dataset" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_get_dataset" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.GetDatasetRequest.pb(service.GetDatasetRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = dataset.Dataset.to_json(dataset.Dataset()) + + request = service.GetDatasetRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = dataset.Dataset() -def test_auto_ml_base_transport(): - # Instantiate the base transport. - with mock.patch( - "google.cloud.automl_v1beta1.services.auto_ml.transports.AutoMlTransport.__init__" - ) as Transport: - Transport.return_value = None - transport = transports.AutoMlTransport( - credentials=ga_credentials.AnonymousCredentials(), + client.get_dataset( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], ) - # Every method on the transport should just blindly - # raise NotImplementedError. - methods = ( - "create_dataset", - "get_dataset", - "list_datasets", - "update_dataset", - "delete_dataset", - "import_data", - "export_data", - "get_annotation_spec", - "get_table_spec", - "list_table_specs", - "update_table_spec", - "get_column_spec", - "list_column_specs", + pre.assert_called_once() + post.assert_called_once() + + +def test_get_dataset_rest_bad_request( + transport: str = "rest", request_type=service.GetDatasetRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.get_dataset(request) + + +def test_get_dataset_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = dataset.Dataset() + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = dataset.Dataset.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.get_dataset(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{name=projects/*/locations/*/datasets/*}" + % client.transport._host, + args[1], + ) + + +def test_get_dataset_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_dataset( + service.GetDatasetRequest(), + name="name_value", + ) + + +def test_get_dataset_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.ListDatasetsRequest, + dict, + ], +) +def test_list_datasets_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = service.ListDatasetsResponse( + next_page_token="next_page_token_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = service.ListDatasetsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.list_datasets(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListDatasetsPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_datasets_rest_required_fields(request_type=service.ListDatasetsRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_datasets._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_datasets._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = service.ListDatasetsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = service.ListDatasetsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.list_datasets(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_datasets_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_datasets._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_datasets_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_list_datasets" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_list_datasets" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.ListDatasetsRequest.pb(service.ListDatasetsRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = service.ListDatasetsResponse.to_json( + service.ListDatasetsResponse() + ) + + request = service.ListDatasetsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = service.ListDatasetsResponse() + + client.list_datasets( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_list_datasets_rest_bad_request( + transport: str = "rest", request_type=service.ListDatasetsRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_datasets(request) + + +def test_list_datasets_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = service.ListDatasetsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = service.ListDatasetsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.list_datasets(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{parent=projects/*/locations/*}/datasets" + % client.transport._host, + args[1], + ) + + +def test_list_datasets_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_datasets( + service.ListDatasetsRequest(), + parent="parent_value", + ) + + +def test_list_datasets_rest_pager(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + service.ListDatasetsResponse( + datasets=[ + dataset.Dataset(), + dataset.Dataset(), + dataset.Dataset(), + ], + next_page_token="abc", + ), + service.ListDatasetsResponse( + datasets=[], + next_page_token="def", + ), + service.ListDatasetsResponse( + datasets=[ + dataset.Dataset(), + ], + next_page_token="ghi", + ), + service.ListDatasetsResponse( + datasets=[ + dataset.Dataset(), + dataset.Dataset(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(service.ListDatasetsResponse.to_json(x) for x in response) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = {"parent": "projects/sample1/locations/sample2"} + + pager = client.list_datasets(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, dataset.Dataset) for i in results) + + pages = list(client.list_datasets(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + service.UpdateDatasetRequest, + dict, + ], +) +def test_update_dataset_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "dataset": {"name": "projects/sample1/locations/sample2/datasets/sample3"} + } + request_init["dataset"] = { + "translation_dataset_metadata": { + "source_language_code": "source_language_code_value", + "target_language_code": "target_language_code_value", + }, + "image_classification_dataset_metadata": {"classification_type": 1}, + "text_classification_dataset_metadata": {"classification_type": 1}, + "image_object_detection_dataset_metadata": {}, + "video_classification_dataset_metadata": {}, + "video_object_tracking_dataset_metadata": {}, + "text_extraction_dataset_metadata": {}, + "text_sentiment_dataset_metadata": {"sentiment_max": 1404}, + "tables_dataset_metadata": { + "primary_table_spec_id": "primary_table_spec_id_value", + "target_column_spec_id": "target_column_spec_id_value", + "weight_column_spec_id": "weight_column_spec_id_value", + "ml_use_column_spec_id": "ml_use_column_spec_id_value", + "target_column_correlations": {}, + "stats_update_time": {"seconds": 751, "nanos": 543}, + }, + "name": "projects/sample1/locations/sample2/datasets/sample3", + "display_name": "display_name_value", + "description": "description_value", + "example_count": 1396, + "create_time": {}, + "etag": "etag_value", + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = gca_dataset.Dataset( + name="name_value", + display_name="display_name_value", + description="description_value", + example_count=1396, + etag="etag_value", + translation_dataset_metadata=translation.TranslationDatasetMetadata( + source_language_code="source_language_code_value" + ), + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = gca_dataset.Dataset.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.update_dataset(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, gca_dataset.Dataset) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.example_count == 1396 + assert response.etag == "etag_value" + + +def test_update_dataset_rest_required_fields(request_type=service.UpdateDatasetRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_dataset._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_dataset._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("update_mask",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = gca_dataset.Dataset() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "patch", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = gca_dataset.Dataset.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.update_dataset(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_update_dataset_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.update_dataset._get_unset_required_fields({}) + assert set(unset_fields) == (set(("updateMask",)) & set(("dataset",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_update_dataset_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_update_dataset" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_update_dataset" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.UpdateDatasetRequest.pb(service.UpdateDatasetRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = gca_dataset.Dataset.to_json(gca_dataset.Dataset()) + + request = service.UpdateDatasetRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = gca_dataset.Dataset() + + client.update_dataset( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_update_dataset_rest_bad_request( + transport: str = "rest", request_type=service.UpdateDatasetRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "dataset": {"name": "projects/sample1/locations/sample2/datasets/sample3"} + } + request_init["dataset"] = { + "translation_dataset_metadata": { + "source_language_code": "source_language_code_value", + "target_language_code": "target_language_code_value", + }, + "image_classification_dataset_metadata": {"classification_type": 1}, + "text_classification_dataset_metadata": {"classification_type": 1}, + "image_object_detection_dataset_metadata": {}, + "video_classification_dataset_metadata": {}, + "video_object_tracking_dataset_metadata": {}, + "text_extraction_dataset_metadata": {}, + "text_sentiment_dataset_metadata": {"sentiment_max": 1404}, + "tables_dataset_metadata": { + "primary_table_spec_id": "primary_table_spec_id_value", + "target_column_spec_id": "target_column_spec_id_value", + "weight_column_spec_id": "weight_column_spec_id_value", + "ml_use_column_spec_id": "ml_use_column_spec_id_value", + "target_column_correlations": {}, + "stats_update_time": {"seconds": 751, "nanos": 543}, + }, + "name": "projects/sample1/locations/sample2/datasets/sample3", + "display_name": "display_name_value", + "description": "description_value", + "example_count": 1396, + "create_time": {}, + "etag": "etag_value", + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.update_dataset(request) + + +def test_update_dataset_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = gca_dataset.Dataset() + + # get arguments that satisfy an http rule for this method + sample_request = { + "dataset": {"name": "projects/sample1/locations/sample2/datasets/sample3"} + } + + # get truthy value for each flattened field + mock_args = dict( + dataset=gca_dataset.Dataset( + translation_dataset_metadata=translation.TranslationDatasetMetadata( + source_language_code="source_language_code_value" + ) + ), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = gca_dataset.Dataset.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.update_dataset(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{dataset.name=projects/*/locations/*/datasets/*}" + % client.transport._host, + args[1], + ) + + +def test_update_dataset_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_dataset( + service.UpdateDatasetRequest(), + dataset=gca_dataset.Dataset( + translation_dataset_metadata=translation.TranslationDatasetMetadata( + source_language_code="source_language_code_value" + ) + ), + ) + + +def test_update_dataset_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.DeleteDatasetRequest, + dict, + ], +) +def test_delete_dataset_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.delete_dataset(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_delete_dataset_rest_required_fields(request_type=service.DeleteDatasetRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_dataset._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_dataset._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "delete", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.delete_dataset(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_dataset_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_dataset._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_delete_dataset_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.AutoMlRestInterceptor, "post_delete_dataset" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_delete_dataset" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.DeleteDatasetRequest.pb(service.DeleteDatasetRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = service.DeleteDatasetRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.delete_dataset( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_delete_dataset_rest_bad_request( + transport: str = "rest", request_type=service.DeleteDatasetRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.delete_dataset(request) + + +def test_delete_dataset_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.delete_dataset(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{name=projects/*/locations/*/datasets/*}" + % client.transport._host, + args[1], + ) + + +def test_delete_dataset_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_dataset( + service.DeleteDatasetRequest(), + name="name_value", + ) + + +def test_delete_dataset_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.ImportDataRequest, + dict, + ], +) +def test_import_data_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.import_data(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_import_data_rest_required_fields(request_type=service.ImportDataRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).import_data._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).import_data._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.import_data(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_import_data_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.import_data._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(()) + & set( + ( + "name", + "inputConfig", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_import_data_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.AutoMlRestInterceptor, "post_import_data" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_import_data" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.ImportDataRequest.pb(service.ImportDataRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = service.ImportDataRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.import_data( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_import_data_rest_bad_request( + transport: str = "rest", request_type=service.ImportDataRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.import_data(request) + + +def test_import_data_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + input_config=io.InputConfig( + gcs_source=io.GcsSource(input_uris=["input_uris_value"]) + ), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.import_data(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{name=projects/*/locations/*/datasets/*}:importData" + % client.transport._host, + args[1], + ) + + +def test_import_data_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.import_data( + service.ImportDataRequest(), + name="name_value", + input_config=io.InputConfig( + gcs_source=io.GcsSource(input_uris=["input_uris_value"]) + ), + ) + + +def test_import_data_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.ExportDataRequest, + dict, + ], +) +def test_export_data_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.export_data(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_export_data_rest_required_fields(request_type=service.ExportDataRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).export_data._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).export_data._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.export_data(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_export_data_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.export_data._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(()) + & set( + ( + "name", + "outputConfig", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_export_data_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.AutoMlRestInterceptor, "post_export_data" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_export_data" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.ExportDataRequest.pb(service.ExportDataRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = service.ExportDataRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.export_data( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_export_data_rest_bad_request( + transport: str = "rest", request_type=service.ExportDataRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.export_data(request) + + +def test_export_data_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/datasets/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + output_config=io.OutputConfig( + gcs_destination=io.GcsDestination( + output_uri_prefix="output_uri_prefix_value" + ) + ), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.export_data(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{name=projects/*/locations/*/datasets/*}:exportData" + % client.transport._host, + args[1], + ) + + +def test_export_data_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.export_data( + service.ExportDataRequest(), + name="name_value", + output_config=io.OutputConfig( + gcs_destination=io.GcsDestination( + output_uri_prefix="output_uri_prefix_value" + ) + ), + ) + + +def test_export_data_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.GetAnnotationSpecRequest, + dict, + ], +) +def test_get_annotation_spec_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/datasets/sample3/annotationSpecs/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = annotation_spec.AnnotationSpec( + name="name_value", + display_name="display_name_value", + example_count=1396, + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = annotation_spec.AnnotationSpec.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.get_annotation_spec(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, annotation_spec.AnnotationSpec) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.example_count == 1396 + + +def test_get_annotation_spec_rest_required_fields( + request_type=service.GetAnnotationSpecRequest, +): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_annotation_spec._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_annotation_spec._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = annotation_spec.AnnotationSpec() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = annotation_spec.AnnotationSpec.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.get_annotation_spec(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_annotation_spec_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_annotation_spec._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_annotation_spec_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_get_annotation_spec" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_get_annotation_spec" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.GetAnnotationSpecRequest.pb( + service.GetAnnotationSpecRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = annotation_spec.AnnotationSpec.to_json( + annotation_spec.AnnotationSpec() + ) + + request = service.GetAnnotationSpecRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = annotation_spec.AnnotationSpec() + + client.get_annotation_spec( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_get_annotation_spec_rest_bad_request( + transport: str = "rest", request_type=service.GetAnnotationSpecRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/datasets/sample3/annotationSpecs/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.get_annotation_spec(request) + + +def test_get_annotation_spec_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = annotation_spec.AnnotationSpec() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/datasets/sample3/annotationSpecs/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = annotation_spec.AnnotationSpec.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.get_annotation_spec(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{name=projects/*/locations/*/datasets/*/annotationSpecs/*}" + % client.transport._host, + args[1], + ) + + +def test_get_annotation_spec_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_annotation_spec( + service.GetAnnotationSpecRequest(), + name="name_value", + ) + + +def test_get_annotation_spec_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.GetTableSpecRequest, + dict, + ], +) +def test_get_table_spec_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/datasets/sample3/tableSpecs/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = table_spec.TableSpec( + name="name_value", + time_column_spec_id="time_column_spec_id_value", + row_count=992, + valid_row_count=1615, + column_count=1302, + etag="etag_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = table_spec.TableSpec.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.get_table_spec(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, table_spec.TableSpec) + assert response.name == "name_value" + assert response.time_column_spec_id == "time_column_spec_id_value" + assert response.row_count == 992 + assert response.valid_row_count == 1615 + assert response.column_count == 1302 + assert response.etag == "etag_value" + + +def test_get_table_spec_rest_required_fields(request_type=service.GetTableSpecRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_table_spec._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_table_spec._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("field_mask",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = table_spec.TableSpec() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = table_spec.TableSpec.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.get_table_spec(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_table_spec_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_table_spec._get_unset_required_fields({}) + assert set(unset_fields) == (set(("fieldMask",)) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_table_spec_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_get_table_spec" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_get_table_spec" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.GetTableSpecRequest.pb(service.GetTableSpecRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = table_spec.TableSpec.to_json(table_spec.TableSpec()) + + request = service.GetTableSpecRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = table_spec.TableSpec() + + client.get_table_spec( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_get_table_spec_rest_bad_request( + transport: str = "rest", request_type=service.GetTableSpecRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/datasets/sample3/tableSpecs/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.get_table_spec(request) + + +def test_get_table_spec_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = table_spec.TableSpec() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/datasets/sample3/tableSpecs/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = table_spec.TableSpec.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.get_table_spec(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{name=projects/*/locations/*/datasets/*/tableSpecs/*}" + % client.transport._host, + args[1], + ) + + +def test_get_table_spec_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_table_spec( + service.GetTableSpecRequest(), + name="name_value", + ) + + +def test_get_table_spec_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.ListTableSpecsRequest, + dict, + ], +) +def test_list_table_specs_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2/datasets/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = service.ListTableSpecsResponse( + next_page_token="next_page_token_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = service.ListTableSpecsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.list_table_specs(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListTableSpecsPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_table_specs_rest_required_fields( + request_type=service.ListTableSpecsRequest, +): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_table_specs._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_table_specs._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "field_mask", + "filter", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = service.ListTableSpecsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = service.ListTableSpecsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.list_table_specs(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_table_specs_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_table_specs._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "fieldMask", + "filter", + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_table_specs_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_list_table_specs" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_list_table_specs" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.ListTableSpecsRequest.pb(service.ListTableSpecsRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = service.ListTableSpecsResponse.to_json( + service.ListTableSpecsResponse() + ) + + request = service.ListTableSpecsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = service.ListTableSpecsResponse() + + client.list_table_specs( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_list_table_specs_rest_bad_request( + transport: str = "rest", request_type=service.ListTableSpecsRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2/datasets/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_table_specs(request) + + +def test_list_table_specs_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = service.ListTableSpecsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = { + "parent": "projects/sample1/locations/sample2/datasets/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = service.ListTableSpecsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.list_table_specs(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{parent=projects/*/locations/*/datasets/*}/tableSpecs" + % client.transport._host, + args[1], + ) + + +def test_list_table_specs_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_table_specs( + service.ListTableSpecsRequest(), + parent="parent_value", + ) + + +def test_list_table_specs_rest_pager(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + service.ListTableSpecsResponse( + table_specs=[ + table_spec.TableSpec(), + table_spec.TableSpec(), + table_spec.TableSpec(), + ], + next_page_token="abc", + ), + service.ListTableSpecsResponse( + table_specs=[], + next_page_token="def", + ), + service.ListTableSpecsResponse( + table_specs=[ + table_spec.TableSpec(), + ], + next_page_token="ghi", + ), + service.ListTableSpecsResponse( + table_specs=[ + table_spec.TableSpec(), + table_spec.TableSpec(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(service.ListTableSpecsResponse.to_json(x) for x in response) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = { + "parent": "projects/sample1/locations/sample2/datasets/sample3" + } + + pager = client.list_table_specs(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, table_spec.TableSpec) for i in results) + + pages = list(client.list_table_specs(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + service.UpdateTableSpecRequest, + dict, + ], +) +def test_update_table_spec_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "table_spec": { + "name": "projects/sample1/locations/sample2/datasets/sample3/tableSpecs/sample4" + } + } + request_init["table_spec"] = { + "name": "projects/sample1/locations/sample2/datasets/sample3/tableSpecs/sample4", + "time_column_spec_id": "time_column_spec_id_value", + "row_count": 992, + "valid_row_count": 1615, + "column_count": 1302, + "input_configs": [ + { + "gcs_source": { + "input_uris": ["input_uris_value1", "input_uris_value2"] + }, + "bigquery_source": {"input_uri": "input_uri_value"}, + "params": {}, + } + ], + "etag": "etag_value", + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = gca_table_spec.TableSpec( + name="name_value", + time_column_spec_id="time_column_spec_id_value", + row_count=992, + valid_row_count=1615, + column_count=1302, + etag="etag_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = gca_table_spec.TableSpec.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.update_table_spec(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, gca_table_spec.TableSpec) + assert response.name == "name_value" + assert response.time_column_spec_id == "time_column_spec_id_value" + assert response.row_count == 992 + assert response.valid_row_count == 1615 + assert response.column_count == 1302 + assert response.etag == "etag_value" + + +def test_update_table_spec_rest_required_fields( + request_type=service.UpdateTableSpecRequest, +): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_table_spec._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_table_spec._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("update_mask",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = gca_table_spec.TableSpec() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "patch", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = gca_table_spec.TableSpec.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.update_table_spec(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_update_table_spec_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.update_table_spec._get_unset_required_fields({}) + assert set(unset_fields) == (set(("updateMask",)) & set(("tableSpec",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_update_table_spec_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_update_table_spec" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_update_table_spec" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.UpdateTableSpecRequest.pb(service.UpdateTableSpecRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = gca_table_spec.TableSpec.to_json( + gca_table_spec.TableSpec() + ) + + request = service.UpdateTableSpecRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = gca_table_spec.TableSpec() + + client.update_table_spec( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_update_table_spec_rest_bad_request( + transport: str = "rest", request_type=service.UpdateTableSpecRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "table_spec": { + "name": "projects/sample1/locations/sample2/datasets/sample3/tableSpecs/sample4" + } + } + request_init["table_spec"] = { + "name": "projects/sample1/locations/sample2/datasets/sample3/tableSpecs/sample4", + "time_column_spec_id": "time_column_spec_id_value", + "row_count": 992, + "valid_row_count": 1615, + "column_count": 1302, + "input_configs": [ + { + "gcs_source": { + "input_uris": ["input_uris_value1", "input_uris_value2"] + }, + "bigquery_source": {"input_uri": "input_uri_value"}, + "params": {}, + } + ], + "etag": "etag_value", + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.update_table_spec(request) + + +def test_update_table_spec_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = gca_table_spec.TableSpec() + + # get arguments that satisfy an http rule for this method + sample_request = { + "table_spec": { + "name": "projects/sample1/locations/sample2/datasets/sample3/tableSpecs/sample4" + } + } + + # get truthy value for each flattened field + mock_args = dict( + table_spec=gca_table_spec.TableSpec(name="name_value"), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = gca_table_spec.TableSpec.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.update_table_spec(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{table_spec.name=projects/*/locations/*/datasets/*/tableSpecs/*}" + % client.transport._host, + args[1], + ) + + +def test_update_table_spec_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_table_spec( + service.UpdateTableSpecRequest(), + table_spec=gca_table_spec.TableSpec(name="name_value"), + ) + + +def test_update_table_spec_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.GetColumnSpecRequest, + dict, + ], +) +def test_get_column_spec_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/datasets/sample3/tableSpecs/sample4/columnSpecs/sample5" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = column_spec.ColumnSpec( + name="name_value", + display_name="display_name_value", + etag="etag_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = column_spec.ColumnSpec.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.get_column_spec(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, column_spec.ColumnSpec) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.etag == "etag_value" + + +def test_get_column_spec_rest_required_fields( + request_type=service.GetColumnSpecRequest, +): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_column_spec._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_column_spec._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("field_mask",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = column_spec.ColumnSpec() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = column_spec.ColumnSpec.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.get_column_spec(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_column_spec_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_column_spec._get_unset_required_fields({}) + assert set(unset_fields) == (set(("fieldMask",)) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_column_spec_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_get_column_spec" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_get_column_spec" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.GetColumnSpecRequest.pb(service.GetColumnSpecRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = column_spec.ColumnSpec.to_json( + column_spec.ColumnSpec() + ) + + request = service.GetColumnSpecRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = column_spec.ColumnSpec() + + client.get_column_spec( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_get_column_spec_rest_bad_request( + transport: str = "rest", request_type=service.GetColumnSpecRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/datasets/sample3/tableSpecs/sample4/columnSpecs/sample5" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.get_column_spec(request) + + +def test_get_column_spec_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = column_spec.ColumnSpec() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/datasets/sample3/tableSpecs/sample4/columnSpecs/sample5" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = column_spec.ColumnSpec.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.get_column_spec(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{name=projects/*/locations/*/datasets/*/tableSpecs/*/columnSpecs/*}" + % client.transport._host, + args[1], + ) + + +def test_get_column_spec_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_column_spec( + service.GetColumnSpecRequest(), + name="name_value", + ) + + +def test_get_column_spec_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.ListColumnSpecsRequest, + dict, + ], +) +def test_list_column_specs_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "parent": "projects/sample1/locations/sample2/datasets/sample3/tableSpecs/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = service.ListColumnSpecsResponse( + next_page_token="next_page_token_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = service.ListColumnSpecsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.list_column_specs(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListColumnSpecsPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_column_specs_rest_required_fields( + request_type=service.ListColumnSpecsRequest, +): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_column_specs._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_column_specs._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "field_mask", + "filter", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = service.ListColumnSpecsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = service.ListColumnSpecsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.list_column_specs(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_column_specs_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_column_specs._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "fieldMask", + "filter", + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_column_specs_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_list_column_specs" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_list_column_specs" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.ListColumnSpecsRequest.pb(service.ListColumnSpecsRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = service.ListColumnSpecsResponse.to_json( + service.ListColumnSpecsResponse() + ) + + request = service.ListColumnSpecsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = service.ListColumnSpecsResponse() + + client.list_column_specs( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_list_column_specs_rest_bad_request( + transport: str = "rest", request_type=service.ListColumnSpecsRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "parent": "projects/sample1/locations/sample2/datasets/sample3/tableSpecs/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_column_specs(request) + + +def test_list_column_specs_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = service.ListColumnSpecsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = { + "parent": "projects/sample1/locations/sample2/datasets/sample3/tableSpecs/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = service.ListColumnSpecsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.list_column_specs(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{parent=projects/*/locations/*/datasets/*/tableSpecs/*}/columnSpecs" + % client.transport._host, + args[1], + ) + + +def test_list_column_specs_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_column_specs( + service.ListColumnSpecsRequest(), + parent="parent_value", + ) + + +def test_list_column_specs_rest_pager(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + service.ListColumnSpecsResponse( + column_specs=[ + column_spec.ColumnSpec(), + column_spec.ColumnSpec(), + column_spec.ColumnSpec(), + ], + next_page_token="abc", + ), + service.ListColumnSpecsResponse( + column_specs=[], + next_page_token="def", + ), + service.ListColumnSpecsResponse( + column_specs=[ + column_spec.ColumnSpec(), + ], + next_page_token="ghi", + ), + service.ListColumnSpecsResponse( + column_specs=[ + column_spec.ColumnSpec(), + column_spec.ColumnSpec(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(service.ListColumnSpecsResponse.to_json(x) for x in response) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = { + "parent": "projects/sample1/locations/sample2/datasets/sample3/tableSpecs/sample4" + } + + pager = client.list_column_specs(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, column_spec.ColumnSpec) for i in results) + + pages = list(client.list_column_specs(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + service.UpdateColumnSpecRequest, + dict, + ], +) +def test_update_column_spec_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "column_spec": { + "name": "projects/sample1/locations/sample2/datasets/sample3/tableSpecs/sample4/columnSpecs/sample5" + } + } + request_init["column_spec"] = { + "name": "projects/sample1/locations/sample2/datasets/sample3/tableSpecs/sample4/columnSpecs/sample5", + "data_type": { + "list_element_type": {}, + "struct_type": {"fields": {}}, + "time_format": "time_format_value", + "type_code": 3, + "nullable": True, + }, + "display_name": "display_name_value", + "data_stats": { + "float64_stats": { + "mean": 0.417, + "standard_deviation": 0.1907, + "quantiles": [0.983, 0.984], + "histogram_buckets": [{"min_": 0.419, "max_": 0.421, "count": 553}], + }, + "string_stats": { + "top_unigram_stats": [{"value": "value_value", "count": 553}] + }, + "timestamp_stats": {"granular_stats": {}}, + "array_stats": {"member_stats": {}}, + "struct_stats": {"field_stats": {}}, + "category_stats": { + "top_category_stats": [{"value": "value_value", "count": 553}] + }, + "distinct_value_count": 2150, + "null_value_count": 1727, + "valid_value_count": 1812, + }, + "top_correlated_columns": [ + { + "column_spec_id": "column_spec_id_value", + "correlation_stats": {"cramers_v": 0.962}, + } + ], + "etag": "etag_value", + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = gca_column_spec.ColumnSpec( + name="name_value", + display_name="display_name_value", + etag="etag_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = gca_column_spec.ColumnSpec.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.update_column_spec(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, gca_column_spec.ColumnSpec) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.etag == "etag_value" + + +def test_update_column_spec_rest_required_fields( + request_type=service.UpdateColumnSpecRequest, +): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_column_spec._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_column_spec._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("update_mask",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = gca_column_spec.ColumnSpec() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "patch", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = gca_column_spec.ColumnSpec.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.update_column_spec(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_update_column_spec_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.update_column_spec._get_unset_required_fields({}) + assert set(unset_fields) == (set(("updateMask",)) & set(("columnSpec",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_update_column_spec_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_update_column_spec" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_update_column_spec" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.UpdateColumnSpecRequest.pb( + service.UpdateColumnSpecRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = gca_column_spec.ColumnSpec.to_json( + gca_column_spec.ColumnSpec() + ) + + request = service.UpdateColumnSpecRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = gca_column_spec.ColumnSpec() + + client.update_column_spec( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_update_column_spec_rest_bad_request( + transport: str = "rest", request_type=service.UpdateColumnSpecRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "column_spec": { + "name": "projects/sample1/locations/sample2/datasets/sample3/tableSpecs/sample4/columnSpecs/sample5" + } + } + request_init["column_spec"] = { + "name": "projects/sample1/locations/sample2/datasets/sample3/tableSpecs/sample4/columnSpecs/sample5", + "data_type": { + "list_element_type": {}, + "struct_type": {"fields": {}}, + "time_format": "time_format_value", + "type_code": 3, + "nullable": True, + }, + "display_name": "display_name_value", + "data_stats": { + "float64_stats": { + "mean": 0.417, + "standard_deviation": 0.1907, + "quantiles": [0.983, 0.984], + "histogram_buckets": [{"min_": 0.419, "max_": 0.421, "count": 553}], + }, + "string_stats": { + "top_unigram_stats": [{"value": "value_value", "count": 553}] + }, + "timestamp_stats": {"granular_stats": {}}, + "array_stats": {"member_stats": {}}, + "struct_stats": {"field_stats": {}}, + "category_stats": { + "top_category_stats": [{"value": "value_value", "count": 553}] + }, + "distinct_value_count": 2150, + "null_value_count": 1727, + "valid_value_count": 1812, + }, + "top_correlated_columns": [ + { + "column_spec_id": "column_spec_id_value", + "correlation_stats": {"cramers_v": 0.962}, + } + ], + "etag": "etag_value", + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.update_column_spec(request) + + +def test_update_column_spec_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = gca_column_spec.ColumnSpec() + + # get arguments that satisfy an http rule for this method + sample_request = { + "column_spec": { + "name": "projects/sample1/locations/sample2/datasets/sample3/tableSpecs/sample4/columnSpecs/sample5" + } + } + + # get truthy value for each flattened field + mock_args = dict( + column_spec=gca_column_spec.ColumnSpec(name="name_value"), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = gca_column_spec.ColumnSpec.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.update_column_spec(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{column_spec.name=projects/*/locations/*/datasets/*/tableSpecs/*/columnSpecs/*}" + % client.transport._host, + args[1], + ) + + +def test_update_column_spec_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_column_spec( + service.UpdateColumnSpecRequest(), + column_spec=gca_column_spec.ColumnSpec(name="name_value"), + ) + + +def test_update_column_spec_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.CreateModelRequest, + dict, + ], +) +def test_create_model_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["model"] = { + "translation_model_metadata": { + "base_model": "base_model_value", + "source_language_code": "source_language_code_value", + "target_language_code": "target_language_code_value", + }, + "image_classification_model_metadata": { + "base_model_id": "base_model_id_value", + "train_budget": 1272, + "train_cost": 1078, + "stop_reason": "stop_reason_value", + "model_type": "model_type_value", + "node_qps": 0.857, + "node_count": 1070, + }, + "text_classification_model_metadata": {"classification_type": 1}, + "image_object_detection_model_metadata": { + "model_type": "model_type_value", + "node_count": 1070, + "node_qps": 0.857, + "stop_reason": "stop_reason_value", + "train_budget_milli_node_hours": 3075, + "train_cost_milli_node_hours": 2881, + }, + "video_classification_model_metadata": {}, + "video_object_tracking_model_metadata": {}, + "text_extraction_model_metadata": {"model_hint": "model_hint_value"}, + "tables_model_metadata": { + "optimization_objective_recall_value": 0.37270000000000003, + "optimization_objective_precision_value": 0.4072, + "target_column_spec": { + "name": "name_value", + "data_type": { + "list_element_type": {}, + "struct_type": {"fields": {}}, + "time_format": "time_format_value", + "type_code": 3, + "nullable": True, + }, + "display_name": "display_name_value", + "data_stats": { + "float64_stats": { + "mean": 0.417, + "standard_deviation": 0.1907, + "quantiles": [0.983, 0.984], + "histogram_buckets": [ + {"min_": 0.419, "max_": 0.421, "count": 553} + ], + }, + "string_stats": { + "top_unigram_stats": [{"value": "value_value", "count": 553}] + }, + "timestamp_stats": {"granular_stats": {}}, + "array_stats": {"member_stats": {}}, + "struct_stats": {"field_stats": {}}, + "category_stats": { + "top_category_stats": [{"value": "value_value", "count": 553}] + }, + "distinct_value_count": 2150, + "null_value_count": 1727, + "valid_value_count": 1812, + }, + "top_correlated_columns": [ + { + "column_spec_id": "column_spec_id_value", + "correlation_stats": {"cramers_v": 0.962}, + } + ], + "etag": "etag_value", + }, + "input_feature_column_specs": {}, + "optimization_objective": "optimization_objective_value", + "tables_model_column_info": [ + { + "column_spec_name": "column_spec_name_value", + "column_display_name": "column_display_name_value", + "feature_importance": 0.1917, + } + ], + "train_budget_milli_node_hours": 3075, + "train_cost_milli_node_hours": 2881, + "disable_early_stopping": True, + }, + "text_sentiment_model_metadata": {}, + "name": "name_value", + "display_name": "display_name_value", + "dataset_id": "dataset_id_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "deployment_state": 1, + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.create_model(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_create_model_rest_required_fields(request_type=service.CreateModelRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.create_model(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_model_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_model._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(()) + & set( + ( + "parent", + "model", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_model_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.AutoMlRestInterceptor, "post_create_model" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_create_model" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.CreateModelRequest.pb(service.CreateModelRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = service.CreateModelRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.create_model( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_create_model_rest_bad_request( + transport: str = "rest", request_type=service.CreateModelRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["model"] = { + "translation_model_metadata": { + "base_model": "base_model_value", + "source_language_code": "source_language_code_value", + "target_language_code": "target_language_code_value", + }, + "image_classification_model_metadata": { + "base_model_id": "base_model_id_value", + "train_budget": 1272, + "train_cost": 1078, + "stop_reason": "stop_reason_value", + "model_type": "model_type_value", + "node_qps": 0.857, + "node_count": 1070, + }, + "text_classification_model_metadata": {"classification_type": 1}, + "image_object_detection_model_metadata": { + "model_type": "model_type_value", + "node_count": 1070, + "node_qps": 0.857, + "stop_reason": "stop_reason_value", + "train_budget_milli_node_hours": 3075, + "train_cost_milli_node_hours": 2881, + }, + "video_classification_model_metadata": {}, + "video_object_tracking_model_metadata": {}, + "text_extraction_model_metadata": {"model_hint": "model_hint_value"}, + "tables_model_metadata": { + "optimization_objective_recall_value": 0.37270000000000003, + "optimization_objective_precision_value": 0.4072, + "target_column_spec": { + "name": "name_value", + "data_type": { + "list_element_type": {}, + "struct_type": {"fields": {}}, + "time_format": "time_format_value", + "type_code": 3, + "nullable": True, + }, + "display_name": "display_name_value", + "data_stats": { + "float64_stats": { + "mean": 0.417, + "standard_deviation": 0.1907, + "quantiles": [0.983, 0.984], + "histogram_buckets": [ + {"min_": 0.419, "max_": 0.421, "count": 553} + ], + }, + "string_stats": { + "top_unigram_stats": [{"value": "value_value", "count": 553}] + }, + "timestamp_stats": {"granular_stats": {}}, + "array_stats": {"member_stats": {}}, + "struct_stats": {"field_stats": {}}, + "category_stats": { + "top_category_stats": [{"value": "value_value", "count": 553}] + }, + "distinct_value_count": 2150, + "null_value_count": 1727, + "valid_value_count": 1812, + }, + "top_correlated_columns": [ + { + "column_spec_id": "column_spec_id_value", + "correlation_stats": {"cramers_v": 0.962}, + } + ], + "etag": "etag_value", + }, + "input_feature_column_specs": {}, + "optimization_objective": "optimization_objective_value", + "tables_model_column_info": [ + { + "column_spec_name": "column_spec_name_value", + "column_display_name": "column_display_name_value", + "feature_importance": 0.1917, + } + ], + "train_budget_milli_node_hours": 3075, + "train_cost_milli_node_hours": 2881, + "disable_early_stopping": True, + }, + "text_sentiment_model_metadata": {}, + "name": "name_value", + "display_name": "display_name_value", + "dataset_id": "dataset_id_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "deployment_state": 1, + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.create_model(request) + + +def test_create_model_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + model=gca_model.Model( + translation_model_metadata=translation.TranslationModelMetadata( + base_model="base_model_value" + ) + ), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.create_model(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{parent=projects/*/locations/*}/models" + % client.transport._host, + args[1], + ) + + +def test_create_model_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_model( + service.CreateModelRequest(), + parent="parent_value", + model=gca_model.Model( + translation_model_metadata=translation.TranslationModelMetadata( + base_model="base_model_value" + ) + ), + ) + + +def test_create_model_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.GetModelRequest, + dict, + ], +) +def test_get_model_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = model.Model( + name="name_value", + display_name="display_name_value", + dataset_id="dataset_id_value", + deployment_state=model.Model.DeploymentState.DEPLOYED, + translation_model_metadata=translation.TranslationModelMetadata( + base_model="base_model_value" + ), + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = model.Model.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.get_model(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, model.Model) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.dataset_id == "dataset_id_value" + assert response.deployment_state == model.Model.DeploymentState.DEPLOYED + + +def test_get_model_rest_required_fields(request_type=service.GetModelRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = model.Model() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = model.Model.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.get_model(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_model_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_model._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_model_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_get_model" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_get_model" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.GetModelRequest.pb(service.GetModelRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = model.Model.to_json(model.Model()) + + request = service.GetModelRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = model.Model() + + client.get_model( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_get_model_rest_bad_request( + transport: str = "rest", request_type=service.GetModelRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.get_model(request) + + +def test_get_model_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = model.Model() + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/models/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = model.Model.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.get_model(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{name=projects/*/locations/*/models/*}" + % client.transport._host, + args[1], + ) + + +def test_get_model_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_model( + service.GetModelRequest(), + name="name_value", + ) + + +def test_get_model_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.ListModelsRequest, + dict, + ], +) +def test_list_models_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = service.ListModelsResponse( + next_page_token="next_page_token_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = service.ListModelsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.list_models(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListModelsPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_models_rest_required_fields(request_type=service.ListModelsRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_models._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_models._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = service.ListModelsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = service.ListModelsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.list_models(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_models_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_models._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_models_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_list_models" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_list_models" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.ListModelsRequest.pb(service.ListModelsRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = service.ListModelsResponse.to_json( + service.ListModelsResponse() + ) + + request = service.ListModelsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = service.ListModelsResponse() + + client.list_models( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_list_models_rest_bad_request( + transport: str = "rest", request_type=service.ListModelsRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_models(request) + + +def test_list_models_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = service.ListModelsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = service.ListModelsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.list_models(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{parent=projects/*/locations/*}/models" + % client.transport._host, + args[1], + ) + + +def test_list_models_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_models( + service.ListModelsRequest(), + parent="parent_value", + ) + + +def test_list_models_rest_pager(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + service.ListModelsResponse( + model=[ + model.Model(), + model.Model(), + model.Model(), + ], + next_page_token="abc", + ), + service.ListModelsResponse( + model=[], + next_page_token="def", + ), + service.ListModelsResponse( + model=[ + model.Model(), + ], + next_page_token="ghi", + ), + service.ListModelsResponse( + model=[ + model.Model(), + model.Model(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(service.ListModelsResponse.to_json(x) for x in response) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = {"parent": "projects/sample1/locations/sample2"} + + pager = client.list_models(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, model.Model) for i in results) + + pages = list(client.list_models(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + service.DeleteModelRequest, + dict, + ], +) +def test_delete_model_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.delete_model(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_delete_model_rest_required_fields(request_type=service.DeleteModelRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "delete", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.delete_model(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_model_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_model._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_delete_model_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.AutoMlRestInterceptor, "post_delete_model" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_delete_model" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.DeleteModelRequest.pb(service.DeleteModelRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = service.DeleteModelRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.delete_model( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_delete_model_rest_bad_request( + transport: str = "rest", request_type=service.DeleteModelRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.delete_model(request) + + +def test_delete_model_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/models/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.delete_model(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{name=projects/*/locations/*/models/*}" + % client.transport._host, + args[1], + ) + + +def test_delete_model_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_model( + service.DeleteModelRequest(), + name="name_value", + ) + + +def test_delete_model_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.DeployModelRequest, + dict, + ], +) +def test_deploy_model_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.deploy_model(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_deploy_model_rest_required_fields(request_type=service.DeployModelRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).deploy_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).deploy_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.deploy_model(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_deploy_model_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.deploy_model._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_deploy_model_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.AutoMlRestInterceptor, "post_deploy_model" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_deploy_model" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.DeployModelRequest.pb(service.DeployModelRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = service.DeployModelRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.deploy_model( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_deploy_model_rest_bad_request( + transport: str = "rest", request_type=service.DeployModelRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.deploy_model(request) + + +def test_deploy_model_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/models/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.deploy_model(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{name=projects/*/locations/*/models/*}:deploy" + % client.transport._host, + args[1], + ) + + +def test_deploy_model_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.deploy_model( + service.DeployModelRequest(), + name="name_value", + ) + + +def test_deploy_model_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.UndeployModelRequest, + dict, + ], +) +def test_undeploy_model_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.undeploy_model(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_undeploy_model_rest_required_fields(request_type=service.UndeployModelRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).undeploy_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).undeploy_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.undeploy_model(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_undeploy_model_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.undeploy_model._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_undeploy_model_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.AutoMlRestInterceptor, "post_undeploy_model" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_undeploy_model" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.UndeployModelRequest.pb(service.UndeployModelRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = service.UndeployModelRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.undeploy_model( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_undeploy_model_rest_bad_request( + transport: str = "rest", request_type=service.UndeployModelRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.undeploy_model(request) + + +def test_undeploy_model_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/models/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.undeploy_model(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{name=projects/*/locations/*/models/*}:undeploy" + % client.transport._host, + args[1], + ) + + +def test_undeploy_model_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.undeploy_model( + service.UndeployModelRequest(), + name="name_value", + ) + + +def test_undeploy_model_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.ExportModelRequest, + dict, + ], +) +def test_export_model_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.export_model(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_export_model_rest_required_fields(request_type=service.ExportModelRequest): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).export_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).export_model._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.export_model(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_export_model_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.export_model._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(()) + & set( + ( + "name", + "outputConfig", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_export_model_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.AutoMlRestInterceptor, "post_export_model" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_export_model" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.ExportModelRequest.pb(service.ExportModelRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = service.ExportModelRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.export_model( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_export_model_rest_bad_request( + transport: str = "rest", request_type=service.ExportModelRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.export_model(request) + + +def test_export_model_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/models/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + output_config=io.ModelExportOutputConfig( + gcs_destination=io.GcsDestination( + output_uri_prefix="output_uri_prefix_value" + ) + ), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.export_model(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{name=projects/*/locations/*/models/*}:export" + % client.transport._host, + args[1], + ) + + +def test_export_model_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.export_model( + service.ExportModelRequest(), + name="name_value", + output_config=io.ModelExportOutputConfig( + gcs_destination=io.GcsDestination( + output_uri_prefix="output_uri_prefix_value" + ) + ), + ) + + +def test_export_model_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.ExportEvaluatedExamplesRequest, + dict, + ], +) +def test_export_evaluated_examples_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.export_evaluated_examples(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_export_evaluated_examples_rest_required_fields( + request_type=service.ExportEvaluatedExamplesRequest, +): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).export_evaluated_examples._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).export_evaluated_examples._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.export_evaluated_examples(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_export_evaluated_examples_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.export_evaluated_examples._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(()) + & set( + ( + "name", + "outputConfig", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_export_evaluated_examples_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.AutoMlRestInterceptor, "post_export_evaluated_examples" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_export_evaluated_examples" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.ExportEvaluatedExamplesRequest.pb( + service.ExportEvaluatedExamplesRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = service.ExportEvaluatedExamplesRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.export_evaluated_examples( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_export_evaluated_examples_rest_bad_request( + transport: str = "rest", request_type=service.ExportEvaluatedExamplesRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.export_evaluated_examples(request) + + +def test_export_evaluated_examples_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/models/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + output_config=io.ExportEvaluatedExamplesOutputConfig( + bigquery_destination=io.BigQueryDestination( + output_uri="output_uri_value" + ) + ), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.export_evaluated_examples(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{name=projects/*/locations/*/models/*}:exportEvaluatedExamples" + % client.transport._host, + args[1], + ) + + +def test_export_evaluated_examples_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.export_evaluated_examples( + service.ExportEvaluatedExamplesRequest(), + name="name_value", + output_config=io.ExportEvaluatedExamplesOutputConfig( + bigquery_destination=io.BigQueryDestination( + output_uri="output_uri_value" + ) + ), + ) + + +def test_export_evaluated_examples_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.GetModelEvaluationRequest, + dict, + ], +) +def test_get_model_evaluation_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/models/sample3/modelEvaluations/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = model_evaluation.ModelEvaluation( + name="name_value", + annotation_spec_id="annotation_spec_id_value", + display_name="display_name_value", + evaluated_example_count=2446, + classification_evaluation_metrics=classification.ClassificationEvaluationMetrics( + au_prc=0.634 + ), + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = model_evaluation.ModelEvaluation.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.get_model_evaluation(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, model_evaluation.ModelEvaluation) + assert response.name == "name_value" + assert response.annotation_spec_id == "annotation_spec_id_value" + assert response.display_name == "display_name_value" + assert response.evaluated_example_count == 2446 + + +def test_get_model_evaluation_rest_required_fields( + request_type=service.GetModelEvaluationRequest, +): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_model_evaluation._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_model_evaluation._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = model_evaluation.ModelEvaluation() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = model_evaluation.ModelEvaluation.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.get_model_evaluation(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_model_evaluation_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_model_evaluation._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_model_evaluation_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_get_model_evaluation" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_get_model_evaluation" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.GetModelEvaluationRequest.pb( + service.GetModelEvaluationRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = model_evaluation.ModelEvaluation.to_json( + model_evaluation.ModelEvaluation() + ) + + request = service.GetModelEvaluationRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = model_evaluation.ModelEvaluation() + + client.get_model_evaluation( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_get_model_evaluation_rest_bad_request( + transport: str = "rest", request_type=service.GetModelEvaluationRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/models/sample3/modelEvaluations/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.get_model_evaluation(request) + + +def test_get_model_evaluation_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = model_evaluation.ModelEvaluation() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/models/sample3/modelEvaluations/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = model_evaluation.ModelEvaluation.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.get_model_evaluation(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{name=projects/*/locations/*/models/*/modelEvaluations/*}" + % client.transport._host, + args[1], + ) + + +def test_get_model_evaluation_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_model_evaluation( + service.GetModelEvaluationRequest(), + name="name_value", + ) + + +def test_get_model_evaluation_rest_error(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + service.ListModelEvaluationsRequest, + dict, + ], +) +def test_list_model_evaluations_rest(request_type): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = service.ListModelEvaluationsResponse( + next_page_token="next_page_token_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = service.ListModelEvaluationsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.list_model_evaluations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListModelEvaluationsPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_model_evaluations_rest_required_fields( + request_type=service.ListModelEvaluationsRequest, +): + transport_class = transports.AutoMlRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_model_evaluations._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_model_evaluations._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = service.ListModelEvaluationsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = service.ListModelEvaluationsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.list_model_evaluations(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_model_evaluations_rest_unset_required_fields(): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_model_evaluations._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_model_evaluations_rest_interceptors(null_interceptor): + transport = transports.AutoMlRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.AutoMlRestInterceptor(), + ) + client = AutoMlClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.AutoMlRestInterceptor, "post_list_model_evaluations" + ) as post, mock.patch.object( + transports.AutoMlRestInterceptor, "pre_list_model_evaluations" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = service.ListModelEvaluationsRequest.pb( + service.ListModelEvaluationsRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = service.ListModelEvaluationsResponse.to_json( + service.ListModelEvaluationsResponse() + ) + + request = service.ListModelEvaluationsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = service.ListModelEvaluationsResponse() + + client.list_model_evaluations( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_list_model_evaluations_rest_bad_request( + transport: str = "rest", request_type=service.ListModelEvaluationsRequest +): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_model_evaluations(request) + + +def test_list_model_evaluations_rest_flattened(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = service.ListModelEvaluationsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2/models/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = service.ListModelEvaluationsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.list_model_evaluations(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{parent=projects/*/locations/*/models/*}/modelEvaluations" + % client.transport._host, + args[1], + ) + + +def test_list_model_evaluations_rest_flattened_error(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_model_evaluations( + service.ListModelEvaluationsRequest(), + parent="parent_value", + ) + + +def test_list_model_evaluations_rest_pager(transport: str = "rest"): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + service.ListModelEvaluationsResponse( + model_evaluation=[ + model_evaluation.ModelEvaluation(), + model_evaluation.ModelEvaluation(), + model_evaluation.ModelEvaluation(), + ], + next_page_token="abc", + ), + service.ListModelEvaluationsResponse( + model_evaluation=[], + next_page_token="def", + ), + service.ListModelEvaluationsResponse( + model_evaluation=[ + model_evaluation.ModelEvaluation(), + ], + next_page_token="ghi", + ), + service.ListModelEvaluationsResponse( + model_evaluation=[ + model_evaluation.ModelEvaluation(), + model_evaluation.ModelEvaluation(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + service.ListModelEvaluationsResponse.to_json(x) for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = {"parent": "projects/sample1/locations/sample2/models/sample3"} + + pager = client.list_model_evaluations(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, model_evaluation.ModelEvaluation) for i in results) + + pages = list(client.list_model_evaluations(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +def test_credentials_transport_error(): + # It is an error to provide credentials and a transport instance. + transport = transports.AutoMlGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # It is an error to provide a credentials file and a transport instance. + transport = transports.AutoMlGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = AutoMlClient( + client_options={"credentials_file": "credentials.json"}, + transport=transport, + ) + + # It is an error to provide an api_key and a transport instance. + transport = transports.AutoMlGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = AutoMlClient( + client_options=options, + transport=transport, + ) + + # It is an error to provide an api_key and a credential. + options = mock.Mock() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = AutoMlClient( + client_options=options, credentials=ga_credentials.AnonymousCredentials() + ) + + # It is an error to provide scopes and a transport instance. + transport = transports.AutoMlGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = AutoMlClient( + client_options={"scopes": ["1", "2"]}, + transport=transport, + ) + + +def test_transport_instance(): + # A client may be instantiated with a custom transport instance. + transport = transports.AutoMlGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + client = AutoMlClient(transport=transport) + assert client.transport is transport + + +def test_transport_get_channel(): + # A client may be instantiated with a custom transport instance. + transport = transports.AutoMlGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + transport = transports.AutoMlGrpcAsyncIOTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.AutoMlGrpcTransport, + transports.AutoMlGrpcAsyncIOTransport, + transports.AutoMlRestTransport, + ], +) +def test_transport_adc(transport_class): + # Test default credentials are used if not provided. + with mock.patch.object(google.auth, "default") as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class() + adc.assert_called_once() + + +@pytest.mark.parametrize( + "transport_name", + [ + "grpc", + "rest", + ], +) +def test_transport_kind(transport_name): + transport = AutoMlClient.get_transport_class(transport_name)( + credentials=ga_credentials.AnonymousCredentials(), + ) + assert transport.kind == transport_name + + +def test_transport_grpc_default(): + # A client should use the gRPC transport by default. + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + assert isinstance( + client.transport, + transports.AutoMlGrpcTransport, + ) + + +def test_auto_ml_base_transport_error(): + # Passing both a credentials object and credentials_file should raise an error + with pytest.raises(core_exceptions.DuplicateCredentialArgs): + transport = transports.AutoMlTransport( + credentials=ga_credentials.AnonymousCredentials(), + credentials_file="credentials.json", + ) + + +def test_auto_ml_base_transport(): + # Instantiate the base transport. + with mock.patch( + "google.cloud.automl_v1beta1.services.auto_ml.transports.AutoMlTransport.__init__" + ) as Transport: + Transport.return_value = None + transport = transports.AutoMlTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Every method on the transport should just blindly + # raise NotImplementedError. + methods = ( + "create_dataset", + "get_dataset", + "list_datasets", + "update_dataset", + "delete_dataset", + "import_data", + "export_data", + "get_annotation_spec", + "get_table_spec", + "list_table_specs", + "update_table_spec", + "get_column_spec", + "list_column_specs", "update_column_spec", "create_model", "get_model", @@ -7835,6 +15155,7 @@ def test_auto_ml_transport_auth_adc(transport_class): [ transports.AutoMlGrpcTransport, transports.AutoMlGrpcAsyncIOTransport, + transports.AutoMlRestTransport, ], ) def test_auto_ml_transport_auth_gdch_credentials(transport_class): @@ -7929,11 +15250,40 @@ def test_auto_ml_grpc_transport_client_cert_source_for_mtls(transport_class): ) +def test_auto_ml_http_transport_client_cert_source_for_mtls(): + cred = ga_credentials.AnonymousCredentials() + with mock.patch( + "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" + ) as mock_configure_mtls_channel: + transports.AutoMlRestTransport( + credentials=cred, client_cert_source_for_mtls=client_cert_source_callback + ) + mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) + + +def test_auto_ml_rest_lro_client(): + client = AutoMlClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + transport = client.transport + + # Ensure that we have a api-core operations client. + assert isinstance( + transport.operations_client, + operations_v1.AbstractOperationsClient, + ) + + # Ensure that subsequent calls to the property send the exact same object. + assert transport.operations_client is transport.operations_client + + @pytest.mark.parametrize( "transport_name", [ "grpc", "grpc_asyncio", + "rest", ], ) def test_auto_ml_host_no_port(transport_name): @@ -7944,7 +15294,11 @@ def test_auto_ml_host_no_port(transport_name): ), transport=transport_name, ) - assert client.transport._host == ("automl.googleapis.com:443") + assert client.transport._host == ( + "automl.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://automl.googleapis.com" + ) @pytest.mark.parametrize( @@ -7952,6 +15306,7 @@ def test_auto_ml_host_no_port(transport_name): [ "grpc", "grpc_asyncio", + "rest", ], ) def test_auto_ml_host_with_port(transport_name): @@ -7962,7 +15317,102 @@ def test_auto_ml_host_with_port(transport_name): ), transport=transport_name, ) - assert client.transport._host == ("automl.googleapis.com:8000") + assert client.transport._host == ( + "automl.googleapis.com:8000" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://automl.googleapis.com:8000" + ) + + +@pytest.mark.parametrize( + "transport_name", + [ + "rest", + ], +) +def test_auto_ml_client_transport_session_collision(transport_name): + creds1 = ga_credentials.AnonymousCredentials() + creds2 = ga_credentials.AnonymousCredentials() + client1 = AutoMlClient( + credentials=creds1, + transport=transport_name, + ) + client2 = AutoMlClient( + credentials=creds2, + transport=transport_name, + ) + session1 = client1.transport.create_dataset._session + session2 = client2.transport.create_dataset._session + assert session1 != session2 + session1 = client1.transport.get_dataset._session + session2 = client2.transport.get_dataset._session + assert session1 != session2 + session1 = client1.transport.list_datasets._session + session2 = client2.transport.list_datasets._session + assert session1 != session2 + session1 = client1.transport.update_dataset._session + session2 = client2.transport.update_dataset._session + assert session1 != session2 + session1 = client1.transport.delete_dataset._session + session2 = client2.transport.delete_dataset._session + assert session1 != session2 + session1 = client1.transport.import_data._session + session2 = client2.transport.import_data._session + assert session1 != session2 + session1 = client1.transport.export_data._session + session2 = client2.transport.export_data._session + assert session1 != session2 + session1 = client1.transport.get_annotation_spec._session + session2 = client2.transport.get_annotation_spec._session + assert session1 != session2 + session1 = client1.transport.get_table_spec._session + session2 = client2.transport.get_table_spec._session + assert session1 != session2 + session1 = client1.transport.list_table_specs._session + session2 = client2.transport.list_table_specs._session + assert session1 != session2 + session1 = client1.transport.update_table_spec._session + session2 = client2.transport.update_table_spec._session + assert session1 != session2 + session1 = client1.transport.get_column_spec._session + session2 = client2.transport.get_column_spec._session + assert session1 != session2 + session1 = client1.transport.list_column_specs._session + session2 = client2.transport.list_column_specs._session + assert session1 != session2 + session1 = client1.transport.update_column_spec._session + session2 = client2.transport.update_column_spec._session + assert session1 != session2 + session1 = client1.transport.create_model._session + session2 = client2.transport.create_model._session + assert session1 != session2 + session1 = client1.transport.get_model._session + session2 = client2.transport.get_model._session + assert session1 != session2 + session1 = client1.transport.list_models._session + session2 = client2.transport.list_models._session + assert session1 != session2 + session1 = client1.transport.delete_model._session + session2 = client2.transport.delete_model._session + assert session1 != session2 + session1 = client1.transport.deploy_model._session + session2 = client2.transport.deploy_model._session + assert session1 != session2 + session1 = client1.transport.undeploy_model._session + session2 = client2.transport.undeploy_model._session + assert session1 != session2 + session1 = client1.transport.export_model._session + session2 = client2.transport.export_model._session + assert session1 != session2 + session1 = client1.transport.export_evaluated_examples._session + session2 = client2.transport.export_evaluated_examples._session + assert session1 != session2 + session1 = client1.transport.get_model_evaluation._session + session2 = client2.transport.get_model_evaluation._session + assert session1 != session2 + session1 = client1.transport.list_model_evaluations._session + session2 = client2.transport.list_model_evaluations._session + assert session1 != session2 def test_auto_ml_grpc_transport_channel(): @@ -8436,6 +15886,7 @@ async def test_transport_close_async(): def test_transport_close(): transports = { + "rest": "_session", "grpc": "_grpc_channel", } @@ -8453,6 +15904,7 @@ def test_transport_close(): def test_client_ctx(): transports = [ + "rest", "grpc", ] for transport in transports: diff --git a/tests/unit/gapic/automl_v1beta1/test_prediction_service.py b/tests/unit/gapic/automl_v1beta1/test_prediction_service.py index 2d80be63..eb1d4b2f 100644 --- a/tests/unit/gapic/automl_v1beta1/test_prediction_service.py +++ b/tests/unit/gapic/automl_v1beta1/test_prediction_service.py @@ -24,10 +24,17 @@ import grpc from grpc.experimental import aio +from collections.abc import Iterable +from google.protobuf import json_format +import json import math import pytest from proto.marshal.rules.dates import DurationRule, TimestampRule from proto.marshal.rules import wrappers +from requests import Response +from requests import Request, PreparedRequest +from requests.sessions import Session +from google.protobuf import json_format from google.api_core import client_options from google.api_core import exceptions as core_exceptions @@ -111,6 +118,7 @@ def test__get_default_mtls_endpoint(): [ (PredictionServiceClient, "grpc"), (PredictionServiceAsyncClient, "grpc_asyncio"), + (PredictionServiceClient, "rest"), ], ) def test_prediction_service_client_from_service_account_info( @@ -126,7 +134,11 @@ def test_prediction_service_client_from_service_account_info( assert client.transport._credentials == creds assert isinstance(client, client_class) - assert client.transport._host == ("automl.googleapis.com:443") + assert client.transport._host == ( + "automl.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://automl.googleapis.com" + ) @pytest.mark.parametrize( @@ -134,6 +146,7 @@ def test_prediction_service_client_from_service_account_info( [ (transports.PredictionServiceGrpcTransport, "grpc"), (transports.PredictionServiceGrpcAsyncIOTransport, "grpc_asyncio"), + (transports.PredictionServiceRestTransport, "rest"), ], ) def test_prediction_service_client_service_account_always_use_jwt( @@ -159,6 +172,7 @@ def test_prediction_service_client_service_account_always_use_jwt( [ (PredictionServiceClient, "grpc"), (PredictionServiceAsyncClient, "grpc_asyncio"), + (PredictionServiceClient, "rest"), ], ) def test_prediction_service_client_from_service_account_file( @@ -181,13 +195,18 @@ def test_prediction_service_client_from_service_account_file( assert client.transport._credentials == creds assert isinstance(client, client_class) - assert client.transport._host == ("automl.googleapis.com:443") + assert client.transport._host == ( + "automl.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://automl.googleapis.com" + ) def test_prediction_service_client_get_transport_class(): transport = PredictionServiceClient.get_transport_class() available_transports = [ transports.PredictionServiceGrpcTransport, + transports.PredictionServiceRestTransport, ] assert transport in available_transports @@ -204,6 +223,7 @@ def test_prediction_service_client_get_transport_class(): transports.PredictionServiceGrpcAsyncIOTransport, "grpc_asyncio", ), + (PredictionServiceClient, transports.PredictionServiceRestTransport, "rest"), ], ) @mock.patch.object( @@ -359,6 +379,18 @@ def test_prediction_service_client_client_options( "grpc_asyncio", "false", ), + ( + PredictionServiceClient, + transports.PredictionServiceRestTransport, + "rest", + "true", + ), + ( + PredictionServiceClient, + transports.PredictionServiceRestTransport, + "rest", + "false", + ), ], ) @mock.patch.object( @@ -558,6 +590,7 @@ def test_prediction_service_client_get_mtls_endpoint_and_cert_source(client_clas transports.PredictionServiceGrpcAsyncIOTransport, "grpc_asyncio", ), + (PredictionServiceClient, transports.PredictionServiceRestTransport, "rest"), ], ) def test_prediction_service_client_client_options_scopes( @@ -598,6 +631,12 @@ def test_prediction_service_client_client_options_scopes( "grpc_asyncio", grpc_helpers_async, ), + ( + PredictionServiceClient, + transports.PredictionServiceRestTransport, + "rest", + None, + ), ], ) def test_prediction_service_client_client_options_credentials_file( @@ -1260,6 +1299,578 @@ async def test_batch_predict_flattened_error_async(): ) +@pytest.mark.parametrize( + "request_type", + [ + prediction_service.PredictRequest, + dict, + ], +) +def test_predict_rest(request_type): + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = prediction_service.PredictResponse() + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = prediction_service.PredictResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.predict(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, prediction_service.PredictResponse) + + +def test_predict_rest_required_fields(request_type=prediction_service.PredictRequest): + transport_class = transports.PredictionServiceRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).predict._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).predict._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = prediction_service.PredictResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = prediction_service.PredictResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.predict(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_predict_rest_unset_required_fields(): + transport = transports.PredictionServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.predict._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(()) + & set( + ( + "name", + "payload", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_predict_rest_interceptors(null_interceptor): + transport = transports.PredictionServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.PredictionServiceRestInterceptor(), + ) + client = PredictionServiceClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.PredictionServiceRestInterceptor, "post_predict" + ) as post, mock.patch.object( + transports.PredictionServiceRestInterceptor, "pre_predict" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = prediction_service.PredictRequest.pb( + prediction_service.PredictRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = prediction_service.PredictResponse.to_json( + prediction_service.PredictResponse() + ) + + request = prediction_service.PredictRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = prediction_service.PredictResponse() + + client.predict( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_predict_rest_bad_request( + transport: str = "rest", request_type=prediction_service.PredictRequest +): + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.predict(request) + + +def test_predict_rest_flattened(): + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = prediction_service.PredictResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/models/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + payload=data_items.ExamplePayload( + image=data_items.Image(image_bytes=b"image_bytes_blob") + ), + params={"key_value": "value_value"}, + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = prediction_service.PredictResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.predict(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{name=projects/*/locations/*/models/*}:predict" + % client.transport._host, + args[1], + ) + + +def test_predict_rest_flattened_error(transport: str = "rest"): + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.predict( + prediction_service.PredictRequest(), + name="name_value", + payload=data_items.ExamplePayload( + image=data_items.Image(image_bytes=b"image_bytes_blob") + ), + params={"key_value": "value_value"}, + ) + + +def test_predict_rest_error(): + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + prediction_service.BatchPredictRequest, + dict, + ], +) +def test_batch_predict_rest(request_type): + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.batch_predict(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_batch_predict_rest_required_fields( + request_type=prediction_service.BatchPredictRequest, +): + transport_class = transports.PredictionServiceRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).batch_predict._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).batch_predict._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.batch_predict(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_batch_predict_rest_unset_required_fields(): + transport = transports.PredictionServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.batch_predict._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(()) + & set( + ( + "name", + "inputConfig", + "outputConfig", + "params", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_batch_predict_rest_interceptors(null_interceptor): + transport = transports.PredictionServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.PredictionServiceRestInterceptor(), + ) + client = PredictionServiceClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.PredictionServiceRestInterceptor, "post_batch_predict" + ) as post, mock.patch.object( + transports.PredictionServiceRestInterceptor, "pre_batch_predict" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = prediction_service.BatchPredictRequest.pb( + prediction_service.BatchPredictRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = prediction_service.BatchPredictRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.batch_predict( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_batch_predict_rest_bad_request( + transport: str = "rest", request_type=prediction_service.BatchPredictRequest +): + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/models/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.batch_predict(request) + + +def test_batch_predict_rest_flattened(): + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/models/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + input_config=io.BatchPredictInputConfig( + gcs_source=io.GcsSource(input_uris=["input_uris_value"]) + ), + output_config=io.BatchPredictOutputConfig( + gcs_destination=io.GcsDestination( + output_uri_prefix="output_uri_prefix_value" + ) + ), + params={"key_value": "value_value"}, + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.batch_predict(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{name=projects/*/locations/*/models/*}:batchPredict" + % client.transport._host, + args[1], + ) + + +def test_batch_predict_rest_flattened_error(transport: str = "rest"): + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.batch_predict( + prediction_service.BatchPredictRequest(), + name="name_value", + input_config=io.BatchPredictInputConfig( + gcs_source=io.GcsSource(input_uris=["input_uris_value"]) + ), + output_config=io.BatchPredictOutputConfig( + gcs_destination=io.GcsDestination( + output_uri_prefix="output_uri_prefix_value" + ) + ), + params={"key_value": "value_value"}, + ) + + +def test_batch_predict_rest_error(): + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.PredictionServiceGrpcTransport( @@ -1341,6 +1952,7 @@ def test_transport_get_channel(): [ transports.PredictionServiceGrpcTransport, transports.PredictionServiceGrpcAsyncIOTransport, + transports.PredictionServiceRestTransport, ], ) def test_transport_adc(transport_class): @@ -1355,6 +1967,7 @@ def test_transport_adc(transport_class): "transport_name", [ "grpc", + "rest", ], ) def test_transport_kind(transport_name): @@ -1490,6 +2103,7 @@ def test_prediction_service_transport_auth_adc(transport_class): [ transports.PredictionServiceGrpcTransport, transports.PredictionServiceGrpcAsyncIOTransport, + transports.PredictionServiceRestTransport, ], ) def test_prediction_service_transport_auth_gdch_credentials(transport_class): @@ -1587,11 +2201,40 @@ def test_prediction_service_grpc_transport_client_cert_source_for_mtls(transport ) +def test_prediction_service_http_transport_client_cert_source_for_mtls(): + cred = ga_credentials.AnonymousCredentials() + with mock.patch( + "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" + ) as mock_configure_mtls_channel: + transports.PredictionServiceRestTransport( + credentials=cred, client_cert_source_for_mtls=client_cert_source_callback + ) + mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) + + +def test_prediction_service_rest_lro_client(): + client = PredictionServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + transport = client.transport + + # Ensure that we have a api-core operations client. + assert isinstance( + transport.operations_client, + operations_v1.AbstractOperationsClient, + ) + + # Ensure that subsequent calls to the property send the exact same object. + assert transport.operations_client is transport.operations_client + + @pytest.mark.parametrize( "transport_name", [ "grpc", "grpc_asyncio", + "rest", ], ) def test_prediction_service_host_no_port(transport_name): @@ -1602,7 +2245,11 @@ def test_prediction_service_host_no_port(transport_name): ), transport=transport_name, ) - assert client.transport._host == ("automl.googleapis.com:443") + assert client.transport._host == ( + "automl.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://automl.googleapis.com" + ) @pytest.mark.parametrize( @@ -1610,6 +2257,7 @@ def test_prediction_service_host_no_port(transport_name): [ "grpc", "grpc_asyncio", + "rest", ], ) def test_prediction_service_host_with_port(transport_name): @@ -1620,7 +2268,36 @@ def test_prediction_service_host_with_port(transport_name): ), transport=transport_name, ) - assert client.transport._host == ("automl.googleapis.com:8000") + assert client.transport._host == ( + "automl.googleapis.com:8000" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://automl.googleapis.com:8000" + ) + + +@pytest.mark.parametrize( + "transport_name", + [ + "rest", + ], +) +def test_prediction_service_client_transport_session_collision(transport_name): + creds1 = ga_credentials.AnonymousCredentials() + creds2 = ga_credentials.AnonymousCredentials() + client1 = PredictionServiceClient( + credentials=creds1, + transport=transport_name, + ) + client2 = PredictionServiceClient( + credentials=creds2, + transport=transport_name, + ) + session1 = client1.transport.predict._session + session2 = client2.transport.predict._session + assert session1 != session2 + session1 = client1.transport.batch_predict._session + session2 = client2.transport.batch_predict._session + assert session1 != session2 def test_prediction_service_grpc_transport_channel(): @@ -1951,6 +2628,7 @@ async def test_transport_close_async(): def test_transport_close(): transports = { + "rest": "_session", "grpc": "_grpc_channel", } @@ -1968,6 +2646,7 @@ def test_transport_close(): def test_client_ctx(): transports = [ + "rest", "grpc", ] for transport in transports: