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/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..1e23804 --- /dev/null +++ b/google/cloud/workflows_v1/services/workflows/transports/rest.py @@ -0,0 +1,879 @@ +# -*- 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]]] = {} + + 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/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/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..88ad15d --- /dev/null +++ b/google/cloud/workflows_v1beta/services/workflows/transports/rest.py @@ -0,0 +1,879 @@ +# -*- 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]]] = {} + + 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/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: