diff --git a/.coveragerc b/.coveragerc index 8a76e68..0b3eb91 100644 --- a/.coveragerc +++ b/.coveragerc @@ -5,6 +5,7 @@ branch = True show_missing = True omit = google/cloud/workflows/__init__.py + google/cloud/workflows/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 889f77d..894fb6b 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:f62c53736eccb0c4934a3ea9316e0d57696bb49c1a7c86c726e9bb8a2f87dadf diff --git a/.kokoro/requirements.txt b/.kokoro/requirements.txt index 05dc467..096e480 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 diff --git a/.release-please-manifest.json b/.release-please-manifest.json index 2b6f978..d9246dd 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "1.9.1" + ".": "1.10.0" } diff --git a/CHANGELOG.md b/CHANGELOG.md index 460db54..8baf27c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [1.10.0](https://github.com/googleapis/python-workflows/compare/v1.9.1...v1.10.0) (2023-02-17) + + +### Features + +* Enable "rest" transport in Python for services supporting numeric enums ([#241](https://github.com/googleapis/python-workflows/issues/241)) ([3fc33a3](https://github.com/googleapis/python-workflows/commit/3fc33a33d2142a6c0e89cbf79959ca2984cf474d)) + ## [1.9.1](https://github.com/googleapis/python-workflows/compare/v1.9.0...v1.9.1) (2023-01-23) diff --git a/google/cloud/workflows/executions_v1/__init__.py b/google/cloud/workflows/executions_v1/__init__.py index fb930f8..ab9e915 100644 --- a/google/cloud/workflows/executions_v1/__init__.py +++ b/google/cloud/workflows/executions_v1/__init__.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # -from google.cloud.workflows.executions import gapic_version as package_version +from google.cloud.workflows.executions_v1 import gapic_version as package_version __version__ = package_version.__version__ diff --git a/google/cloud/workflows/executions_v1/gapic_version.py b/google/cloud/workflows/executions_v1/gapic_version.py index 13f1b4a..7ce1917 100644 --- a/google/cloud/workflows/executions_v1/gapic_version.py +++ b/google/cloud/workflows/executions_v1/gapic_version.py @@ -13,4 +13,4 @@ # See the License for the specific language governing permissions and # limitations under the License. # -__version__ = "1.9.1" # {x-release-please-version} +__version__ = "1.10.0" # {x-release-please-version} diff --git a/google/cloud/workflows/executions_v1beta/__init__.py b/google/cloud/workflows/executions_v1beta/__init__.py index fb930f8..a833957 100644 --- a/google/cloud/workflows/executions_v1beta/__init__.py +++ b/google/cloud/workflows/executions_v1beta/__init__.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # -from google.cloud.workflows.executions import gapic_version as package_version +from google.cloud.workflows.executions_v1beta import gapic_version as package_version __version__ = package_version.__version__ diff --git a/google/cloud/workflows/executions_v1beta/gapic_version.py b/google/cloud/workflows/executions_v1beta/gapic_version.py index 13f1b4a..7ce1917 100644 --- a/google/cloud/workflows/executions_v1beta/gapic_version.py +++ b/google/cloud/workflows/executions_v1beta/gapic_version.py @@ -13,4 +13,4 @@ # See the License for the specific language governing permissions and # limitations under the License. # -__version__ = "1.9.1" # {x-release-please-version} +__version__ = "1.10.0" # {x-release-please-version} diff --git a/google/cloud/workflows/gapic_version.py b/google/cloud/workflows/gapic_version.py index 13f1b4a..7ce1917 100644 --- a/google/cloud/workflows/gapic_version.py +++ b/google/cloud/workflows/gapic_version.py @@ -13,4 +13,4 @@ # See the License for the specific language governing permissions and # limitations under the License. # -__version__ = "1.9.1" # {x-release-please-version} +__version__ = "1.10.0" # {x-release-please-version} diff --git a/google/cloud/workflows_v1/__init__.py b/google/cloud/workflows_v1/__init__.py index f3c84b7..d81ede0 100644 --- a/google/cloud/workflows_v1/__init__.py +++ b/google/cloud/workflows_v1/__init__.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # -from google.cloud.workflows import gapic_version as package_version +from google.cloud.workflows_v1 import gapic_version as package_version __version__ = package_version.__version__ diff --git a/google/cloud/workflows_v1/gapic_metadata.json b/google/cloud/workflows_v1/gapic_metadata.json index 60ac0dc..a41eecf 100644 --- a/google/cloud/workflows_v1/gapic_metadata.json +++ b/google/cloud/workflows_v1/gapic_metadata.json @@ -66,6 +66,36 @@ ] } } + }, + "rest": { + "libraryClient": "WorkflowsClient", + "rpcs": { + "CreateWorkflow": { + "methods": [ + "create_workflow" + ] + }, + "DeleteWorkflow": { + "methods": [ + "delete_workflow" + ] + }, + "GetWorkflow": { + "methods": [ + "get_workflow" + ] + }, + "ListWorkflows": { + "methods": [ + "list_workflows" + ] + }, + "UpdateWorkflow": { + "methods": [ + "update_workflow" + ] + } + } } } } diff --git a/google/cloud/workflows_v1/gapic_version.py b/google/cloud/workflows_v1/gapic_version.py index 13f1b4a..7ce1917 100644 --- a/google/cloud/workflows_v1/gapic_version.py +++ b/google/cloud/workflows_v1/gapic_version.py @@ -13,4 +13,4 @@ # See the License for the specific language governing permissions and # limitations under the License. # -__version__ = "1.9.1" # {x-release-please-version} +__version__ = "1.10.0" # {x-release-please-version} diff --git a/google/cloud/workflows_v1/services/workflows/client.py b/google/cloud/workflows_v1/services/workflows/client.py index bb5fcd3..7942865 100644 --- a/google/cloud/workflows_v1/services/workflows/client.py +++ b/google/cloud/workflows_v1/services/workflows/client.py @@ -58,6 +58,7 @@ from .transports.base import DEFAULT_CLIENT_INFO, WorkflowsTransport from .transports.grpc import WorkflowsGrpcTransport from .transports.grpc_asyncio import WorkflowsGrpcAsyncIOTransport +from .transports.rest import WorkflowsRestTransport class WorkflowsClientMeta(type): @@ -71,6 +72,7 @@ class WorkflowsClientMeta(type): _transport_registry = OrderedDict() # type: Dict[str, Type[WorkflowsTransport]] _transport_registry["grpc"] = WorkflowsGrpcTransport _transport_registry["grpc_asyncio"] = WorkflowsGrpcAsyncIOTransport + _transport_registry["rest"] = WorkflowsRestTransport def get_transport_class( cls, diff --git a/google/cloud/workflows_v1/services/workflows/transports/__init__.py b/google/cloud/workflows_v1/services/workflows/transports/__init__.py index 3508736..bb7fa05 100644 --- a/google/cloud/workflows_v1/services/workflows/transports/__init__.py +++ b/google/cloud/workflows_v1/services/workflows/transports/__init__.py @@ -19,14 +19,18 @@ from .base import WorkflowsTransport from .grpc import WorkflowsGrpcTransport from .grpc_asyncio import WorkflowsGrpcAsyncIOTransport +from .rest import WorkflowsRestInterceptor, WorkflowsRestTransport # Compile a registry of transports. _transport_registry = OrderedDict() # type: Dict[str, Type[WorkflowsTransport]] _transport_registry["grpc"] = WorkflowsGrpcTransport _transport_registry["grpc_asyncio"] = WorkflowsGrpcAsyncIOTransport +_transport_registry["rest"] = WorkflowsRestTransport __all__ = ( "WorkflowsTransport", "WorkflowsGrpcTransport", "WorkflowsGrpcAsyncIOTransport", + "WorkflowsRestTransport", + "WorkflowsRestInterceptor", ) diff --git a/google/cloud/workflows_v1/services/workflows/transports/rest.py b/google/cloud/workflows_v1/services/workflows/transports/rest.py new file mode 100644 index 0000000..19b6c83 --- /dev/null +++ b/google/cloud/workflows_v1/services/workflows/transports/rest.py @@ -0,0 +1,898 @@ +# -*- 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. +# + +import dataclasses +import json # type: ignore +import re +from typing import Callable, Dict, List, Optional, Sequence, Tuple, Union +import warnings + +from google.api_core import ( + gapic_v1, + operations_v1, + path_template, + rest_helpers, + rest_streaming, +) +from google.api_core import exceptions as core_exceptions +from google.api_core import retry as retries +from google.auth import credentials as ga_credentials # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore +from google.auth.transport.requests import AuthorizedSession # type: ignore +from google.protobuf import json_format +import grpc # type: ignore +from requests import __version__ as requests_version + +try: + OptionalRetry = Union[retries.Retry, gapic_v1.method._MethodDefault] +except AttributeError: # pragma: NO COVER + OptionalRetry = Union[retries.Retry, object] # type: ignore + + +from google.longrunning import operations_pb2 # type: ignore + +from google.cloud.workflows_v1.types import workflows + +from .base import DEFAULT_CLIENT_INFO as BASE_DEFAULT_CLIENT_INFO +from .base import WorkflowsTransport + +DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=BASE_DEFAULT_CLIENT_INFO.gapic_version, + grpc_version=None, + rest_version=requests_version, +) + + +class WorkflowsRestInterceptor: + """Interceptor for Workflows. + + 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 WorkflowsRestTransport. + + .. code-block:: python + class MyCustomWorkflowsInterceptor(WorkflowsRestInterceptor): + def pre_create_workflow(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_create_workflow(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_delete_workflow(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_delete_workflow(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_get_workflow(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_workflow(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_list_workflows(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_workflows(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_update_workflow(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_update_workflow(self, response): + logging.log(f"Received response: {response}") + return response + + transport = WorkflowsRestTransport(interceptor=MyCustomWorkflowsInterceptor()) + client = WorkflowsClient(transport=transport) + + + """ + + def pre_create_workflow( + self, + request: workflows.CreateWorkflowRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[workflows.CreateWorkflowRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for create_workflow + + Override in a subclass to manipulate the request or metadata + before they are sent to the Workflows server. + """ + return request, metadata + + def post_create_workflow( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for create_workflow + + Override in a subclass to manipulate the response + after it is returned by the Workflows server but before + it is returned to user code. + """ + return response + + def pre_delete_workflow( + self, + request: workflows.DeleteWorkflowRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[workflows.DeleteWorkflowRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for delete_workflow + + Override in a subclass to manipulate the request or metadata + before they are sent to the Workflows server. + """ + return request, metadata + + def post_delete_workflow( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for delete_workflow + + Override in a subclass to manipulate the response + after it is returned by the Workflows server but before + it is returned to user code. + """ + return response + + def pre_get_workflow( + self, request: workflows.GetWorkflowRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[workflows.GetWorkflowRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_workflow + + Override in a subclass to manipulate the request or metadata + before they are sent to the Workflows server. + """ + return request, metadata + + def post_get_workflow(self, response: workflows.Workflow) -> workflows.Workflow: + """Post-rpc interceptor for get_workflow + + Override in a subclass to manipulate the response + after it is returned by the Workflows server but before + it is returned to user code. + """ + return response + + def pre_list_workflows( + self, + request: workflows.ListWorkflowsRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[workflows.ListWorkflowsRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for list_workflows + + Override in a subclass to manipulate the request or metadata + before they are sent to the Workflows server. + """ + return request, metadata + + def post_list_workflows( + self, response: workflows.ListWorkflowsResponse + ) -> workflows.ListWorkflowsResponse: + """Post-rpc interceptor for list_workflows + + Override in a subclass to manipulate the response + after it is returned by the Workflows server but before + it is returned to user code. + """ + return response + + def pre_update_workflow( + self, + request: workflows.UpdateWorkflowRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[workflows.UpdateWorkflowRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for update_workflow + + Override in a subclass to manipulate the request or metadata + before they are sent to the Workflows server. + """ + return request, metadata + + def post_update_workflow( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for update_workflow + + Override in a subclass to manipulate the response + after it is returned by the Workflows server but before + it is returned to user code. + """ + return response + + +@dataclasses.dataclass +class WorkflowsRestStub: + _session: AuthorizedSession + _host: str + _interceptor: WorkflowsRestInterceptor + + +class WorkflowsRestTransport(WorkflowsTransport): + """REST backend transport for Workflows. + + Workflows is used to deploy and execute workflow programs. + Workflows makes sure the program executes reliably, despite + hardware and networking interruptions. + + 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 = "workflows.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[WorkflowsRestInterceptor] = 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 WorkflowsRestInterceptor() + 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.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", + }, + ], + } + + 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 _CreateWorkflow(WorkflowsRestStub): + def __hash__(self): + return hash("CreateWorkflow") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = { + "workflowId": "", + } + + @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: workflows.CreateWorkflowRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the create workflow method over HTTP. + + Args: + request (~.workflows.CreateWorkflowRequest): + The request object. Request for the + [CreateWorkflow][google.cloud.workflows.v1.Workflows.CreateWorkflow] + method. + + 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/*}/workflows", + "body": "workflow", + }, + ] + request, metadata = self._interceptor.pre_create_workflow(request, metadata) + pb_request = workflows.CreateWorkflowRequest.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_workflow(resp) + return resp + + class _DeleteWorkflow(WorkflowsRestStub): + def __hash__(self): + return hash("DeleteWorkflow") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @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: workflows.DeleteWorkflowRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the delete workflow method over HTTP. + + Args: + request (~.workflows.DeleteWorkflowRequest): + The request object. Request for the + [DeleteWorkflow][google.cloud.workflows.v1.Workflows.DeleteWorkflow] + method. + + 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/*/workflows/*}", + }, + ] + request, metadata = self._interceptor.pre_delete_workflow(request, metadata) + pb_request = workflows.DeleteWorkflowRequest.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_workflow(resp) + return resp + + class _GetWorkflow(WorkflowsRestStub): + def __hash__(self): + return hash("GetWorkflow") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @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: workflows.GetWorkflowRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> workflows.Workflow: + r"""Call the get workflow method over HTTP. + + Args: + request (~.workflows.GetWorkflowRequest): + The request object. Request for the + [GetWorkflow][google.cloud.workflows.v1.Workflows.GetWorkflow] + method. + + 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: + ~.workflows.Workflow: + Workflow program to be executed by + Workflows. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/workflows/*}", + }, + ] + request, metadata = self._interceptor.pre_get_workflow(request, metadata) + pb_request = workflows.GetWorkflowRequest.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 = workflows.Workflow() + pb_resp = workflows.Workflow.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_workflow(resp) + return resp + + class _ListWorkflows(WorkflowsRestStub): + def __hash__(self): + return hash("ListWorkflows") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @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: workflows.ListWorkflowsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> workflows.ListWorkflowsResponse: + r"""Call the list workflows method over HTTP. + + Args: + request (~.workflows.ListWorkflowsRequest): + The request object. Request for the + [ListWorkflows][google.cloud.workflows.v1.Workflows.ListWorkflows] + method. + + 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: + ~.workflows.ListWorkflowsResponse: + Response for the + [ListWorkflows][google.cloud.workflows.v1.Workflows.ListWorkflows] + method. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{parent=projects/*/locations/*}/workflows", + }, + ] + request, metadata = self._interceptor.pre_list_workflows(request, metadata) + pb_request = workflows.ListWorkflowsRequest.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 = workflows.ListWorkflowsResponse() + pb_resp = workflows.ListWorkflowsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_workflows(resp) + return resp + + class _UpdateWorkflow(WorkflowsRestStub): + def __hash__(self): + return hash("UpdateWorkflow") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @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: workflows.UpdateWorkflowRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the update workflow method over HTTP. + + Args: + request (~.workflows.UpdateWorkflowRequest): + The request object. Request for the + [UpdateWorkflow][google.cloud.workflows.v1.Workflows.UpdateWorkflow] + method. + + 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": "patch", + "uri": "/v1/{workflow.name=projects/*/locations/*/workflows/*}", + "body": "workflow", + }, + ] + request, metadata = self._interceptor.pre_update_workflow(request, metadata) + pb_request = workflows.UpdateWorkflowRequest.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_update_workflow(resp) + return resp + + @property + def create_workflow( + self, + ) -> Callable[[workflows.CreateWorkflowRequest], 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._CreateWorkflow(self._session, self._host, self._interceptor) # type: ignore + + @property + def delete_workflow( + self, + ) -> Callable[[workflows.DeleteWorkflowRequest], 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._DeleteWorkflow(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_workflow( + self, + ) -> Callable[[workflows.GetWorkflowRequest], workflows.Workflow]: + # 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._GetWorkflow(self._session, self._host, self._interceptor) # type: ignore + + @property + def list_workflows( + self, + ) -> Callable[[workflows.ListWorkflowsRequest], workflows.ListWorkflowsResponse]: + # 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._ListWorkflows(self._session, self._host, self._interceptor) # type: ignore + + @property + def update_workflow( + self, + ) -> Callable[[workflows.UpdateWorkflowRequest], 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._UpdateWorkflow(self._session, self._host, self._interceptor) # type: ignore + + @property + def kind(self) -> str: + return "rest" + + def close(self): + self._session.close() + + +__all__ = ("WorkflowsRestTransport",) diff --git a/google/cloud/workflows_v1beta/__init__.py b/google/cloud/workflows_v1beta/__init__.py index f3c84b7..8957d18 100644 --- a/google/cloud/workflows_v1beta/__init__.py +++ b/google/cloud/workflows_v1beta/__init__.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # -from google.cloud.workflows import gapic_version as package_version +from google.cloud.workflows_v1beta import gapic_version as package_version __version__ = package_version.__version__ diff --git a/google/cloud/workflows_v1beta/gapic_metadata.json b/google/cloud/workflows_v1beta/gapic_metadata.json index f56aae9..2884682 100644 --- a/google/cloud/workflows_v1beta/gapic_metadata.json +++ b/google/cloud/workflows_v1beta/gapic_metadata.json @@ -66,6 +66,36 @@ ] } } + }, + "rest": { + "libraryClient": "WorkflowsClient", + "rpcs": { + "CreateWorkflow": { + "methods": [ + "create_workflow" + ] + }, + "DeleteWorkflow": { + "methods": [ + "delete_workflow" + ] + }, + "GetWorkflow": { + "methods": [ + "get_workflow" + ] + }, + "ListWorkflows": { + "methods": [ + "list_workflows" + ] + }, + "UpdateWorkflow": { + "methods": [ + "update_workflow" + ] + } + } } } } diff --git a/google/cloud/workflows_v1beta/gapic_version.py b/google/cloud/workflows_v1beta/gapic_version.py index 13f1b4a..7ce1917 100644 --- a/google/cloud/workflows_v1beta/gapic_version.py +++ b/google/cloud/workflows_v1beta/gapic_version.py @@ -13,4 +13,4 @@ # See the License for the specific language governing permissions and # limitations under the License. # -__version__ = "1.9.1" # {x-release-please-version} +__version__ = "1.10.0" # {x-release-please-version} diff --git a/google/cloud/workflows_v1beta/services/workflows/client.py b/google/cloud/workflows_v1beta/services/workflows/client.py index b7016cd..5a762b0 100644 --- a/google/cloud/workflows_v1beta/services/workflows/client.py +++ b/google/cloud/workflows_v1beta/services/workflows/client.py @@ -58,6 +58,7 @@ from .transports.base import DEFAULT_CLIENT_INFO, WorkflowsTransport from .transports.grpc import WorkflowsGrpcTransport from .transports.grpc_asyncio import WorkflowsGrpcAsyncIOTransport +from .transports.rest import WorkflowsRestTransport class WorkflowsClientMeta(type): @@ -71,6 +72,7 @@ class WorkflowsClientMeta(type): _transport_registry = OrderedDict() # type: Dict[str, Type[WorkflowsTransport]] _transport_registry["grpc"] = WorkflowsGrpcTransport _transport_registry["grpc_asyncio"] = WorkflowsGrpcAsyncIOTransport + _transport_registry["rest"] = WorkflowsRestTransport def get_transport_class( cls, diff --git a/google/cloud/workflows_v1beta/services/workflows/transports/__init__.py b/google/cloud/workflows_v1beta/services/workflows/transports/__init__.py index 3508736..bb7fa05 100644 --- a/google/cloud/workflows_v1beta/services/workflows/transports/__init__.py +++ b/google/cloud/workflows_v1beta/services/workflows/transports/__init__.py @@ -19,14 +19,18 @@ from .base import WorkflowsTransport from .grpc import WorkflowsGrpcTransport from .grpc_asyncio import WorkflowsGrpcAsyncIOTransport +from .rest import WorkflowsRestInterceptor, WorkflowsRestTransport # Compile a registry of transports. _transport_registry = OrderedDict() # type: Dict[str, Type[WorkflowsTransport]] _transport_registry["grpc"] = WorkflowsGrpcTransport _transport_registry["grpc_asyncio"] = WorkflowsGrpcAsyncIOTransport +_transport_registry["rest"] = WorkflowsRestTransport __all__ = ( "WorkflowsTransport", "WorkflowsGrpcTransport", "WorkflowsGrpcAsyncIOTransport", + "WorkflowsRestTransport", + "WorkflowsRestInterceptor", ) diff --git a/google/cloud/workflows_v1beta/services/workflows/transports/rest.py b/google/cloud/workflows_v1beta/services/workflows/transports/rest.py new file mode 100644 index 0000000..4f21e7d --- /dev/null +++ b/google/cloud/workflows_v1beta/services/workflows/transports/rest.py @@ -0,0 +1,898 @@ +# -*- 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. +# + +import dataclasses +import json # type: ignore +import re +from typing import Callable, Dict, List, Optional, Sequence, Tuple, Union +import warnings + +from google.api_core import ( + gapic_v1, + operations_v1, + path_template, + rest_helpers, + rest_streaming, +) +from google.api_core import exceptions as core_exceptions +from google.api_core import retry as retries +from google.auth import credentials as ga_credentials # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore +from google.auth.transport.requests import AuthorizedSession # type: ignore +from google.protobuf import json_format +import grpc # type: ignore +from requests import __version__ as requests_version + +try: + OptionalRetry = Union[retries.Retry, gapic_v1.method._MethodDefault] +except AttributeError: # pragma: NO COVER + OptionalRetry = Union[retries.Retry, object] # type: ignore + + +from google.longrunning import operations_pb2 # type: ignore + +from google.cloud.workflows_v1beta.types import workflows + +from .base import DEFAULT_CLIENT_INFO as BASE_DEFAULT_CLIENT_INFO +from .base import WorkflowsTransport + +DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=BASE_DEFAULT_CLIENT_INFO.gapic_version, + grpc_version=None, + rest_version=requests_version, +) + + +class WorkflowsRestInterceptor: + """Interceptor for Workflows. + + 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 WorkflowsRestTransport. + + .. code-block:: python + class MyCustomWorkflowsInterceptor(WorkflowsRestInterceptor): + def pre_create_workflow(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_create_workflow(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_delete_workflow(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_delete_workflow(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_get_workflow(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_workflow(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_list_workflows(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_workflows(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_update_workflow(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_update_workflow(self, response): + logging.log(f"Received response: {response}") + return response + + transport = WorkflowsRestTransport(interceptor=MyCustomWorkflowsInterceptor()) + client = WorkflowsClient(transport=transport) + + + """ + + def pre_create_workflow( + self, + request: workflows.CreateWorkflowRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[workflows.CreateWorkflowRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for create_workflow + + Override in a subclass to manipulate the request or metadata + before they are sent to the Workflows server. + """ + return request, metadata + + def post_create_workflow( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for create_workflow + + Override in a subclass to manipulate the response + after it is returned by the Workflows server but before + it is returned to user code. + """ + return response + + def pre_delete_workflow( + self, + request: workflows.DeleteWorkflowRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[workflows.DeleteWorkflowRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for delete_workflow + + Override in a subclass to manipulate the request or metadata + before they are sent to the Workflows server. + """ + return request, metadata + + def post_delete_workflow( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for delete_workflow + + Override in a subclass to manipulate the response + after it is returned by the Workflows server but before + it is returned to user code. + """ + return response + + def pre_get_workflow( + self, request: workflows.GetWorkflowRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[workflows.GetWorkflowRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_workflow + + Override in a subclass to manipulate the request or metadata + before they are sent to the Workflows server. + """ + return request, metadata + + def post_get_workflow(self, response: workflows.Workflow) -> workflows.Workflow: + """Post-rpc interceptor for get_workflow + + Override in a subclass to manipulate the response + after it is returned by the Workflows server but before + it is returned to user code. + """ + return response + + def pre_list_workflows( + self, + request: workflows.ListWorkflowsRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[workflows.ListWorkflowsRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for list_workflows + + Override in a subclass to manipulate the request or metadata + before they are sent to the Workflows server. + """ + return request, metadata + + def post_list_workflows( + self, response: workflows.ListWorkflowsResponse + ) -> workflows.ListWorkflowsResponse: + """Post-rpc interceptor for list_workflows + + Override in a subclass to manipulate the response + after it is returned by the Workflows server but before + it is returned to user code. + """ + return response + + def pre_update_workflow( + self, + request: workflows.UpdateWorkflowRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[workflows.UpdateWorkflowRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for update_workflow + + Override in a subclass to manipulate the request or metadata + before they are sent to the Workflows server. + """ + return request, metadata + + def post_update_workflow( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for update_workflow + + Override in a subclass to manipulate the response + after it is returned by the Workflows server but before + it is returned to user code. + """ + return response + + +@dataclasses.dataclass +class WorkflowsRestStub: + _session: AuthorizedSession + _host: str + _interceptor: WorkflowsRestInterceptor + + +class WorkflowsRestTransport(WorkflowsTransport): + """REST backend transport for Workflows. + + Workflows is used to deploy and execute workflow programs. + Workflows makes sure the program executes reliably, despite + hardware and networking interruptions. + + 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 = "workflows.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[WorkflowsRestInterceptor] = 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 WorkflowsRestInterceptor() + 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.DeleteOperation": [ + { + "method": "delete", + "uri": "/v1beta/{name=projects/*/locations/*/operations/*}", + }, + ], + "google.longrunning.Operations.GetOperation": [ + { + "method": "get", + "uri": "/v1beta/{name=projects/*/locations/*/operations/*}", + }, + ], + "google.longrunning.Operations.ListOperations": [ + { + "method": "get", + "uri": "/v1beta/{name=projects/*/locations/*}/operations", + }, + ], + } + + 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="v1beta", + ) + + self._operations_client = operations_v1.AbstractOperationsClient( + transport=rest_transport + ) + + # Return the client from cache. + return self._operations_client + + class _CreateWorkflow(WorkflowsRestStub): + def __hash__(self): + return hash("CreateWorkflow") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = { + "workflowId": "", + } + + @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: workflows.CreateWorkflowRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the create workflow method over HTTP. + + Args: + request (~.workflows.CreateWorkflowRequest): + The request object. Request for the + [CreateWorkflow][google.cloud.workflows.v1beta.Workflows.CreateWorkflow] + method. + + 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": "/v1beta/{parent=projects/*/locations/*}/workflows", + "body": "workflow", + }, + ] + request, metadata = self._interceptor.pre_create_workflow(request, metadata) + pb_request = workflows.CreateWorkflowRequest.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_workflow(resp) + return resp + + class _DeleteWorkflow(WorkflowsRestStub): + def __hash__(self): + return hash("DeleteWorkflow") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @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: workflows.DeleteWorkflowRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the delete workflow method over HTTP. + + Args: + request (~.workflows.DeleteWorkflowRequest): + The request object. Request for the + [DeleteWorkflow][google.cloud.workflows.v1beta.Workflows.DeleteWorkflow] + method. + + 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": "/v1beta/{name=projects/*/locations/*/workflows/*}", + }, + ] + request, metadata = self._interceptor.pre_delete_workflow(request, metadata) + pb_request = workflows.DeleteWorkflowRequest.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_workflow(resp) + return resp + + class _GetWorkflow(WorkflowsRestStub): + def __hash__(self): + return hash("GetWorkflow") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @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: workflows.GetWorkflowRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> workflows.Workflow: + r"""Call the get workflow method over HTTP. + + Args: + request (~.workflows.GetWorkflowRequest): + The request object. Request for the + [GetWorkflow][google.cloud.workflows.v1beta.Workflows.GetWorkflow] + method. + + 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: + ~.workflows.Workflow: + Workflow program to be executed by + Workflows. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1beta/{name=projects/*/locations/*/workflows/*}", + }, + ] + request, metadata = self._interceptor.pre_get_workflow(request, metadata) + pb_request = workflows.GetWorkflowRequest.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 = workflows.Workflow() + pb_resp = workflows.Workflow.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_workflow(resp) + return resp + + class _ListWorkflows(WorkflowsRestStub): + def __hash__(self): + return hash("ListWorkflows") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @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: workflows.ListWorkflowsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> workflows.ListWorkflowsResponse: + r"""Call the list workflows method over HTTP. + + Args: + request (~.workflows.ListWorkflowsRequest): + The request object. Request for the + [ListWorkflows][google.cloud.workflows.v1beta.Workflows.ListWorkflows] + method. + + 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: + ~.workflows.ListWorkflowsResponse: + Response for the + [ListWorkflows][google.cloud.workflows.v1beta.Workflows.ListWorkflows] + method. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1beta/{parent=projects/*/locations/*}/workflows", + }, + ] + request, metadata = self._interceptor.pre_list_workflows(request, metadata) + pb_request = workflows.ListWorkflowsRequest.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 = workflows.ListWorkflowsResponse() + pb_resp = workflows.ListWorkflowsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_workflows(resp) + return resp + + class _UpdateWorkflow(WorkflowsRestStub): + def __hash__(self): + return hash("UpdateWorkflow") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @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: workflows.UpdateWorkflowRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the update workflow method over HTTP. + + Args: + request (~.workflows.UpdateWorkflowRequest): + The request object. Request for the + [UpdateWorkflow][google.cloud.workflows.v1beta.Workflows.UpdateWorkflow] + method. + + 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": "patch", + "uri": "/v1beta/{workflow.name=projects/*/locations/*/workflows/*}", + "body": "workflow", + }, + ] + request, metadata = self._interceptor.pre_update_workflow(request, metadata) + pb_request = workflows.UpdateWorkflowRequest.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_update_workflow(resp) + return resp + + @property + def create_workflow( + self, + ) -> Callable[[workflows.CreateWorkflowRequest], 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._CreateWorkflow(self._session, self._host, self._interceptor) # type: ignore + + @property + def delete_workflow( + self, + ) -> Callable[[workflows.DeleteWorkflowRequest], 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._DeleteWorkflow(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_workflow( + self, + ) -> Callable[[workflows.GetWorkflowRequest], workflows.Workflow]: + # 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._GetWorkflow(self._session, self._host, self._interceptor) # type: ignore + + @property + def list_workflows( + self, + ) -> Callable[[workflows.ListWorkflowsRequest], workflows.ListWorkflowsResponse]: + # 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._ListWorkflows(self._session, self._host, self._interceptor) # type: ignore + + @property + def update_workflow( + self, + ) -> Callable[[workflows.UpdateWorkflowRequest], 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._UpdateWorkflow(self._session, self._host, self._interceptor) # type: ignore + + @property + def kind(self) -> str: + return "rest" + + def close(self): + self._session.close() + + +__all__ = ("WorkflowsRestTransport",) diff --git a/noxfile.py b/noxfile.py index e716318..95e58c5 100644 --- a/noxfile.py +++ b/noxfile.py @@ -189,9 +189,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) @@ -346,9 +346,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) @@ -378,8 +376,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.workflows.v1.json b/samples/generated_samples/snippet_metadata_google.cloud.workflows.v1.json index 6b075df..76d9ad9 100644 --- a/samples/generated_samples/snippet_metadata_google.cloud.workflows.v1.json +++ b/samples/generated_samples/snippet_metadata_google.cloud.workflows.v1.json @@ -8,7 +8,7 @@ ], "language": "PYTHON", "name": "google-cloud-workflows", - "version": "1.9.1" + "version": "1.10.0" }, "snippets": [ { diff --git a/samples/generated_samples/snippet_metadata_google.cloud.workflows.v1beta.json b/samples/generated_samples/snippet_metadata_google.cloud.workflows.v1beta.json index 7c714be..4d87d2f 100644 --- a/samples/generated_samples/snippet_metadata_google.cloud.workflows.v1beta.json +++ b/samples/generated_samples/snippet_metadata_google.cloud.workflows.v1beta.json @@ -8,7 +8,7 @@ ], "language": "PYTHON", "name": "google-cloud-workflows", - "version": "1.9.1" + "version": "1.10.0" }, "snippets": [ { diff --git a/setup.py b/setup.py index 1268751..9055955 100644 --- a/setup.py +++ b/setup.py @@ -55,9 +55,7 @@ if package.startswith("google") ] -namespaces = ["google"] -if "google.cloud" in packages: - namespaces.append("google.cloud") +namespaces = ["google", "google.cloud"] setuptools.setup( name=name, diff --git a/tests/unit/gapic/workflows_v1/test_workflows.py b/tests/unit/gapic/workflows_v1/test_workflows.py index 46009d9..77f5ec2 100644 --- a/tests/unit/gapic/workflows_v1/test_workflows.py +++ b/tests/unit/gapic/workflows_v1/test_workflows.py @@ -22,6 +22,8 @@ except ImportError: # pragma: NO COVER import mock +from collections.abc import Iterable +import json import math from google.api_core import ( @@ -43,12 +45,15 @@ from google.oauth2 import service_account from google.protobuf import empty_pb2 # type: ignore from google.protobuf import field_mask_pb2 # type: ignore +from google.protobuf import json_format from google.protobuf import timestamp_pb2 # type: ignore import grpc from grpc.experimental import aio from proto.marshal.rules import wrappers from proto.marshal.rules.dates import DurationRule, TimestampRule import pytest +from requests import PreparedRequest, Request, Response +from requests.sessions import Session from google.cloud.workflows_v1.services.workflows import ( WorkflowsAsyncClient, @@ -103,6 +108,7 @@ def test__get_default_mtls_endpoint(): [ (WorkflowsClient, "grpc"), (WorkflowsAsyncClient, "grpc_asyncio"), + (WorkflowsClient, "rest"), ], ) def test_workflows_client_from_service_account_info(client_class, transport_name): @@ -116,7 +122,11 @@ def test_workflows_client_from_service_account_info(client_class, transport_name assert client.transport._credentials == creds assert isinstance(client, client_class) - assert client.transport._host == ("workflows.googleapis.com:443") + assert client.transport._host == ( + "workflows.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://workflows.googleapis.com" + ) @pytest.mark.parametrize( @@ -124,6 +134,7 @@ def test_workflows_client_from_service_account_info(client_class, transport_name [ (transports.WorkflowsGrpcTransport, "grpc"), (transports.WorkflowsGrpcAsyncIOTransport, "grpc_asyncio"), + (transports.WorkflowsRestTransport, "rest"), ], ) def test_workflows_client_service_account_always_use_jwt( @@ -149,6 +160,7 @@ def test_workflows_client_service_account_always_use_jwt( [ (WorkflowsClient, "grpc"), (WorkflowsAsyncClient, "grpc_asyncio"), + (WorkflowsClient, "rest"), ], ) def test_workflows_client_from_service_account_file(client_class, transport_name): @@ -169,13 +181,18 @@ def test_workflows_client_from_service_account_file(client_class, transport_name assert client.transport._credentials == creds assert isinstance(client, client_class) - assert client.transport._host == ("workflows.googleapis.com:443") + assert client.transport._host == ( + "workflows.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://workflows.googleapis.com" + ) def test_workflows_client_get_transport_class(): transport = WorkflowsClient.get_transport_class() available_transports = [ transports.WorkflowsGrpcTransport, + transports.WorkflowsRestTransport, ] assert transport in available_transports @@ -192,6 +209,7 @@ def test_workflows_client_get_transport_class(): transports.WorkflowsGrpcAsyncIOTransport, "grpc_asyncio", ), + (WorkflowsClient, transports.WorkflowsRestTransport, "rest"), ], ) @mock.patch.object( @@ -333,6 +351,8 @@ def test_workflows_client_client_options(client_class, transport_class, transpor "grpc_asyncio", "false", ), + (WorkflowsClient, transports.WorkflowsRestTransport, "rest", "true"), + (WorkflowsClient, transports.WorkflowsRestTransport, "rest", "false"), ], ) @mock.patch.object( @@ -526,6 +546,7 @@ def test_workflows_client_get_mtls_endpoint_and_cert_source(client_class): transports.WorkflowsGrpcAsyncIOTransport, "grpc_asyncio", ), + (WorkflowsClient, transports.WorkflowsRestTransport, "rest"), ], ) def test_workflows_client_client_options_scopes( @@ -561,6 +582,7 @@ def test_workflows_client_client_options_scopes( "grpc_asyncio", grpc_helpers_async, ), + (WorkflowsClient, transports.WorkflowsRestTransport, "rest", None), ], ) def test_workflows_client_client_options_credentials_file( @@ -2043,157 +2065,1634 @@ async def test_update_workflow_flattened_error_async(): ) -def test_credentials_transport_error(): - # It is an error to provide credentials and a transport instance. - transport = transports.WorkflowsGrpcTransport( +@pytest.mark.parametrize( + "request_type", + [ + workflows.ListWorkflowsRequest, + dict, + ], +) +def test_list_workflows_rest(request_type): + client = WorkflowsClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - with pytest.raises(ValueError): - client = WorkflowsClient( - 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 response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = workflows.ListWorkflowsResponse( + next_page_token="next_page_token_value", + unreachable=["unreachable_value"], ) - # It is an error to provide a credentials file and a transport instance. - transport = transports.WorkflowsGrpcTransport( + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = workflows.ListWorkflowsResponse.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_workflows(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListWorkflowsPager) + assert response.next_page_token == "next_page_token_value" + assert response.unreachable == ["unreachable_value"] + + +def test_list_workflows_rest_required_fields( + request_type=workflows.ListWorkflowsRequest, +): + transport_class = transports.WorkflowsRestTransport + + 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_workflows._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_workflows._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "order_by", + "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 = WorkflowsClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - with pytest.raises(ValueError): - client = WorkflowsClient( - client_options={"credentials_file": "credentials.json"}, - transport=transport, + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = workflows.ListWorkflowsResponse() + # 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 = workflows.ListWorkflowsResponse.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_workflows(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_workflows_rest_unset_required_fields(): + transport = transports.WorkflowsRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_workflows._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "orderBy", + "pageSize", + "pageToken", + ) ) + & set(("parent",)) + ) - # It is an error to provide an api_key and a transport instance. - transport = transports.WorkflowsGrpcTransport( + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_workflows_rest_interceptors(null_interceptor): + transport = transports.WorkflowsRestTransport( credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.WorkflowsRestInterceptor(), ) - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = WorkflowsClient( - client_options=options, - transport=transport, + client = WorkflowsClient(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.WorkflowsRestInterceptor, "post_list_workflows" + ) as post, mock.patch.object( + transports.WorkflowsRestInterceptor, "pre_list_workflows" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = workflows.ListWorkflowsRequest.pb(workflows.ListWorkflowsRequest()) + 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 = workflows.ListWorkflowsResponse.to_json( + workflows.ListWorkflowsResponse() ) - # 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 = WorkflowsClient( - client_options=options, credentials=ga_credentials.AnonymousCredentials() + request = workflows.ListWorkflowsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = workflows.ListWorkflowsResponse() + + client.list_workflows( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], ) - # It is an error to provide scopes and a transport instance. - transport = transports.WorkflowsGrpcTransport( + pre.assert_called_once() + post.assert_called_once() + + +def test_list_workflows_rest_bad_request( + transport: str = "rest", request_type=workflows.ListWorkflowsRequest +): + client = WorkflowsClient( credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - with pytest.raises(ValueError): - client = WorkflowsClient( - client_options={"scopes": ["1", "2"]}, - transport=transport, - ) + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) -def test_transport_instance(): - # A client may be instantiated with a custom transport instance. - transport = transports.WorkflowsGrpcTransport( + # 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_workflows(request) + + +def test_list_workflows_rest_flattened(): + client = WorkflowsClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - client = WorkflowsClient(transport=transport) - assert client.transport is 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 = workflows.ListWorkflowsResponse() -def test_transport_get_channel(): - # A client may be instantiated with a custom transport instance. - transport = transports.WorkflowsGrpcTransport( + # 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 = workflows.ListWorkflowsResponse.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_workflows(**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/*}/workflows" % client.transport._host, + args[1], + ) + + +def test_list_workflows_rest_flattened_error(transport: str = "rest"): + client = WorkflowsClient( credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - channel = transport.grpc_channel - assert channel - transport = transports.WorkflowsGrpcAsyncIOTransport( + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_workflows( + workflows.ListWorkflowsRequest(), + parent="parent_value", + ) + + +def test_list_workflows_rest_pager(transport: str = "rest"): + client = WorkflowsClient( credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - channel = transport.grpc_channel - assert channel + # 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 = ( + workflows.ListWorkflowsResponse( + workflows=[ + workflows.Workflow(), + workflows.Workflow(), + workflows.Workflow(), + ], + next_page_token="abc", + ), + workflows.ListWorkflowsResponse( + workflows=[], + next_page_token="def", + ), + workflows.ListWorkflowsResponse( + workflows=[ + workflows.Workflow(), + ], + next_page_token="ghi", + ), + workflows.ListWorkflowsResponse( + workflows=[ + workflows.Workflow(), + workflows.Workflow(), + ], + ), + ) + # Two responses for two calls + response = response + response -@pytest.mark.parametrize( - "transport_class", - [ - transports.WorkflowsGrpcTransport, - transports.WorkflowsGrpcAsyncIOTransport, - ], -) -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() + # Wrap the values into proper Response objs + response = tuple(workflows.ListWorkflowsResponse.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_workflows(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, workflows.Workflow) for i in results) + + pages = list(client.list_workflows(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token @pytest.mark.parametrize( - "transport_name", + "request_type", [ - "grpc", + workflows.GetWorkflowRequest, + dict, ], ) -def test_transport_kind(transport_name): - transport = WorkflowsClient.get_transport_class(transport_name)( +def test_get_workflow_rest(request_type): + client = WorkflowsClient( 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/workflows/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 = workflows.Workflow( + name="name_value", + description="description_value", + state=workflows.Workflow.State.ACTIVE, + revision_id="revision_id_value", + service_account="service_account_value", + source_contents="source_contents_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = workflows.Workflow.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_workflow(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, workflows.Workflow) + assert response.name == "name_value" + assert response.description == "description_value" + assert response.state == workflows.Workflow.State.ACTIVE + assert response.revision_id == "revision_id_value" + assert response.service_account == "service_account_value" + + +def test_get_workflow_rest_required_fields(request_type=workflows.GetWorkflowRequest): + transport_class = transports.WorkflowsRestTransport + + 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_workflow._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_workflow._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 = WorkflowsClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - assert isinstance( - client.transport, - transports.WorkflowsGrpcTransport, + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = workflows.Workflow() + # 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 = workflows.Workflow.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_workflow(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_workflow_rest_unset_required_fields(): + transport = transports.WorkflowsRestTransport( + credentials=ga_credentials.AnonymousCredentials ) + unset_fields = transport.get_workflow._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) -def test_workflows_base_transport_error(): - # Passing both a credentials object and credentials_file should raise an error - with pytest.raises(core_exceptions.DuplicateCredentialArgs): - transport = transports.WorkflowsTransport( - credentials=ga_credentials.AnonymousCredentials(), - credentials_file="credentials.json", - ) +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_workflow_rest_interceptors(null_interceptor): + transport = transports.WorkflowsRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.WorkflowsRestInterceptor(), + ) + client = WorkflowsClient(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.WorkflowsRestInterceptor, "post_get_workflow" + ) as post, mock.patch.object( + transports.WorkflowsRestInterceptor, "pre_get_workflow" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = workflows.GetWorkflowRequest.pb(workflows.GetWorkflowRequest()) + 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 = workflows.Workflow.to_json(workflows.Workflow()) + + request = workflows.GetWorkflowRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = workflows.Workflow() -def test_workflows_base_transport(): - # Instantiate the base transport. - with mock.patch( - "google.cloud.workflows_v1.services.workflows.transports.WorkflowsTransport.__init__" - ) as Transport: - Transport.return_value = None - transport = transports.WorkflowsTransport( - credentials=ga_credentials.AnonymousCredentials(), + client.get_workflow( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], ) - # Every method on the transport should just blindly - # raise NotImplementedError. - methods = ( - "list_workflows", - "get_workflow", - "create_workflow", - "delete_workflow", - "update_workflow", - ) - for method in methods: - with pytest.raises(NotImplementedError): - getattr(transport, method)(request=object()) + pre.assert_called_once() + post.assert_called_once() - with pytest.raises(NotImplementedError): - transport.close() - # Additionally, the LRO client (a property) should +def test_get_workflow_rest_bad_request( + transport: str = "rest", request_type=workflows.GetWorkflowRequest +): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/workflows/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_workflow(request) + + +def test_get_workflow_rest_flattened(): + client = WorkflowsClient( + 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 = workflows.Workflow() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/workflows/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 = workflows.Workflow.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_workflow(**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/*/workflows/*}" % client.transport._host, + args[1], + ) + + +def test_get_workflow_rest_flattened_error(transport: str = "rest"): + client = WorkflowsClient( + 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_workflow( + workflows.GetWorkflowRequest(), + name="name_value", + ) + + +def test_get_workflow_rest_error(): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + workflows.CreateWorkflowRequest, + dict, + ], +) +def test_create_workflow_rest(request_type): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["workflow"] = { + "name": "name_value", + "description": "description_value", + "state": 1, + "revision_id": "revision_id_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "revision_create_time": {}, + "labels": {}, + "service_account": "service_account_value", + "source_contents": "source_contents_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 = 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_workflow(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_create_workflow_rest_required_fields( + request_type=workflows.CreateWorkflowRequest, +): + transport_class = transports.WorkflowsRestTransport + + request_init = {} + request_init["parent"] = "" + request_init["workflow_id"] = "" + 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 "workflowId" not in jsonified_request + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_workflow._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + assert "workflowId" in jsonified_request + assert jsonified_request["workflowId"] == request_init["workflow_id"] + + jsonified_request["parent"] = "parent_value" + jsonified_request["workflowId"] = "workflow_id_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_workflow._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("workflow_id",)) + 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 "workflowId" in jsonified_request + assert jsonified_request["workflowId"] == "workflow_id_value" + + client = WorkflowsClient( + 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_workflow(request) + + expected_params = [ + ( + "workflowId", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_workflow_rest_unset_required_fields(): + transport = transports.WorkflowsRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_workflow._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("workflowId",)) + & set( + ( + "parent", + "workflow", + "workflowId", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_workflow_rest_interceptors(null_interceptor): + transport = transports.WorkflowsRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.WorkflowsRestInterceptor(), + ) + client = WorkflowsClient(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.WorkflowsRestInterceptor, "post_create_workflow" + ) as post, mock.patch.object( + transports.WorkflowsRestInterceptor, "pre_create_workflow" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = workflows.CreateWorkflowRequest.pb( + workflows.CreateWorkflowRequest() + ) + 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 = workflows.CreateWorkflowRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.create_workflow( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_create_workflow_rest_bad_request( + transport: str = "rest", request_type=workflows.CreateWorkflowRequest +): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["workflow"] = { + "name": "name_value", + "description": "description_value", + "state": 1, + "revision_id": "revision_id_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "revision_create_time": {}, + "labels": {}, + "service_account": "service_account_value", + "source_contents": "source_contents_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.create_workflow(request) + + +def test_create_workflow_rest_flattened(): + client = WorkflowsClient( + 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", + workflow=workflows.Workflow(name="name_value"), + workflow_id="workflow_id_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_workflow(**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/*}/workflows" % client.transport._host, + args[1], + ) + + +def test_create_workflow_rest_flattened_error(transport: str = "rest"): + client = WorkflowsClient( + 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_workflow( + workflows.CreateWorkflowRequest(), + parent="parent_value", + workflow=workflows.Workflow(name="name_value"), + workflow_id="workflow_id_value", + ) + + +def test_create_workflow_rest_error(): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + workflows.DeleteWorkflowRequest, + dict, + ], +) +def test_delete_workflow_rest(request_type): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/workflows/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_workflow(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_delete_workflow_rest_required_fields( + request_type=workflows.DeleteWorkflowRequest, +): + transport_class = transports.WorkflowsRestTransport + + 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_workflow._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_workflow._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 = WorkflowsClient( + 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_workflow(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_workflow_rest_unset_required_fields(): + transport = transports.WorkflowsRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_workflow._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_delete_workflow_rest_interceptors(null_interceptor): + transport = transports.WorkflowsRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.WorkflowsRestInterceptor(), + ) + client = WorkflowsClient(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.WorkflowsRestInterceptor, "post_delete_workflow" + ) as post, mock.patch.object( + transports.WorkflowsRestInterceptor, "pre_delete_workflow" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = workflows.DeleteWorkflowRequest.pb( + workflows.DeleteWorkflowRequest() + ) + 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 = workflows.DeleteWorkflowRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.delete_workflow( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_delete_workflow_rest_bad_request( + transport: str = "rest", request_type=workflows.DeleteWorkflowRequest +): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/workflows/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_workflow(request) + + +def test_delete_workflow_rest_flattened(): + client = WorkflowsClient( + 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/workflows/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_workflow(**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/*/workflows/*}" % client.transport._host, + args[1], + ) + + +def test_delete_workflow_rest_flattened_error(transport: str = "rest"): + client = WorkflowsClient( + 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_workflow( + workflows.DeleteWorkflowRequest(), + name="name_value", + ) + + +def test_delete_workflow_rest_error(): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + workflows.UpdateWorkflowRequest, + dict, + ], +) +def test_update_workflow_rest(request_type): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "workflow": {"name": "projects/sample1/locations/sample2/workflows/sample3"} + } + request_init["workflow"] = { + "name": "projects/sample1/locations/sample2/workflows/sample3", + "description": "description_value", + "state": 1, + "revision_id": "revision_id_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "revision_create_time": {}, + "labels": {}, + "service_account": "service_account_value", + "source_contents": "source_contents_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 = 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.update_workflow(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_update_workflow_rest_required_fields( + request_type=workflows.UpdateWorkflowRequest, +): + transport_class = transports.WorkflowsRestTransport + + 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_workflow._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_workflow._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 = WorkflowsClient( + 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": "patch", + "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.update_workflow(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_update_workflow_rest_unset_required_fields(): + transport = transports.WorkflowsRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.update_workflow._get_unset_required_fields({}) + assert set(unset_fields) == (set(("updateMask",)) & set(("workflow",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_update_workflow_rest_interceptors(null_interceptor): + transport = transports.WorkflowsRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.WorkflowsRestInterceptor(), + ) + client = WorkflowsClient(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.WorkflowsRestInterceptor, "post_update_workflow" + ) as post, mock.patch.object( + transports.WorkflowsRestInterceptor, "pre_update_workflow" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = workflows.UpdateWorkflowRequest.pb( + workflows.UpdateWorkflowRequest() + ) + 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 = workflows.UpdateWorkflowRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.update_workflow( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_update_workflow_rest_bad_request( + transport: str = "rest", request_type=workflows.UpdateWorkflowRequest +): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "workflow": {"name": "projects/sample1/locations/sample2/workflows/sample3"} + } + request_init["workflow"] = { + "name": "projects/sample1/locations/sample2/workflows/sample3", + "description": "description_value", + "state": 1, + "revision_id": "revision_id_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "revision_create_time": {}, + "labels": {}, + "service_account": "service_account_value", + "source_contents": "source_contents_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_workflow(request) + + +def test_update_workflow_rest_flattened(): + client = WorkflowsClient( + 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 = { + "workflow": {"name": "projects/sample1/locations/sample2/workflows/sample3"} + } + + # get truthy value for each flattened field + mock_args = dict( + workflow=workflows.Workflow(name="name_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 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.update_workflow(**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/{workflow.name=projects/*/locations/*/workflows/*}" + % client.transport._host, + args[1], + ) + + +def test_update_workflow_rest_flattened_error(transport: str = "rest"): + client = WorkflowsClient( + 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_workflow( + workflows.UpdateWorkflowRequest(), + workflow=workflows.Workflow(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + +def test_update_workflow_rest_error(): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +def test_credentials_transport_error(): + # It is an error to provide credentials and a transport instance. + transport = transports.WorkflowsGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # It is an error to provide a credentials file and a transport instance. + transport = transports.WorkflowsGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = WorkflowsClient( + client_options={"credentials_file": "credentials.json"}, + transport=transport, + ) + + # It is an error to provide an api_key and a transport instance. + transport = transports.WorkflowsGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = WorkflowsClient( + 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 = WorkflowsClient( + client_options=options, credentials=ga_credentials.AnonymousCredentials() + ) + + # It is an error to provide scopes and a transport instance. + transport = transports.WorkflowsGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = WorkflowsClient( + client_options={"scopes": ["1", "2"]}, + transport=transport, + ) + + +def test_transport_instance(): + # A client may be instantiated with a custom transport instance. + transport = transports.WorkflowsGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + client = WorkflowsClient(transport=transport) + assert client.transport is transport + + +def test_transport_get_channel(): + # A client may be instantiated with a custom transport instance. + transport = transports.WorkflowsGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + transport = transports.WorkflowsGrpcAsyncIOTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.WorkflowsGrpcTransport, + transports.WorkflowsGrpcAsyncIOTransport, + transports.WorkflowsRestTransport, + ], +) +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 = WorkflowsClient.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 = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + assert isinstance( + client.transport, + transports.WorkflowsGrpcTransport, + ) + + +def test_workflows_base_transport_error(): + # Passing both a credentials object and credentials_file should raise an error + with pytest.raises(core_exceptions.DuplicateCredentialArgs): + transport = transports.WorkflowsTransport( + credentials=ga_credentials.AnonymousCredentials(), + credentials_file="credentials.json", + ) + + +def test_workflows_base_transport(): + # Instantiate the base transport. + with mock.patch( + "google.cloud.workflows_v1.services.workflows.transports.WorkflowsTransport.__init__" + ) as Transport: + Transport.return_value = None + transport = transports.WorkflowsTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Every method on the transport should just blindly + # raise NotImplementedError. + methods = ( + "list_workflows", + "get_workflow", + "create_workflow", + "delete_workflow", + "update_workflow", + ) + 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 @@ -2276,6 +3775,7 @@ def test_workflows_transport_auth_adc(transport_class): [ transports.WorkflowsGrpcTransport, transports.WorkflowsGrpcAsyncIOTransport, + transports.WorkflowsRestTransport, ], ) def test_workflows_transport_auth_gdch_credentials(transport_class): @@ -2370,11 +3870,40 @@ def test_workflows_grpc_transport_client_cert_source_for_mtls(transport_class): ) +def test_workflows_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.WorkflowsRestTransport( + 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_workflows_rest_lro_client(): + client = WorkflowsClient( + 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_workflows_host_no_port(transport_name): @@ -2385,7 +3914,11 @@ def test_workflows_host_no_port(transport_name): ), transport=transport_name, ) - assert client.transport._host == ("workflows.googleapis.com:443") + assert client.transport._host == ( + "workflows.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://workflows.googleapis.com" + ) @pytest.mark.parametrize( @@ -2393,6 +3926,7 @@ def test_workflows_host_no_port(transport_name): [ "grpc", "grpc_asyncio", + "rest", ], ) def test_workflows_host_with_port(transport_name): @@ -2403,7 +3937,45 @@ def test_workflows_host_with_port(transport_name): ), transport=transport_name, ) - assert client.transport._host == ("workflows.googleapis.com:8000") + assert client.transport._host == ( + "workflows.googleapis.com:8000" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://workflows.googleapis.com:8000" + ) + + +@pytest.mark.parametrize( + "transport_name", + [ + "rest", + ], +) +def test_workflows_client_transport_session_collision(transport_name): + creds1 = ga_credentials.AnonymousCredentials() + creds2 = ga_credentials.AnonymousCredentials() + client1 = WorkflowsClient( + credentials=creds1, + transport=transport_name, + ) + client2 = WorkflowsClient( + credentials=creds2, + transport=transport_name, + ) + session1 = client1.transport.list_workflows._session + session2 = client2.transport.list_workflows._session + assert session1 != session2 + session1 = client1.transport.get_workflow._session + session2 = client2.transport.get_workflow._session + assert session1 != session2 + session1 = client1.transport.create_workflow._session + session2 = client2.transport.create_workflow._session + assert session1 != session2 + session1 = client1.transport.delete_workflow._session + session2 = client2.transport.delete_workflow._session + assert session1 != session2 + session1 = client1.transport.update_workflow._session + session2 = client2.transport.update_workflow._session + assert session1 != session2 def test_workflows_grpc_transport_channel(): @@ -2726,6 +4298,7 @@ async def test_transport_close_async(): def test_transport_close(): transports = { + "rest": "_session", "grpc": "_grpc_channel", } @@ -2743,6 +4316,7 @@ def test_transport_close(): def test_client_ctx(): transports = [ + "rest", "grpc", ] for transport in transports: diff --git a/tests/unit/gapic/workflows_v1beta/test_workflows.py b/tests/unit/gapic/workflows_v1beta/test_workflows.py index 8c863d5..184ed82 100644 --- a/tests/unit/gapic/workflows_v1beta/test_workflows.py +++ b/tests/unit/gapic/workflows_v1beta/test_workflows.py @@ -22,6 +22,8 @@ except ImportError: # pragma: NO COVER import mock +from collections.abc import Iterable +import json import math from google.api_core import ( @@ -43,12 +45,15 @@ from google.oauth2 import service_account from google.protobuf import empty_pb2 # type: ignore from google.protobuf import field_mask_pb2 # type: ignore +from google.protobuf import json_format from google.protobuf import timestamp_pb2 # type: ignore import grpc from grpc.experimental import aio from proto.marshal.rules import wrappers from proto.marshal.rules.dates import DurationRule, TimestampRule import pytest +from requests import PreparedRequest, Request, Response +from requests.sessions import Session from google.cloud.workflows_v1beta.services.workflows import ( WorkflowsAsyncClient, @@ -103,6 +108,7 @@ def test__get_default_mtls_endpoint(): [ (WorkflowsClient, "grpc"), (WorkflowsAsyncClient, "grpc_asyncio"), + (WorkflowsClient, "rest"), ], ) def test_workflows_client_from_service_account_info(client_class, transport_name): @@ -116,7 +122,11 @@ def test_workflows_client_from_service_account_info(client_class, transport_name assert client.transport._credentials == creds assert isinstance(client, client_class) - assert client.transport._host == ("workflows.googleapis.com:443") + assert client.transport._host == ( + "workflows.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://workflows.googleapis.com" + ) @pytest.mark.parametrize( @@ -124,6 +134,7 @@ def test_workflows_client_from_service_account_info(client_class, transport_name [ (transports.WorkflowsGrpcTransport, "grpc"), (transports.WorkflowsGrpcAsyncIOTransport, "grpc_asyncio"), + (transports.WorkflowsRestTransport, "rest"), ], ) def test_workflows_client_service_account_always_use_jwt( @@ -149,6 +160,7 @@ def test_workflows_client_service_account_always_use_jwt( [ (WorkflowsClient, "grpc"), (WorkflowsAsyncClient, "grpc_asyncio"), + (WorkflowsClient, "rest"), ], ) def test_workflows_client_from_service_account_file(client_class, transport_name): @@ -169,13 +181,18 @@ def test_workflows_client_from_service_account_file(client_class, transport_name assert client.transport._credentials == creds assert isinstance(client, client_class) - assert client.transport._host == ("workflows.googleapis.com:443") + assert client.transport._host == ( + "workflows.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://workflows.googleapis.com" + ) def test_workflows_client_get_transport_class(): transport = WorkflowsClient.get_transport_class() available_transports = [ transports.WorkflowsGrpcTransport, + transports.WorkflowsRestTransport, ] assert transport in available_transports @@ -192,6 +209,7 @@ def test_workflows_client_get_transport_class(): transports.WorkflowsGrpcAsyncIOTransport, "grpc_asyncio", ), + (WorkflowsClient, transports.WorkflowsRestTransport, "rest"), ], ) @mock.patch.object( @@ -333,6 +351,8 @@ def test_workflows_client_client_options(client_class, transport_class, transpor "grpc_asyncio", "false", ), + (WorkflowsClient, transports.WorkflowsRestTransport, "rest", "true"), + (WorkflowsClient, transports.WorkflowsRestTransport, "rest", "false"), ], ) @mock.patch.object( @@ -526,6 +546,7 @@ def test_workflows_client_get_mtls_endpoint_and_cert_source(client_class): transports.WorkflowsGrpcAsyncIOTransport, "grpc_asyncio", ), + (WorkflowsClient, transports.WorkflowsRestTransport, "rest"), ], ) def test_workflows_client_client_options_scopes( @@ -561,6 +582,7 @@ def test_workflows_client_client_options_scopes( "grpc_asyncio", grpc_helpers_async, ), + (WorkflowsClient, transports.WorkflowsRestTransport, "rest", None), ], ) def test_workflows_client_client_options_credentials_file( @@ -2043,157 +2065,1638 @@ async def test_update_workflow_flattened_error_async(): ) -def test_credentials_transport_error(): - # It is an error to provide credentials and a transport instance. - transport = transports.WorkflowsGrpcTransport( +@pytest.mark.parametrize( + "request_type", + [ + workflows.ListWorkflowsRequest, + dict, + ], +) +def test_list_workflows_rest(request_type): + client = WorkflowsClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - with pytest.raises(ValueError): - client = WorkflowsClient( - 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 response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = workflows.ListWorkflowsResponse( + next_page_token="next_page_token_value", + unreachable=["unreachable_value"], ) - # It is an error to provide a credentials file and a transport instance. - transport = transports.WorkflowsGrpcTransport( + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = workflows.ListWorkflowsResponse.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_workflows(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListWorkflowsPager) + assert response.next_page_token == "next_page_token_value" + assert response.unreachable == ["unreachable_value"] + + +def test_list_workflows_rest_required_fields( + request_type=workflows.ListWorkflowsRequest, +): + transport_class = transports.WorkflowsRestTransport + + 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_workflows._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_workflows._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "order_by", + "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 = WorkflowsClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - with pytest.raises(ValueError): - client = WorkflowsClient( - client_options={"credentials_file": "credentials.json"}, - transport=transport, + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = workflows.ListWorkflowsResponse() + # 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 = workflows.ListWorkflowsResponse.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_workflows(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_workflows_rest_unset_required_fields(): + transport = transports.WorkflowsRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_workflows._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "orderBy", + "pageSize", + "pageToken", + ) ) + & set(("parent",)) + ) - # It is an error to provide an api_key and a transport instance. - transport = transports.WorkflowsGrpcTransport( + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_workflows_rest_interceptors(null_interceptor): + transport = transports.WorkflowsRestTransport( credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.WorkflowsRestInterceptor(), ) - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = WorkflowsClient( - client_options=options, - transport=transport, + client = WorkflowsClient(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.WorkflowsRestInterceptor, "post_list_workflows" + ) as post, mock.patch.object( + transports.WorkflowsRestInterceptor, "pre_list_workflows" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = workflows.ListWorkflowsRequest.pb(workflows.ListWorkflowsRequest()) + 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 = workflows.ListWorkflowsResponse.to_json( + workflows.ListWorkflowsResponse() ) - # 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 = WorkflowsClient( - client_options=options, credentials=ga_credentials.AnonymousCredentials() + request = workflows.ListWorkflowsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = workflows.ListWorkflowsResponse() + + client.list_workflows( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], ) - # It is an error to provide scopes and a transport instance. - transport = transports.WorkflowsGrpcTransport( + pre.assert_called_once() + post.assert_called_once() + + +def test_list_workflows_rest_bad_request( + transport: str = "rest", request_type=workflows.ListWorkflowsRequest +): + client = WorkflowsClient( credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - with pytest.raises(ValueError): - client = WorkflowsClient( - client_options={"scopes": ["1", "2"]}, - transport=transport, - ) + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) -def test_transport_instance(): - # A client may be instantiated with a custom transport instance. - transport = transports.WorkflowsGrpcTransport( + # 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_workflows(request) + + +def test_list_workflows_rest_flattened(): + client = WorkflowsClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - client = WorkflowsClient(transport=transport) - assert client.transport is 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 = workflows.ListWorkflowsResponse() -def test_transport_get_channel(): - # A client may be instantiated with a custom transport instance. - transport = transports.WorkflowsGrpcTransport( + # 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 = workflows.ListWorkflowsResponse.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_workflows(**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/v1beta/{parent=projects/*/locations/*}/workflows" + % client.transport._host, + args[1], + ) + + +def test_list_workflows_rest_flattened_error(transport: str = "rest"): + client = WorkflowsClient( credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - channel = transport.grpc_channel - assert channel - transport = transports.WorkflowsGrpcAsyncIOTransport( + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_workflows( + workflows.ListWorkflowsRequest(), + parent="parent_value", + ) + + +def test_list_workflows_rest_pager(transport: str = "rest"): + client = WorkflowsClient( credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - channel = transport.grpc_channel - assert channel + # 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 = ( + workflows.ListWorkflowsResponse( + workflows=[ + workflows.Workflow(), + workflows.Workflow(), + workflows.Workflow(), + ], + next_page_token="abc", + ), + workflows.ListWorkflowsResponse( + workflows=[], + next_page_token="def", + ), + workflows.ListWorkflowsResponse( + workflows=[ + workflows.Workflow(), + ], + next_page_token="ghi", + ), + workflows.ListWorkflowsResponse( + workflows=[ + workflows.Workflow(), + workflows.Workflow(), + ], + ), + ) + # Two responses for two calls + response = response + response -@pytest.mark.parametrize( - "transport_class", - [ - transports.WorkflowsGrpcTransport, - transports.WorkflowsGrpcAsyncIOTransport, - ], -) -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() + # Wrap the values into proper Response objs + response = tuple(workflows.ListWorkflowsResponse.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_workflows(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, workflows.Workflow) for i in results) + + pages = list(client.list_workflows(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token @pytest.mark.parametrize( - "transport_name", + "request_type", [ - "grpc", + workflows.GetWorkflowRequest, + dict, ], ) -def test_transport_kind(transport_name): - transport = WorkflowsClient.get_transport_class(transport_name)( +def test_get_workflow_rest(request_type): + client = WorkflowsClient( 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/workflows/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 = workflows.Workflow( + name="name_value", + description="description_value", + state=workflows.Workflow.State.ACTIVE, + revision_id="revision_id_value", + service_account="service_account_value", + source_contents="source_contents_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = workflows.Workflow.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_workflow(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, workflows.Workflow) + assert response.name == "name_value" + assert response.description == "description_value" + assert response.state == workflows.Workflow.State.ACTIVE + assert response.revision_id == "revision_id_value" + assert response.service_account == "service_account_value" + + +def test_get_workflow_rest_required_fields(request_type=workflows.GetWorkflowRequest): + transport_class = transports.WorkflowsRestTransport + + 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_workflow._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_workflow._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 = WorkflowsClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - assert isinstance( - client.transport, - transports.WorkflowsGrpcTransport, + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = workflows.Workflow() + # 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 = workflows.Workflow.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_workflow(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_workflow_rest_unset_required_fields(): + transport = transports.WorkflowsRestTransport( + credentials=ga_credentials.AnonymousCredentials ) + unset_fields = transport.get_workflow._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) -def test_workflows_base_transport_error(): - # Passing both a credentials object and credentials_file should raise an error - with pytest.raises(core_exceptions.DuplicateCredentialArgs): - transport = transports.WorkflowsTransport( - credentials=ga_credentials.AnonymousCredentials(), - credentials_file="credentials.json", - ) +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_workflow_rest_interceptors(null_interceptor): + transport = transports.WorkflowsRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.WorkflowsRestInterceptor(), + ) + client = WorkflowsClient(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.WorkflowsRestInterceptor, "post_get_workflow" + ) as post, mock.patch.object( + transports.WorkflowsRestInterceptor, "pre_get_workflow" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = workflows.GetWorkflowRequest.pb(workflows.GetWorkflowRequest()) + 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 = workflows.Workflow.to_json(workflows.Workflow()) + + request = workflows.GetWorkflowRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = workflows.Workflow() -def test_workflows_base_transport(): - # Instantiate the base transport. - with mock.patch( - "google.cloud.workflows_v1beta.services.workflows.transports.WorkflowsTransport.__init__" - ) as Transport: - Transport.return_value = None - transport = transports.WorkflowsTransport( - credentials=ga_credentials.AnonymousCredentials(), + client.get_workflow( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], ) - # Every method on the transport should just blindly - # raise NotImplementedError. - methods = ( - "list_workflows", - "get_workflow", - "create_workflow", - "delete_workflow", - "update_workflow", - ) - for method in methods: - with pytest.raises(NotImplementedError): - getattr(transport, method)(request=object()) + pre.assert_called_once() + post.assert_called_once() - with pytest.raises(NotImplementedError): - transport.close() - # Additionally, the LRO client (a property) should +def test_get_workflow_rest_bad_request( + transport: str = "rest", request_type=workflows.GetWorkflowRequest +): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/workflows/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_workflow(request) + + +def test_get_workflow_rest_flattened(): + client = WorkflowsClient( + 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 = workflows.Workflow() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/workflows/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 = workflows.Workflow.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_workflow(**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/v1beta/{name=projects/*/locations/*/workflows/*}" + % client.transport._host, + args[1], + ) + + +def test_get_workflow_rest_flattened_error(transport: str = "rest"): + client = WorkflowsClient( + 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_workflow( + workflows.GetWorkflowRequest(), + name="name_value", + ) + + +def test_get_workflow_rest_error(): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + workflows.CreateWorkflowRequest, + dict, + ], +) +def test_create_workflow_rest(request_type): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["workflow"] = { + "name": "name_value", + "description": "description_value", + "state": 1, + "revision_id": "revision_id_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "revision_create_time": {}, + "labels": {}, + "service_account": "service_account_value", + "source_contents": "source_contents_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 = 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_workflow(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_create_workflow_rest_required_fields( + request_type=workflows.CreateWorkflowRequest, +): + transport_class = transports.WorkflowsRestTransport + + request_init = {} + request_init["parent"] = "" + request_init["workflow_id"] = "" + 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 "workflowId" not in jsonified_request + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_workflow._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + assert "workflowId" in jsonified_request + assert jsonified_request["workflowId"] == request_init["workflow_id"] + + jsonified_request["parent"] = "parent_value" + jsonified_request["workflowId"] = "workflow_id_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_workflow._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("workflow_id",)) + 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 "workflowId" in jsonified_request + assert jsonified_request["workflowId"] == "workflow_id_value" + + client = WorkflowsClient( + 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_workflow(request) + + expected_params = [ + ( + "workflowId", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_workflow_rest_unset_required_fields(): + transport = transports.WorkflowsRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_workflow._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(("workflowId",)) + & set( + ( + "parent", + "workflow", + "workflowId", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_workflow_rest_interceptors(null_interceptor): + transport = transports.WorkflowsRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.WorkflowsRestInterceptor(), + ) + client = WorkflowsClient(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.WorkflowsRestInterceptor, "post_create_workflow" + ) as post, mock.patch.object( + transports.WorkflowsRestInterceptor, "pre_create_workflow" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = workflows.CreateWorkflowRequest.pb( + workflows.CreateWorkflowRequest() + ) + 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 = workflows.CreateWorkflowRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.create_workflow( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_create_workflow_rest_bad_request( + transport: str = "rest", request_type=workflows.CreateWorkflowRequest +): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["workflow"] = { + "name": "name_value", + "description": "description_value", + "state": 1, + "revision_id": "revision_id_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "revision_create_time": {}, + "labels": {}, + "service_account": "service_account_value", + "source_contents": "source_contents_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.create_workflow(request) + + +def test_create_workflow_rest_flattened(): + client = WorkflowsClient( + 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", + workflow=workflows.Workflow(name="name_value"), + workflow_id="workflow_id_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_workflow(**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/v1beta/{parent=projects/*/locations/*}/workflows" + % client.transport._host, + args[1], + ) + + +def test_create_workflow_rest_flattened_error(transport: str = "rest"): + client = WorkflowsClient( + 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_workflow( + workflows.CreateWorkflowRequest(), + parent="parent_value", + workflow=workflows.Workflow(name="name_value"), + workflow_id="workflow_id_value", + ) + + +def test_create_workflow_rest_error(): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + workflows.DeleteWorkflowRequest, + dict, + ], +) +def test_delete_workflow_rest(request_type): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/workflows/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_workflow(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_delete_workflow_rest_required_fields( + request_type=workflows.DeleteWorkflowRequest, +): + transport_class = transports.WorkflowsRestTransport + + 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_workflow._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_workflow._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 = WorkflowsClient( + 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_workflow(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_workflow_rest_unset_required_fields(): + transport = transports.WorkflowsRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_workflow._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_delete_workflow_rest_interceptors(null_interceptor): + transport = transports.WorkflowsRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.WorkflowsRestInterceptor(), + ) + client = WorkflowsClient(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.WorkflowsRestInterceptor, "post_delete_workflow" + ) as post, mock.patch.object( + transports.WorkflowsRestInterceptor, "pre_delete_workflow" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = workflows.DeleteWorkflowRequest.pb( + workflows.DeleteWorkflowRequest() + ) + 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 = workflows.DeleteWorkflowRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.delete_workflow( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_delete_workflow_rest_bad_request( + transport: str = "rest", request_type=workflows.DeleteWorkflowRequest +): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/workflows/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_workflow(request) + + +def test_delete_workflow_rest_flattened(): + client = WorkflowsClient( + 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/workflows/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_workflow(**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/v1beta/{name=projects/*/locations/*/workflows/*}" + % client.transport._host, + args[1], + ) + + +def test_delete_workflow_rest_flattened_error(transport: str = "rest"): + client = WorkflowsClient( + 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_workflow( + workflows.DeleteWorkflowRequest(), + name="name_value", + ) + + +def test_delete_workflow_rest_error(): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + workflows.UpdateWorkflowRequest, + dict, + ], +) +def test_update_workflow_rest(request_type): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "workflow": {"name": "projects/sample1/locations/sample2/workflows/sample3"} + } + request_init["workflow"] = { + "name": "projects/sample1/locations/sample2/workflows/sample3", + "description": "description_value", + "state": 1, + "revision_id": "revision_id_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "revision_create_time": {}, + "labels": {}, + "service_account": "service_account_value", + "source_contents": "source_contents_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 = 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.update_workflow(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_update_workflow_rest_required_fields( + request_type=workflows.UpdateWorkflowRequest, +): + transport_class = transports.WorkflowsRestTransport + + 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_workflow._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_workflow._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 = WorkflowsClient( + 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": "patch", + "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.update_workflow(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_update_workflow_rest_unset_required_fields(): + transport = transports.WorkflowsRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.update_workflow._get_unset_required_fields({}) + assert set(unset_fields) == (set(("updateMask",)) & set(("workflow",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_update_workflow_rest_interceptors(null_interceptor): + transport = transports.WorkflowsRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None if null_interceptor else transports.WorkflowsRestInterceptor(), + ) + client = WorkflowsClient(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.WorkflowsRestInterceptor, "post_update_workflow" + ) as post, mock.patch.object( + transports.WorkflowsRestInterceptor, "pre_update_workflow" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = workflows.UpdateWorkflowRequest.pb( + workflows.UpdateWorkflowRequest() + ) + 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 = workflows.UpdateWorkflowRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.update_workflow( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_update_workflow_rest_bad_request( + transport: str = "rest", request_type=workflows.UpdateWorkflowRequest +): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "workflow": {"name": "projects/sample1/locations/sample2/workflows/sample3"} + } + request_init["workflow"] = { + "name": "projects/sample1/locations/sample2/workflows/sample3", + "description": "description_value", + "state": 1, + "revision_id": "revision_id_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "revision_create_time": {}, + "labels": {}, + "service_account": "service_account_value", + "source_contents": "source_contents_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_workflow(request) + + +def test_update_workflow_rest_flattened(): + client = WorkflowsClient( + 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 = { + "workflow": {"name": "projects/sample1/locations/sample2/workflows/sample3"} + } + + # get truthy value for each flattened field + mock_args = dict( + workflow=workflows.Workflow(name="name_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 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.update_workflow(**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/v1beta/{workflow.name=projects/*/locations/*/workflows/*}" + % client.transport._host, + args[1], + ) + + +def test_update_workflow_rest_flattened_error(transport: str = "rest"): + client = WorkflowsClient( + 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_workflow( + workflows.UpdateWorkflowRequest(), + workflow=workflows.Workflow(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + +def test_update_workflow_rest_error(): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +def test_credentials_transport_error(): + # It is an error to provide credentials and a transport instance. + transport = transports.WorkflowsGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # It is an error to provide a credentials file and a transport instance. + transport = transports.WorkflowsGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = WorkflowsClient( + client_options={"credentials_file": "credentials.json"}, + transport=transport, + ) + + # It is an error to provide an api_key and a transport instance. + transport = transports.WorkflowsGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = WorkflowsClient( + 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 = WorkflowsClient( + client_options=options, credentials=ga_credentials.AnonymousCredentials() + ) + + # It is an error to provide scopes and a transport instance. + transport = transports.WorkflowsGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = WorkflowsClient( + client_options={"scopes": ["1", "2"]}, + transport=transport, + ) + + +def test_transport_instance(): + # A client may be instantiated with a custom transport instance. + transport = transports.WorkflowsGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + client = WorkflowsClient(transport=transport) + assert client.transport is transport + + +def test_transport_get_channel(): + # A client may be instantiated with a custom transport instance. + transport = transports.WorkflowsGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + transport = transports.WorkflowsGrpcAsyncIOTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.WorkflowsGrpcTransport, + transports.WorkflowsGrpcAsyncIOTransport, + transports.WorkflowsRestTransport, + ], +) +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 = WorkflowsClient.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 = WorkflowsClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + assert isinstance( + client.transport, + transports.WorkflowsGrpcTransport, + ) + + +def test_workflows_base_transport_error(): + # Passing both a credentials object and credentials_file should raise an error + with pytest.raises(core_exceptions.DuplicateCredentialArgs): + transport = transports.WorkflowsTransport( + credentials=ga_credentials.AnonymousCredentials(), + credentials_file="credentials.json", + ) + + +def test_workflows_base_transport(): + # Instantiate the base transport. + with mock.patch( + "google.cloud.workflows_v1beta.services.workflows.transports.WorkflowsTransport.__init__" + ) as Transport: + Transport.return_value = None + transport = transports.WorkflowsTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Every method on the transport should just blindly + # raise NotImplementedError. + methods = ( + "list_workflows", + "get_workflow", + "create_workflow", + "delete_workflow", + "update_workflow", + ) + 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 @@ -2276,6 +3779,7 @@ def test_workflows_transport_auth_adc(transport_class): [ transports.WorkflowsGrpcTransport, transports.WorkflowsGrpcAsyncIOTransport, + transports.WorkflowsRestTransport, ], ) def test_workflows_transport_auth_gdch_credentials(transport_class): @@ -2370,11 +3874,40 @@ def test_workflows_grpc_transport_client_cert_source_for_mtls(transport_class): ) +def test_workflows_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.WorkflowsRestTransport( + 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_workflows_rest_lro_client(): + client = WorkflowsClient( + 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_workflows_host_no_port(transport_name): @@ -2385,7 +3918,11 @@ def test_workflows_host_no_port(transport_name): ), transport=transport_name, ) - assert client.transport._host == ("workflows.googleapis.com:443") + assert client.transport._host == ( + "workflows.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://workflows.googleapis.com" + ) @pytest.mark.parametrize( @@ -2393,6 +3930,7 @@ def test_workflows_host_no_port(transport_name): [ "grpc", "grpc_asyncio", + "rest", ], ) def test_workflows_host_with_port(transport_name): @@ -2403,7 +3941,45 @@ def test_workflows_host_with_port(transport_name): ), transport=transport_name, ) - assert client.transport._host == ("workflows.googleapis.com:8000") + assert client.transport._host == ( + "workflows.googleapis.com:8000" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://workflows.googleapis.com:8000" + ) + + +@pytest.mark.parametrize( + "transport_name", + [ + "rest", + ], +) +def test_workflows_client_transport_session_collision(transport_name): + creds1 = ga_credentials.AnonymousCredentials() + creds2 = ga_credentials.AnonymousCredentials() + client1 = WorkflowsClient( + credentials=creds1, + transport=transport_name, + ) + client2 = WorkflowsClient( + credentials=creds2, + transport=transport_name, + ) + session1 = client1.transport.list_workflows._session + session2 = client2.transport.list_workflows._session + assert session1 != session2 + session1 = client1.transport.get_workflow._session + session2 = client2.transport.get_workflow._session + assert session1 != session2 + session1 = client1.transport.create_workflow._session + session2 = client2.transport.create_workflow._session + assert session1 != session2 + session1 = client1.transport.delete_workflow._session + session2 = client2.transport.delete_workflow._session + assert session1 != session2 + session1 = client1.transport.update_workflow._session + session2 = client2.transport.update_workflow._session + assert session1 != session2 def test_workflows_grpc_transport_channel(): @@ -2726,6 +4302,7 @@ async def test_transport_close_async(): def test_transport_close(): transports = { + "rest": "_session", "grpc": "_grpc_channel", } @@ -2743,6 +4320,7 @@ def test_transport_close(): def test_client_ctx(): transports = [ + "rest", "grpc", ] for transport in transports: