From 5a18fa7606272b06c479129ef8dce918326556a2 Mon Sep 17 00:00:00 2001 From: "gcf-owl-bot[bot]" <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Date: Thu, 16 Feb 2023 07:56:24 -0800 Subject: [PATCH] feat: enable "rest" transport in Python for services supporting numeric enums (#439) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: enable "rest" transport in Python for services supporting numeric enums PiperOrigin-RevId: 508143576 Source-Link: https://github.com/googleapis/googleapis/commit/7a702a989db3b413f39ff8994ca53fb38b6928c2 Source-Link: https://github.com/googleapis/googleapis-gen/commit/6ad1279c0e7aa787ac6b66c9fd4a210692edffcd Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiNmFkMTI3OWMwZTdhYTc4N2FjNmI2NmM5ZmQ0YTIxMDY5MmVkZmZjZCJ9 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * add rest transport to system test --------- Co-authored-by: Owl Bot Co-authored-by: Anthonios Partheniou --- .../gapic_metadata.json | 80 + .../services/data_transfer_service/client.py | 2 + .../transports/__init__.py | 4 + .../data_transfer_service/transports/rest.py | 2388 +++++++++ .../tests/system.py | 5 +- .../test_data_transfer_service.py | 4761 ++++++++++++++++- 6 files changed, 7136 insertions(+), 104 deletions(-) create mode 100644 packages/google-cloud-bigquery-datatransfer/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/transports/rest.py diff --git a/packages/google-cloud-bigquery-datatransfer/google/cloud/bigquery_datatransfer_v1/gapic_metadata.json b/packages/google-cloud-bigquery-datatransfer/google/cloud/bigquery_datatransfer_v1/gapic_metadata.json index 3b914fe7c9fb..c349f4cf3adf 100644 --- a/packages/google-cloud-bigquery-datatransfer/google/cloud/bigquery_datatransfer_v1/gapic_metadata.json +++ b/packages/google-cloud-bigquery-datatransfer/google/cloud/bigquery_datatransfer_v1/gapic_metadata.json @@ -166,6 +166,86 @@ ] } } + }, + "rest": { + "libraryClient": "DataTransferServiceClient", + "rpcs": { + "CheckValidCreds": { + "methods": [ + "check_valid_creds" + ] + }, + "CreateTransferConfig": { + "methods": [ + "create_transfer_config" + ] + }, + "DeleteTransferConfig": { + "methods": [ + "delete_transfer_config" + ] + }, + "DeleteTransferRun": { + "methods": [ + "delete_transfer_run" + ] + }, + "EnrollDataSources": { + "methods": [ + "enroll_data_sources" + ] + }, + "GetDataSource": { + "methods": [ + "get_data_source" + ] + }, + "GetTransferConfig": { + "methods": [ + "get_transfer_config" + ] + }, + "GetTransferRun": { + "methods": [ + "get_transfer_run" + ] + }, + "ListDataSources": { + "methods": [ + "list_data_sources" + ] + }, + "ListTransferConfigs": { + "methods": [ + "list_transfer_configs" + ] + }, + "ListTransferLogs": { + "methods": [ + "list_transfer_logs" + ] + }, + "ListTransferRuns": { + "methods": [ + "list_transfer_runs" + ] + }, + "ScheduleTransferRuns": { + "methods": [ + "schedule_transfer_runs" + ] + }, + "StartManualTransferRuns": { + "methods": [ + "start_manual_transfer_runs" + ] + }, + "UpdateTransferConfig": { + "methods": [ + "update_transfer_config" + ] + } + } } } } diff --git a/packages/google-cloud-bigquery-datatransfer/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/client.py b/packages/google-cloud-bigquery-datatransfer/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/client.py index fb865c8fc3ae..0495d0401b73 100644 --- a/packages/google-cloud-bigquery-datatransfer/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/client.py +++ b/packages/google-cloud-bigquery-datatransfer/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/client.py @@ -60,6 +60,7 @@ from .transports.base import DEFAULT_CLIENT_INFO, DataTransferServiceTransport from .transports.grpc import DataTransferServiceGrpcTransport from .transports.grpc_asyncio import DataTransferServiceGrpcAsyncIOTransport +from .transports.rest import DataTransferServiceRestTransport class DataTransferServiceClientMeta(type): @@ -75,6 +76,7 @@ class DataTransferServiceClientMeta(type): ) # type: Dict[str, Type[DataTransferServiceTransport]] _transport_registry["grpc"] = DataTransferServiceGrpcTransport _transport_registry["grpc_asyncio"] = DataTransferServiceGrpcAsyncIOTransport + _transport_registry["rest"] = DataTransferServiceRestTransport def get_transport_class( cls, diff --git a/packages/google-cloud-bigquery-datatransfer/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/transports/__init__.py b/packages/google-cloud-bigquery-datatransfer/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/transports/__init__.py index 01d735cb7565..ea74188810ed 100644 --- a/packages/google-cloud-bigquery-datatransfer/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/transports/__init__.py +++ b/packages/google-cloud-bigquery-datatransfer/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/transports/__init__.py @@ -19,6 +19,7 @@ from .base import DataTransferServiceTransport from .grpc import DataTransferServiceGrpcTransport from .grpc_asyncio import DataTransferServiceGrpcAsyncIOTransport +from .rest import DataTransferServiceRestInterceptor, DataTransferServiceRestTransport # Compile a registry of transports. _transport_registry = ( @@ -26,9 +27,12 @@ ) # type: Dict[str, Type[DataTransferServiceTransport]] _transport_registry["grpc"] = DataTransferServiceGrpcTransport _transport_registry["grpc_asyncio"] = DataTransferServiceGrpcAsyncIOTransport +_transport_registry["rest"] = DataTransferServiceRestTransport __all__ = ( "DataTransferServiceTransport", "DataTransferServiceGrpcTransport", "DataTransferServiceGrpcAsyncIOTransport", + "DataTransferServiceRestTransport", + "DataTransferServiceRestInterceptor", ) diff --git a/packages/google-cloud-bigquery-datatransfer/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/transports/rest.py b/packages/google-cloud-bigquery-datatransfer/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/transports/rest.py new file mode 100644 index 000000000000..65dd635638d3 --- /dev/null +++ b/packages/google-cloud-bigquery-datatransfer/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/transports/rest.py @@ -0,0 +1,2388 @@ +# -*- 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, 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.cloud.location import locations_pb2 # 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.protobuf import empty_pb2 # type: ignore + +from google.cloud.bigquery_datatransfer_v1.types import datatransfer, transfer + +from .base import DEFAULT_CLIENT_INFO as BASE_DEFAULT_CLIENT_INFO +from .base import DataTransferServiceTransport + +DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=BASE_DEFAULT_CLIENT_INFO.gapic_version, + grpc_version=None, + rest_version=requests_version, +) + + +class DataTransferServiceRestInterceptor: + """Interceptor for DataTransferService. + + 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 DataTransferServiceRestTransport. + + .. code-block:: python + class MyCustomDataTransferServiceInterceptor(DataTransferServiceRestInterceptor): + def pre_check_valid_creds(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_check_valid_creds(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_create_transfer_config(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_create_transfer_config(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_delete_transfer_config(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def pre_delete_transfer_run(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def pre_enroll_data_sources(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def pre_get_data_source(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_data_source(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_get_transfer_config(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_transfer_config(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_get_transfer_run(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_transfer_run(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_list_data_sources(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_data_sources(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_list_transfer_configs(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_transfer_configs(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_list_transfer_logs(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_transfer_logs(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_list_transfer_runs(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_transfer_runs(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_schedule_transfer_runs(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_schedule_transfer_runs(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_start_manual_transfer_runs(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_start_manual_transfer_runs(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_update_transfer_config(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_update_transfer_config(self, response): + logging.log(f"Received response: {response}") + return response + + transport = DataTransferServiceRestTransport(interceptor=MyCustomDataTransferServiceInterceptor()) + client = DataTransferServiceClient(transport=transport) + + + """ + + def pre_check_valid_creds( + self, + request: datatransfer.CheckValidCredsRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[datatransfer.CheckValidCredsRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for check_valid_creds + + Override in a subclass to manipulate the request or metadata + before they are sent to the DataTransferService server. + """ + return request, metadata + + def post_check_valid_creds( + self, response: datatransfer.CheckValidCredsResponse + ) -> datatransfer.CheckValidCredsResponse: + """Post-rpc interceptor for check_valid_creds + + Override in a subclass to manipulate the response + after it is returned by the DataTransferService server but before + it is returned to user code. + """ + return response + + def pre_create_transfer_config( + self, + request: datatransfer.CreateTransferConfigRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[datatransfer.CreateTransferConfigRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for create_transfer_config + + Override in a subclass to manipulate the request or metadata + before they are sent to the DataTransferService server. + """ + return request, metadata + + def post_create_transfer_config( + self, response: transfer.TransferConfig + ) -> transfer.TransferConfig: + """Post-rpc interceptor for create_transfer_config + + Override in a subclass to manipulate the response + after it is returned by the DataTransferService server but before + it is returned to user code. + """ + return response + + def pre_delete_transfer_config( + self, + request: datatransfer.DeleteTransferConfigRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[datatransfer.DeleteTransferConfigRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for delete_transfer_config + + Override in a subclass to manipulate the request or metadata + before they are sent to the DataTransferService server. + """ + return request, metadata + + def pre_delete_transfer_run( + self, + request: datatransfer.DeleteTransferRunRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[datatransfer.DeleteTransferRunRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for delete_transfer_run + + Override in a subclass to manipulate the request or metadata + before they are sent to the DataTransferService server. + """ + return request, metadata + + def pre_enroll_data_sources( + self, + request: datatransfer.EnrollDataSourcesRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[datatransfer.EnrollDataSourcesRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for enroll_data_sources + + Override in a subclass to manipulate the request or metadata + before they are sent to the DataTransferService server. + """ + return request, metadata + + def pre_get_data_source( + self, + request: datatransfer.GetDataSourceRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[datatransfer.GetDataSourceRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_data_source + + Override in a subclass to manipulate the request or metadata + before they are sent to the DataTransferService server. + """ + return request, metadata + + def post_get_data_source( + self, response: datatransfer.DataSource + ) -> datatransfer.DataSource: + """Post-rpc interceptor for get_data_source + + Override in a subclass to manipulate the response + after it is returned by the DataTransferService server but before + it is returned to user code. + """ + return response + + def pre_get_transfer_config( + self, + request: datatransfer.GetTransferConfigRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[datatransfer.GetTransferConfigRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_transfer_config + + Override in a subclass to manipulate the request or metadata + before they are sent to the DataTransferService server. + """ + return request, metadata + + def post_get_transfer_config( + self, response: transfer.TransferConfig + ) -> transfer.TransferConfig: + """Post-rpc interceptor for get_transfer_config + + Override in a subclass to manipulate the response + after it is returned by the DataTransferService server but before + it is returned to user code. + """ + return response + + def pre_get_transfer_run( + self, + request: datatransfer.GetTransferRunRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[datatransfer.GetTransferRunRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_transfer_run + + Override in a subclass to manipulate the request or metadata + before they are sent to the DataTransferService server. + """ + return request, metadata + + def post_get_transfer_run( + self, response: transfer.TransferRun + ) -> transfer.TransferRun: + """Post-rpc interceptor for get_transfer_run + + Override in a subclass to manipulate the response + after it is returned by the DataTransferService server but before + it is returned to user code. + """ + return response + + def pre_list_data_sources( + self, + request: datatransfer.ListDataSourcesRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[datatransfer.ListDataSourcesRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for list_data_sources + + Override in a subclass to manipulate the request or metadata + before they are sent to the DataTransferService server. + """ + return request, metadata + + def post_list_data_sources( + self, response: datatransfer.ListDataSourcesResponse + ) -> datatransfer.ListDataSourcesResponse: + """Post-rpc interceptor for list_data_sources + + Override in a subclass to manipulate the response + after it is returned by the DataTransferService server but before + it is returned to user code. + """ + return response + + def pre_list_transfer_configs( + self, + request: datatransfer.ListTransferConfigsRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[datatransfer.ListTransferConfigsRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for list_transfer_configs + + Override in a subclass to manipulate the request or metadata + before they are sent to the DataTransferService server. + """ + return request, metadata + + def post_list_transfer_configs( + self, response: datatransfer.ListTransferConfigsResponse + ) -> datatransfer.ListTransferConfigsResponse: + """Post-rpc interceptor for list_transfer_configs + + Override in a subclass to manipulate the response + after it is returned by the DataTransferService server but before + it is returned to user code. + """ + return response + + def pre_list_transfer_logs( + self, + request: datatransfer.ListTransferLogsRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[datatransfer.ListTransferLogsRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for list_transfer_logs + + Override in a subclass to manipulate the request or metadata + before they are sent to the DataTransferService server. + """ + return request, metadata + + def post_list_transfer_logs( + self, response: datatransfer.ListTransferLogsResponse + ) -> datatransfer.ListTransferLogsResponse: + """Post-rpc interceptor for list_transfer_logs + + Override in a subclass to manipulate the response + after it is returned by the DataTransferService server but before + it is returned to user code. + """ + return response + + def pre_list_transfer_runs( + self, + request: datatransfer.ListTransferRunsRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[datatransfer.ListTransferRunsRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for list_transfer_runs + + Override in a subclass to manipulate the request or metadata + before they are sent to the DataTransferService server. + """ + return request, metadata + + def post_list_transfer_runs( + self, response: datatransfer.ListTransferRunsResponse + ) -> datatransfer.ListTransferRunsResponse: + """Post-rpc interceptor for list_transfer_runs + + Override in a subclass to manipulate the response + after it is returned by the DataTransferService server but before + it is returned to user code. + """ + return response + + def pre_schedule_transfer_runs( + self, + request: datatransfer.ScheduleTransferRunsRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[datatransfer.ScheduleTransferRunsRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for schedule_transfer_runs + + Override in a subclass to manipulate the request or metadata + before they are sent to the DataTransferService server. + """ + return request, metadata + + def post_schedule_transfer_runs( + self, response: datatransfer.ScheduleTransferRunsResponse + ) -> datatransfer.ScheduleTransferRunsResponse: + """Post-rpc interceptor for schedule_transfer_runs + + Override in a subclass to manipulate the response + after it is returned by the DataTransferService server but before + it is returned to user code. + """ + return response + + def pre_start_manual_transfer_runs( + self, + request: datatransfer.StartManualTransferRunsRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[datatransfer.StartManualTransferRunsRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for start_manual_transfer_runs + + Override in a subclass to manipulate the request or metadata + before they are sent to the DataTransferService server. + """ + return request, metadata + + def post_start_manual_transfer_runs( + self, response: datatransfer.StartManualTransferRunsResponse + ) -> datatransfer.StartManualTransferRunsResponse: + """Post-rpc interceptor for start_manual_transfer_runs + + Override in a subclass to manipulate the response + after it is returned by the DataTransferService server but before + it is returned to user code. + """ + return response + + def pre_update_transfer_config( + self, + request: datatransfer.UpdateTransferConfigRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[datatransfer.UpdateTransferConfigRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for update_transfer_config + + Override in a subclass to manipulate the request or metadata + before they are sent to the DataTransferService server. + """ + return request, metadata + + def post_update_transfer_config( + self, response: transfer.TransferConfig + ) -> transfer.TransferConfig: + """Post-rpc interceptor for update_transfer_config + + Override in a subclass to manipulate the response + after it is returned by the DataTransferService server but before + it is returned to user code. + """ + return response + + def pre_get_location( + self, + request: locations_pb2.GetLocationRequest, + metadata: Sequence[Tuple[str, str]], + ) -> locations_pb2.Location: + """Pre-rpc interceptor for get_location + + Override in a subclass to manipulate the request or metadata + before they are sent to the DataTransferService server. + """ + return request, metadata + + def post_get_location( + self, response: locations_pb2.GetLocationRequest + ) -> locations_pb2.Location: + """Post-rpc interceptor for get_location + + Override in a subclass to manipulate the response + after it is returned by the DataTransferService server but before + it is returned to user code. + """ + return response + + def pre_list_locations( + self, + request: locations_pb2.ListLocationsRequest, + metadata: Sequence[Tuple[str, str]], + ) -> locations_pb2.ListLocationsResponse: + """Pre-rpc interceptor for list_locations + + Override in a subclass to manipulate the request or metadata + before they are sent to the DataTransferService server. + """ + return request, metadata + + def post_list_locations( + self, response: locations_pb2.ListLocationsRequest + ) -> locations_pb2.ListLocationsResponse: + """Post-rpc interceptor for list_locations + + Override in a subclass to manipulate the response + after it is returned by the DataTransferService server but before + it is returned to user code. + """ + return response + + +@dataclasses.dataclass +class DataTransferServiceRestStub: + _session: AuthorizedSession + _host: str + _interceptor: DataTransferServiceRestInterceptor + + +class DataTransferServiceRestTransport(DataTransferServiceTransport): + """REST backend transport for DataTransferService. + + This API allows users to manage their data transfers into + BigQuery. + + 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 = "bigquerydatatransfer.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[DataTransferServiceRestInterceptor] = 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 + ) + if client_cert_source_for_mtls: + self._session.configure_mtls_channel(client_cert_source_for_mtls) + self._interceptor = interceptor or DataTransferServiceRestInterceptor() + self._prep_wrapped_messages(client_info) + + class _CheckValidCreds(DataTransferServiceRestStub): + def __hash__(self): + return hash("CheckValidCreds") + + __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: datatransfer.CheckValidCredsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> datatransfer.CheckValidCredsResponse: + r"""Call the check valid creds method over HTTP. + + Args: + request (~.datatransfer.CheckValidCredsRequest): + The request object. A request to determine whether the + user has valid credentials. This method + is used to limit the number of OAuth + popups in the user interface. The user + id is inferred from the API call + context. If the data source has the + Google+ authorization type, this method + returns false, as it cannot be + determined whether the credentials are + already valid merely based on the user + id. + + 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: + ~.datatransfer.CheckValidCredsResponse: + A response indicating whether the + credentials exist and are valid. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{name=projects/*/locations/*/dataSources/*}:checkValidCreds", + "body": "*", + }, + { + "method": "post", + "uri": "/v1/{name=projects/*/dataSources/*}:checkValidCreds", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_check_valid_creds( + request, metadata + ) + pb_request = datatransfer.CheckValidCredsRequest.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 = datatransfer.CheckValidCredsResponse() + pb_resp = datatransfer.CheckValidCredsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_check_valid_creds(resp) + return resp + + class _CreateTransferConfig(DataTransferServiceRestStub): + def __hash__(self): + return hash("CreateTransferConfig") + + __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: datatransfer.CreateTransferConfigRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> transfer.TransferConfig: + r"""Call the create transfer config method over HTTP. + + Args: + request (~.datatransfer.CreateTransferConfigRequest): + The request object. A request to create a data transfer + configuration. If new credentials are + needed for this transfer configuration, + authorization info must be provided. If + authorization info is provided, the + transfer configuration will be + associated with the user id + corresponding to the authorization info. + Otherwise, the transfer configuration + will be associated with the calling + user. + + 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: + ~.transfer.TransferConfig: + Represents a data transfer configuration. A transfer + configuration contains all metadata needed to perform a + data transfer. For example, ``destination_dataset_id`` + specifies where data should be stored. When a new + transfer configuration is created, the specified + ``destination_dataset_id`` is created when needed and + shared with the appropriate data source service account. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{parent=projects/*/locations/*}/transferConfigs", + "body": "transfer_config", + }, + { + "method": "post", + "uri": "/v1/{parent=projects/*}/transferConfigs", + "body": "transfer_config", + }, + ] + request, metadata = self._interceptor.pre_create_transfer_config( + request, metadata + ) + pb_request = datatransfer.CreateTransferConfigRequest.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 = transfer.TransferConfig() + pb_resp = transfer.TransferConfig.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_create_transfer_config(resp) + return resp + + class _DeleteTransferConfig(DataTransferServiceRestStub): + def __hash__(self): + return hash("DeleteTransferConfig") + + __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: datatransfer.DeleteTransferConfigRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ): + r"""Call the delete transfer config method over HTTP. + + Args: + request (~.datatransfer.DeleteTransferConfigRequest): + The request object. A request to delete data transfer + information. All associated transfer + runs and log messages will be deleted as + well. + + 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. + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "delete", + "uri": "/v1/{name=projects/*/locations/*/transferConfigs/*}", + }, + { + "method": "delete", + "uri": "/v1/{name=projects/*/transferConfigs/*}", + }, + ] + request, metadata = self._interceptor.pre_delete_transfer_config( + request, metadata + ) + pb_request = datatransfer.DeleteTransferConfigRequest.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) + + class _DeleteTransferRun(DataTransferServiceRestStub): + def __hash__(self): + return hash("DeleteTransferRun") + + __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: datatransfer.DeleteTransferRunRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ): + r"""Call the delete transfer run method over HTTP. + + Args: + request (~.datatransfer.DeleteTransferRunRequest): + The request object. A request to delete data transfer run + information. + + 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. + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "delete", + "uri": "/v1/{name=projects/*/locations/*/transferConfigs/*/runs/*}", + }, + { + "method": "delete", + "uri": "/v1/{name=projects/*/transferConfigs/*/runs/*}", + }, + ] + request, metadata = self._interceptor.pre_delete_transfer_run( + request, metadata + ) + pb_request = datatransfer.DeleteTransferRunRequest.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) + + class _EnrollDataSources(DataTransferServiceRestStub): + def __hash__(self): + return hash("EnrollDataSources") + + def __call__( + self, + request: datatransfer.EnrollDataSourcesRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ): + r"""Call the enroll data sources method over HTTP. + + Args: + request (~.datatransfer.EnrollDataSourcesRequest): + The request object. A request to enroll a set of data sources so they are + visible in the BigQuery UI's ``Transfer`` tab. + + 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. + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{name=projects/*/locations/*}:enrollDataSources", + "body": "*", + }, + { + "method": "post", + "uri": "/v1/{name=projects/*}:enrollDataSources", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_enroll_data_sources( + request, metadata + ) + pb_request = datatransfer.EnrollDataSourcesRequest.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["$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) + + class _GetDataSource(DataTransferServiceRestStub): + def __hash__(self): + return hash("GetDataSource") + + __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: datatransfer.GetDataSourceRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> datatransfer.DataSource: + r"""Call the get data source method over HTTP. + + Args: + request (~.datatransfer.GetDataSourceRequest): + The request object. A request to get data source info. + 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: + ~.datatransfer.DataSource: + Defines the properties and custom + parameters for a data source. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/dataSources/*}", + }, + { + "method": "get", + "uri": "/v1/{name=projects/*/dataSources/*}", + }, + ] + request, metadata = self._interceptor.pre_get_data_source(request, metadata) + pb_request = datatransfer.GetDataSourceRequest.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 = datatransfer.DataSource() + pb_resp = datatransfer.DataSource.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_data_source(resp) + return resp + + class _GetTransferConfig(DataTransferServiceRestStub): + def __hash__(self): + return hash("GetTransferConfig") + + __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: datatransfer.GetTransferConfigRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> transfer.TransferConfig: + r"""Call the get transfer config method over HTTP. + + Args: + request (~.datatransfer.GetTransferConfigRequest): + The request object. A request to get data transfer + information. + + 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: + ~.transfer.TransferConfig: + Represents a data transfer configuration. A transfer + configuration contains all metadata needed to perform a + data transfer. For example, ``destination_dataset_id`` + specifies where data should be stored. When a new + transfer configuration is created, the specified + ``destination_dataset_id`` is created when needed and + shared with the appropriate data source service account. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/transferConfigs/*}", + }, + { + "method": "get", + "uri": "/v1/{name=projects/*/transferConfigs/*}", + }, + ] + request, metadata = self._interceptor.pre_get_transfer_config( + request, metadata + ) + pb_request = datatransfer.GetTransferConfigRequest.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 = transfer.TransferConfig() + pb_resp = transfer.TransferConfig.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_transfer_config(resp) + return resp + + class _GetTransferRun(DataTransferServiceRestStub): + def __hash__(self): + return hash("GetTransferRun") + + __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: datatransfer.GetTransferRunRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> transfer.TransferRun: + r"""Call the get transfer run method over HTTP. + + Args: + request (~.datatransfer.GetTransferRunRequest): + The request object. A request to get data transfer run + information. + + 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: + ~.transfer.TransferRun: + Represents a data transfer run. + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/transferConfigs/*/runs/*}", + }, + { + "method": "get", + "uri": "/v1/{name=projects/*/transferConfigs/*/runs/*}", + }, + ] + request, metadata = self._interceptor.pre_get_transfer_run( + request, metadata + ) + pb_request = datatransfer.GetTransferRunRequest.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 = transfer.TransferRun() + pb_resp = transfer.TransferRun.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_transfer_run(resp) + return resp + + class _ListDataSources(DataTransferServiceRestStub): + def __hash__(self): + return hash("ListDataSources") + + __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: datatransfer.ListDataSourcesRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> datatransfer.ListDataSourcesResponse: + r"""Call the list data sources method over HTTP. + + Args: + request (~.datatransfer.ListDataSourcesRequest): + The request object. Request to list supported data + sources and their data transfer + settings. + + 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: + ~.datatransfer.ListDataSourcesResponse: + Returns list of supported data + sources and their metadata. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{parent=projects/*/locations/*}/dataSources", + }, + { + "method": "get", + "uri": "/v1/{parent=projects/*}/dataSources", + }, + ] + request, metadata = self._interceptor.pre_list_data_sources( + request, metadata + ) + pb_request = datatransfer.ListDataSourcesRequest.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 = datatransfer.ListDataSourcesResponse() + pb_resp = datatransfer.ListDataSourcesResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_data_sources(resp) + return resp + + class _ListTransferConfigs(DataTransferServiceRestStub): + def __hash__(self): + return hash("ListTransferConfigs") + + __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: datatransfer.ListTransferConfigsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> datatransfer.ListTransferConfigsResponse: + r"""Call the list transfer configs method over HTTP. + + Args: + request (~.datatransfer.ListTransferConfigsRequest): + The request object. A request to list data transfers + configured for a BigQuery project. + + 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: + ~.datatransfer.ListTransferConfigsResponse: + The returned list of pipelines in the + project. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{parent=projects/*/locations/*}/transferConfigs", + }, + { + "method": "get", + "uri": "/v1/{parent=projects/*}/transferConfigs", + }, + ] + request, metadata = self._interceptor.pre_list_transfer_configs( + request, metadata + ) + pb_request = datatransfer.ListTransferConfigsRequest.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 = datatransfer.ListTransferConfigsResponse() + pb_resp = datatransfer.ListTransferConfigsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_transfer_configs(resp) + return resp + + class _ListTransferLogs(DataTransferServiceRestStub): + def __hash__(self): + return hash("ListTransferLogs") + + __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: datatransfer.ListTransferLogsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> datatransfer.ListTransferLogsResponse: + r"""Call the list transfer logs method over HTTP. + + Args: + request (~.datatransfer.ListTransferLogsRequest): + The request object. A request to get user facing log + messages associated with data transfer + run. + + 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: + ~.datatransfer.ListTransferLogsResponse: + The returned list transfer run + messages. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{parent=projects/*/locations/*/transferConfigs/*/runs/*}/transferLogs", + }, + { + "method": "get", + "uri": "/v1/{parent=projects/*/transferConfigs/*/runs/*}/transferLogs", + }, + ] + request, metadata = self._interceptor.pre_list_transfer_logs( + request, metadata + ) + pb_request = datatransfer.ListTransferLogsRequest.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 = datatransfer.ListTransferLogsResponse() + pb_resp = datatransfer.ListTransferLogsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_transfer_logs(resp) + return resp + + class _ListTransferRuns(DataTransferServiceRestStub): + def __hash__(self): + return hash("ListTransferRuns") + + __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: datatransfer.ListTransferRunsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> datatransfer.ListTransferRunsResponse: + r"""Call the list transfer runs method over HTTP. + + Args: + request (~.datatransfer.ListTransferRunsRequest): + The request object. A request to list data transfer runs. + 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: + ~.datatransfer.ListTransferRunsResponse: + The returned list of pipelines in the + project. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{parent=projects/*/locations/*/transferConfigs/*}/runs", + }, + { + "method": "get", + "uri": "/v1/{parent=projects/*/transferConfigs/*}/runs", + }, + ] + request, metadata = self._interceptor.pre_list_transfer_runs( + request, metadata + ) + pb_request = datatransfer.ListTransferRunsRequest.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 = datatransfer.ListTransferRunsResponse() + pb_resp = datatransfer.ListTransferRunsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_transfer_runs(resp) + return resp + + class _ScheduleTransferRuns(DataTransferServiceRestStub): + def __hash__(self): + return hash("ScheduleTransferRuns") + + __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: datatransfer.ScheduleTransferRunsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> datatransfer.ScheduleTransferRunsResponse: + r"""Call the schedule transfer runs method over HTTP. + + Args: + request (~.datatransfer.ScheduleTransferRunsRequest): + The request object. A request to schedule transfer runs + for a time range. + + 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: + ~.datatransfer.ScheduleTransferRunsResponse: + A response to schedule transfer runs + for a time range. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{parent=projects/*/locations/*/transferConfigs/*}:scheduleRuns", + "body": "*", + }, + { + "method": "post", + "uri": "/v1/{parent=projects/*/transferConfigs/*}:scheduleRuns", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_schedule_transfer_runs( + request, metadata + ) + pb_request = datatransfer.ScheduleTransferRunsRequest.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 = datatransfer.ScheduleTransferRunsResponse() + pb_resp = datatransfer.ScheduleTransferRunsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_schedule_transfer_runs(resp) + return resp + + class _StartManualTransferRuns(DataTransferServiceRestStub): + def __hash__(self): + return hash("StartManualTransferRuns") + + def __call__( + self, + request: datatransfer.StartManualTransferRunsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> datatransfer.StartManualTransferRunsResponse: + r"""Call the start manual transfer + runs method over HTTP. + + Args: + request (~.datatransfer.StartManualTransferRunsRequest): + The request object. A request to start manual transfer + runs. + + 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: + ~.datatransfer.StartManualTransferRunsResponse: + A response to start manual transfer + runs. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{parent=projects/*/locations/*/transferConfigs/*}:startManualRuns", + "body": "*", + }, + { + "method": "post", + "uri": "/v1/{parent=projects/*/transferConfigs/*}:startManualRuns", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_start_manual_transfer_runs( + request, metadata + ) + pb_request = datatransfer.StartManualTransferRunsRequest.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["$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 = datatransfer.StartManualTransferRunsResponse() + pb_resp = datatransfer.StartManualTransferRunsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_start_manual_transfer_runs(resp) + return resp + + class _UpdateTransferConfig(DataTransferServiceRestStub): + def __hash__(self): + return hash("UpdateTransferConfig") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = { + "updateMask": {}, + } + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: datatransfer.UpdateTransferConfigRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> transfer.TransferConfig: + r"""Call the update transfer config method over HTTP. + + Args: + request (~.datatransfer.UpdateTransferConfigRequest): + The request object. A request to update a transfer + configuration. To update the user id of + the transfer configuration, + authorization info needs to be provided. + + 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: + ~.transfer.TransferConfig: + Represents a data transfer configuration. A transfer + configuration contains all metadata needed to perform a + data transfer. For example, ``destination_dataset_id`` + specifies where data should be stored. When a new + transfer configuration is created, the specified + ``destination_dataset_id`` is created when needed and + shared with the appropriate data source service account. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "patch", + "uri": "/v1/{transfer_config.name=projects/*/locations/*/transferConfigs/*}", + "body": "transfer_config", + }, + { + "method": "patch", + "uri": "/v1/{transfer_config.name=projects/*/transferConfigs/*}", + "body": "transfer_config", + }, + ] + request, metadata = self._interceptor.pre_update_transfer_config( + request, metadata + ) + pb_request = datatransfer.UpdateTransferConfigRequest.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 = transfer.TransferConfig() + pb_resp = transfer.TransferConfig.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_update_transfer_config(resp) + return resp + + @property + def check_valid_creds( + self, + ) -> Callable[ + [datatransfer.CheckValidCredsRequest], datatransfer.CheckValidCredsResponse + ]: + # 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._CheckValidCreds(self._session, self._host, self._interceptor) # type: ignore + + @property + def create_transfer_config( + self, + ) -> Callable[[datatransfer.CreateTransferConfigRequest], transfer.TransferConfig]: + # 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._CreateTransferConfig(self._session, self._host, self._interceptor) # type: ignore + + @property + def delete_transfer_config( + self, + ) -> Callable[[datatransfer.DeleteTransferConfigRequest], empty_pb2.Empty]: + # 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._DeleteTransferConfig(self._session, self._host, self._interceptor) # type: ignore + + @property + def delete_transfer_run( + self, + ) -> Callable[[datatransfer.DeleteTransferRunRequest], empty_pb2.Empty]: + # 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._DeleteTransferRun(self._session, self._host, self._interceptor) # type: ignore + + @property + def enroll_data_sources( + self, + ) -> Callable[[datatransfer.EnrollDataSourcesRequest], empty_pb2.Empty]: + # 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._EnrollDataSources(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_data_source( + self, + ) -> Callable[[datatransfer.GetDataSourceRequest], datatransfer.DataSource]: + # 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._GetDataSource(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_transfer_config( + self, + ) -> Callable[[datatransfer.GetTransferConfigRequest], transfer.TransferConfig]: + # 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._GetTransferConfig(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_transfer_run( + self, + ) -> Callable[[datatransfer.GetTransferRunRequest], transfer.TransferRun]: + # 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._GetTransferRun(self._session, self._host, self._interceptor) # type: ignore + + @property + def list_data_sources( + self, + ) -> Callable[ + [datatransfer.ListDataSourcesRequest], datatransfer.ListDataSourcesResponse + ]: + # 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._ListDataSources(self._session, self._host, self._interceptor) # type: ignore + + @property + def list_transfer_configs( + self, + ) -> Callable[ + [datatransfer.ListTransferConfigsRequest], + datatransfer.ListTransferConfigsResponse, + ]: + # 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._ListTransferConfigs(self._session, self._host, self._interceptor) # type: ignore + + @property + def list_transfer_logs( + self, + ) -> Callable[ + [datatransfer.ListTransferLogsRequest], datatransfer.ListTransferLogsResponse + ]: + # 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._ListTransferLogs(self._session, self._host, self._interceptor) # type: ignore + + @property + def list_transfer_runs( + self, + ) -> Callable[ + [datatransfer.ListTransferRunsRequest], datatransfer.ListTransferRunsResponse + ]: + # 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._ListTransferRuns(self._session, self._host, self._interceptor) # type: ignore + + @property + def schedule_transfer_runs( + self, + ) -> Callable[ + [datatransfer.ScheduleTransferRunsRequest], + datatransfer.ScheduleTransferRunsResponse, + ]: + # 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._ScheduleTransferRuns(self._session, self._host, self._interceptor) # type: ignore + + @property + def start_manual_transfer_runs( + self, + ) -> Callable[ + [datatransfer.StartManualTransferRunsRequest], + datatransfer.StartManualTransferRunsResponse, + ]: + # 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._StartManualTransferRuns(self._session, self._host, self._interceptor) # type: ignore + + @property + def update_transfer_config( + self, + ) -> Callable[[datatransfer.UpdateTransferConfigRequest], transfer.TransferConfig]: + # 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._UpdateTransferConfig(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_location(self): + return self._GetLocation(self._session, self._host, self._interceptor) # type: ignore + + class _GetLocation(DataTransferServiceRestStub): + def __call__( + self, + request: locations_pb2.GetLocationRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> locations_pb2.Location: + + r"""Call the get location method over HTTP. + + Args: + request (locations_pb2.GetLocationRequest): + The request object for GetLocation 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: + locations_pb2.Location: Response from GetLocation method. + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*}", + }, + ] + + request, metadata = self._interceptor.pre_get_location(request, metadata) + request_kwargs = json_format.MessageToDict(request) + transcoded_request = path_template.transcode(http_options, **request_kwargs) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads(json.dumps(transcoded_request["query_params"])) + + # 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), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + resp = locations_pb2.Location() + resp = json_format.Parse(response.content.decode("utf-8"), resp) + resp = self._interceptor.post_get_location(resp) + return resp + + @property + def list_locations(self): + return self._ListLocations(self._session, self._host, self._interceptor) # type: ignore + + class _ListLocations(DataTransferServiceRestStub): + def __call__( + self, + request: locations_pb2.ListLocationsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> locations_pb2.ListLocationsResponse: + + r"""Call the list locations method over HTTP. + + Args: + request (locations_pb2.ListLocationsRequest): + The request object for ListLocations 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: + locations_pb2.ListLocationsResponse: Response from ListLocations method. + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*}/locations", + }, + ] + + request, metadata = self._interceptor.pre_list_locations(request, metadata) + request_kwargs = json_format.MessageToDict(request) + transcoded_request = path_template.transcode(http_options, **request_kwargs) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads(json.dumps(transcoded_request["query_params"])) + + # 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), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + resp = locations_pb2.ListLocationsResponse() + resp = json_format.Parse(response.content.decode("utf-8"), resp) + resp = self._interceptor.post_list_locations(resp) + return resp + + @property + def kind(self) -> str: + return "rest" + + def close(self): + self._session.close() + + +__all__ = ("DataTransferServiceRestTransport",) diff --git a/packages/google-cloud-bigquery-datatransfer/tests/system.py b/packages/google-cloud-bigquery-datatransfer/tests/system.py index 03e24313ea5c..920c0db723bb 100644 --- a/packages/google-cloud-bigquery-datatransfer/tests/system.py +++ b/packages/google-cloud-bigquery-datatransfer/tests/system.py @@ -26,8 +26,9 @@ def project_id(): return os.environ["PROJECT_ID"] -def test_list_data_sources(project_id): - client = bigquery_datatransfer.DataTransferServiceClient() +@pytest.mark.parametrize("transport", ["grpc", "rest"]) +def test_list_data_sources(project_id: str, transport: str): + client = bigquery_datatransfer.DataTransferServiceClient(transport=transport) parent = client.common_project_path(project_id) data_sources = list(client.list_data_sources(parent=parent)) diff --git a/packages/google-cloud-bigquery-datatransfer/tests/unit/gapic/bigquery_datatransfer_v1/test_data_transfer_service.py b/packages/google-cloud-bigquery-datatransfer/tests/unit/gapic/bigquery_datatransfer_v1/test_data_transfer_service.py index 483c9a7f3388..70ebdf4124e0 100644 --- a/packages/google-cloud-bigquery-datatransfer/tests/unit/gapic/bigquery_datatransfer_v1/test_data_transfer_service.py +++ b/packages/google-cloud-bigquery-datatransfer/tests/unit/gapic/bigquery_datatransfer_v1/test_data_transfer_service.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 gapic_v1, grpc_helpers, grpc_helpers_async, path_template @@ -34,6 +36,7 @@ from google.oauth2 import service_account from google.protobuf import duration_pb2 # type: ignore from google.protobuf import field_mask_pb2 # type: ignore +from google.protobuf import json_format from google.protobuf import struct_pb2 # type: ignore from google.protobuf import timestamp_pb2 # type: ignore from google.rpc import status_pb2 # type: ignore @@ -42,6 +45,8 @@ 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.bigquery_datatransfer_v1.services.data_transfer_service import ( DataTransferServiceAsyncClient, @@ -102,6 +107,7 @@ def test__get_default_mtls_endpoint(): [ (DataTransferServiceClient, "grpc"), (DataTransferServiceAsyncClient, "grpc_asyncio"), + (DataTransferServiceClient, "rest"), ], ) def test_data_transfer_service_client_from_service_account_info( @@ -117,7 +123,11 @@ def test_data_transfer_service_client_from_service_account_info( assert client.transport._credentials == creds assert isinstance(client, client_class) - assert client.transport._host == ("bigquerydatatransfer.googleapis.com:443") + assert client.transport._host == ( + "bigquerydatatransfer.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://bigquerydatatransfer.googleapis.com" + ) @pytest.mark.parametrize( @@ -125,6 +135,7 @@ def test_data_transfer_service_client_from_service_account_info( [ (transports.DataTransferServiceGrpcTransport, "grpc"), (transports.DataTransferServiceGrpcAsyncIOTransport, "grpc_asyncio"), + (transports.DataTransferServiceRestTransport, "rest"), ], ) def test_data_transfer_service_client_service_account_always_use_jwt( @@ -150,6 +161,7 @@ def test_data_transfer_service_client_service_account_always_use_jwt( [ (DataTransferServiceClient, "grpc"), (DataTransferServiceAsyncClient, "grpc_asyncio"), + (DataTransferServiceClient, "rest"), ], ) def test_data_transfer_service_client_from_service_account_file( @@ -172,13 +184,18 @@ def test_data_transfer_service_client_from_service_account_file( assert client.transport._credentials == creds assert isinstance(client, client_class) - assert client.transport._host == ("bigquerydatatransfer.googleapis.com:443") + assert client.transport._host == ( + "bigquerydatatransfer.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://bigquerydatatransfer.googleapis.com" + ) def test_data_transfer_service_client_get_transport_class(): transport = DataTransferServiceClient.get_transport_class() available_transports = [ transports.DataTransferServiceGrpcTransport, + transports.DataTransferServiceRestTransport, ] assert transport in available_transports @@ -199,6 +216,11 @@ def test_data_transfer_service_client_get_transport_class(): transports.DataTransferServiceGrpcAsyncIOTransport, "grpc_asyncio", ), + ( + DataTransferServiceClient, + transports.DataTransferServiceRestTransport, + "rest", + ), ], ) @mock.patch.object( @@ -354,6 +376,18 @@ def test_data_transfer_service_client_client_options( "grpc_asyncio", "false", ), + ( + DataTransferServiceClient, + transports.DataTransferServiceRestTransport, + "rest", + "true", + ), + ( + DataTransferServiceClient, + transports.DataTransferServiceRestTransport, + "rest", + "false", + ), ], ) @mock.patch.object( @@ -557,6 +591,11 @@ def test_data_transfer_service_client_get_mtls_endpoint_and_cert_source(client_c transports.DataTransferServiceGrpcAsyncIOTransport, "grpc_asyncio", ), + ( + DataTransferServiceClient, + transports.DataTransferServiceRestTransport, + "rest", + ), ], ) def test_data_transfer_service_client_client_options_scopes( @@ -597,6 +636,12 @@ def test_data_transfer_service_client_client_options_scopes( "grpc_asyncio", grpc_helpers_async, ), + ( + DataTransferServiceClient, + transports.DataTransferServiceRestTransport, + "rest", + None, + ), ], ) def test_data_transfer_service_client_client_options_credentials_file( @@ -5187,148 +5232,4458 @@ async def test_enroll_data_sources_field_headers_async(): ) in kw["metadata"] -def test_credentials_transport_error(): - # It is an error to provide credentials and a transport instance. - transport = transports.DataTransferServiceGrpcTransport( +@pytest.mark.parametrize( + "request_type", + [ + datatransfer.GetDataSourceRequest, + dict, + ], +) +def test_get_data_source_rest(request_type): + client = DataTransferServiceClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - with pytest.raises(ValueError): - client = DataTransferServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/dataSources/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 = datatransfer.DataSource( + name="name_value", + data_source_id="data_source_id_value", + display_name="display_name_value", + description="description_value", + client_id="client_id_value", + scopes=["scopes_value"], + transfer_type=transfer.TransferType.BATCH, + supports_multiple_transfers=True, + update_deadline_seconds=2406, + default_schedule="default_schedule_value", + supports_custom_schedule=True, + help_url="help_url_value", + authorization_type=datatransfer.DataSource.AuthorizationType.AUTHORIZATION_CODE, + data_refresh_type=datatransfer.DataSource.DataRefreshType.SLIDING_WINDOW, + default_data_refresh_window_days=3379, + manual_runs_disabled=True, ) - # It is an error to provide a credentials file and a transport instance. - transport = transports.DataTransferServiceGrpcTransport( - credentials=ga_credentials.AnonymousCredentials(), + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = datatransfer.DataSource.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_data_source(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, datatransfer.DataSource) + assert response.name == "name_value" + assert response.data_source_id == "data_source_id_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.client_id == "client_id_value" + assert response.scopes == ["scopes_value"] + assert response.transfer_type == transfer.TransferType.BATCH + assert response.supports_multiple_transfers is True + assert response.update_deadline_seconds == 2406 + assert response.default_schedule == "default_schedule_value" + assert response.supports_custom_schedule is True + assert response.help_url == "help_url_value" + assert ( + response.authorization_type + == datatransfer.DataSource.AuthorizationType.AUTHORIZATION_CODE ) - with pytest.raises(ValueError): - client = DataTransferServiceClient( - client_options={"credentials_file": "credentials.json"}, - transport=transport, + assert ( + response.data_refresh_type + == datatransfer.DataSource.DataRefreshType.SLIDING_WINDOW + ) + assert response.default_data_refresh_window_days == 3379 + assert response.manual_runs_disabled is True + + +def test_get_data_source_rest_required_fields( + request_type=datatransfer.GetDataSourceRequest, +): + transport_class = transports.DataTransferServiceRestTransport + + 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, ) + ) - # It is an error to provide an api_key and a transport instance. - transport = transports.DataTransferServiceGrpcTransport( + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_data_source._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_data_source._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 = DataTransferServiceClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = datatransfer.DataSource() + # 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 = datatransfer.DataSource.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_data_source(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_data_source_rest_unset_required_fields(): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials ) - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = DataTransferServiceClient( - 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 = DataTransferServiceClient( - client_options=options, credentials=ga_credentials.AnonymousCredentials() - ) + unset_fields = transport.get_data_source._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) - # It is an error to provide scopes and a transport instance. - transport = transports.DataTransferServiceGrpcTransport( + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_data_source_rest_interceptors(null_interceptor): + transport = transports.DataTransferServiceRestTransport( credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.DataTransferServiceRestInterceptor(), ) - with pytest.raises(ValueError): - client = DataTransferServiceClient( - client_options={"scopes": ["1", "2"]}, - transport=transport, + client = DataTransferServiceClient(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.DataTransferServiceRestInterceptor, "post_get_data_source" + ) as post, mock.patch.object( + transports.DataTransferServiceRestInterceptor, "pre_get_data_source" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = datatransfer.GetDataSourceRequest.pb( + datatransfer.GetDataSourceRequest() + ) + 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 = datatransfer.DataSource.to_json( + datatransfer.DataSource() ) + request = datatransfer.GetDataSourceRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = datatransfer.DataSource() -def test_transport_instance(): - # A client may be instantiated with a custom transport instance. - transport = transports.DataTransferServiceGrpcTransport( + client.get_data_source( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_get_data_source_rest_bad_request( + transport: str = "rest", request_type=datatransfer.GetDataSourceRequest +): + client = DataTransferServiceClient( credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - client = DataTransferServiceClient(transport=transport) - assert client.transport is transport + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/dataSources/sample3"} + request = request_type(**request_init) -def test_transport_get_channel(): - # A client may be instantiated with a custom transport instance. - transport = transports.DataTransferServiceGrpcTransport( + # 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_data_source(request) + + +def test_get_data_source_rest_flattened(): + client = DataTransferServiceClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - channel = transport.grpc_channel - assert channel - transport = transports.DataTransferServiceGrpcAsyncIOTransport( + # 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 = datatransfer.DataSource() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/dataSources/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 = datatransfer.DataSource.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_data_source(**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/*/dataSources/*}" + % client.transport._host, + args[1], + ) + + +def test_get_data_source_rest_flattened_error(transport: str = "rest"): + client = DataTransferServiceClient( credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - channel = transport.grpc_channel - assert channel + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_data_source( + datatransfer.GetDataSourceRequest(), + name="name_value", + ) -@pytest.mark.parametrize( - "transport_class", - [ - transports.DataTransferServiceGrpcTransport, - transports.DataTransferServiceGrpcAsyncIOTransport, - ], -) -def test_transport_adc(transport_class): - # Test default credentials are used if not provided. - with mock.patch.object(google.auth, "default") as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport_class() - adc.assert_called_once() + +def test_get_data_source_rest_error(): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) @pytest.mark.parametrize( - "transport_name", + "request_type", [ - "grpc", + datatransfer.ListDataSourcesRequest, + dict, ], ) -def test_transport_kind(transport_name): - transport = DataTransferServiceClient.get_transport_class(transport_name)( +def test_list_data_sources_rest(request_type): + client = DataTransferServiceClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - assert transport.kind == transport_name + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) -def test_transport_grpc_default(): - # A client should use the gRPC transport by default. - client = DataTransferServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - assert isinstance( - client.transport, - transports.DataTransferServiceGrpcTransport, - ) + # 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 = datatransfer.ListDataSourcesResponse( + next_page_token="next_page_token_value", + ) + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = datatransfer.ListDataSourcesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) -def test_data_transfer_service_base_transport_error(): - # Passing both a credentials object and credentials_file should raise an error - with pytest.raises(core_exceptions.DuplicateCredentialArgs): - transport = transports.DataTransferServiceTransport( - credentials=ga_credentials.AnonymousCredentials(), - credentials_file="credentials.json", + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.list_data_sources(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListDataSourcesPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_data_sources_rest_required_fields( + request_type=datatransfer.ListDataSourcesRequest, +): + transport_class = transports.DataTransferServiceRestTransport + + 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 -def test_data_transfer_service_base_transport(): - # Instantiate the base transport. - with mock.patch( - "google.cloud.bigquery_datatransfer_v1.services.data_transfer_service.transports.DataTransferServiceTransport.__init__" - ) as Transport: - Transport.return_value = None - transport = transports.DataTransferServiceTransport( - credentials=ga_credentials.AnonymousCredentials(), + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_data_sources._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_data_sources._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "page_size", + "page_token", ) + ) + jsonified_request.update(unset_fields) - # Every method on the transport should just blindly - # raise NotImplementedError. - methods = ( - "get_data_source", - "list_data_sources", - "create_transfer_config", - "update_transfer_config", - "delete_transfer_config", + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = datatransfer.ListDataSourcesResponse() + # 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 = datatransfer.ListDataSourcesResponse.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_data_sources(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_data_sources_rest_unset_required_fields(): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_data_sources._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_data_sources_rest_interceptors(null_interceptor): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.DataTransferServiceRestInterceptor(), + ) + client = DataTransferServiceClient(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.DataTransferServiceRestInterceptor, "post_list_data_sources" + ) as post, mock.patch.object( + transports.DataTransferServiceRestInterceptor, "pre_list_data_sources" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = datatransfer.ListDataSourcesRequest.pb( + datatransfer.ListDataSourcesRequest() + ) + 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 = datatransfer.ListDataSourcesResponse.to_json( + datatransfer.ListDataSourcesResponse() + ) + + request = datatransfer.ListDataSourcesRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = datatransfer.ListDataSourcesResponse() + + client.list_data_sources( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_list_data_sources_rest_bad_request( + transport: str = "rest", request_type=datatransfer.ListDataSourcesRequest +): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_data_sources(request) + + +def test_list_data_sources_rest_flattened(): + client = DataTransferServiceClient( + 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 = datatransfer.ListDataSourcesResponse() + + # 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 = datatransfer.ListDataSourcesResponse.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_data_sources(**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/*}/dataSources" + % client.transport._host, + args[1], + ) + + +def test_list_data_sources_rest_flattened_error(transport: str = "rest"): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_data_sources( + datatransfer.ListDataSourcesRequest(), + parent="parent_value", + ) + + +def test_list_data_sources_rest_pager(transport: str = "rest"): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + datatransfer.ListDataSourcesResponse( + data_sources=[ + datatransfer.DataSource(), + datatransfer.DataSource(), + datatransfer.DataSource(), + ], + next_page_token="abc", + ), + datatransfer.ListDataSourcesResponse( + data_sources=[], + next_page_token="def", + ), + datatransfer.ListDataSourcesResponse( + data_sources=[ + datatransfer.DataSource(), + ], + next_page_token="ghi", + ), + datatransfer.ListDataSourcesResponse( + data_sources=[ + datatransfer.DataSource(), + datatransfer.DataSource(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + datatransfer.ListDataSourcesResponse.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_data_sources(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, datatransfer.DataSource) for i in results) + + pages = list(client.list_data_sources(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + datatransfer.CreateTransferConfigRequest, + dict, + ], +) +def test_create_transfer_config_rest(request_type): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["transfer_config"] = { + "name": "name_value", + "destination_dataset_id": "destination_dataset_id_value", + "display_name": "display_name_value", + "data_source_id": "data_source_id_value", + "params": {"fields": {}}, + "schedule": "schedule_value", + "schedule_options": { + "disable_auto_scheduling": True, + "start_time": {"seconds": 751, "nanos": 543}, + "end_time": {}, + }, + "data_refresh_window_days": 2543, + "disabled": True, + "update_time": {}, + "next_run_time": {}, + "state": 2, + "user_id": 747, + "dataset_region": "dataset_region_value", + "notification_pubsub_topic": "notification_pubsub_topic_value", + "email_preferences": {"enable_failure_email": True}, + "owner_info": {"email": "email_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 = transfer.TransferConfig( + name="name_value", + display_name="display_name_value", + data_source_id="data_source_id_value", + schedule="schedule_value", + data_refresh_window_days=2543, + disabled=True, + state=transfer.TransferState.PENDING, + user_id=747, + dataset_region="dataset_region_value", + notification_pubsub_topic="notification_pubsub_topic_value", + destination_dataset_id="destination_dataset_id_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = transfer.TransferConfig.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.create_transfer_config(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, transfer.TransferConfig) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.data_source_id == "data_source_id_value" + assert response.schedule == "schedule_value" + assert response.data_refresh_window_days == 2543 + assert response.disabled is True + assert response.state == transfer.TransferState.PENDING + assert response.user_id == 747 + assert response.dataset_region == "dataset_region_value" + assert response.notification_pubsub_topic == "notification_pubsub_topic_value" + + +def test_create_transfer_config_rest_required_fields( + request_type=datatransfer.CreateTransferConfigRequest, +): + transport_class = transports.DataTransferServiceRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_transfer_config._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_transfer_config._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "authorization_code", + "service_account_name", + "version_info", + ) + ) + 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 = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = transfer.TransferConfig() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = transfer.TransferConfig.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.create_transfer_config(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_transfer_config_rest_unset_required_fields(): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_transfer_config._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "authorizationCode", + "serviceAccountName", + "versionInfo", + ) + ) + & set( + ( + "parent", + "transferConfig", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_transfer_config_rest_interceptors(null_interceptor): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.DataTransferServiceRestInterceptor(), + ) + client = DataTransferServiceClient(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.DataTransferServiceRestInterceptor, "post_create_transfer_config" + ) as post, mock.patch.object( + transports.DataTransferServiceRestInterceptor, "pre_create_transfer_config" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = datatransfer.CreateTransferConfigRequest.pb( + datatransfer.CreateTransferConfigRequest() + ) + 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 = transfer.TransferConfig.to_json( + transfer.TransferConfig() + ) + + request = datatransfer.CreateTransferConfigRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = transfer.TransferConfig() + + client.create_transfer_config( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_create_transfer_config_rest_bad_request( + transport: str = "rest", request_type=datatransfer.CreateTransferConfigRequest +): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["transfer_config"] = { + "name": "name_value", + "destination_dataset_id": "destination_dataset_id_value", + "display_name": "display_name_value", + "data_source_id": "data_source_id_value", + "params": {"fields": {}}, + "schedule": "schedule_value", + "schedule_options": { + "disable_auto_scheduling": True, + "start_time": {"seconds": 751, "nanos": 543}, + "end_time": {}, + }, + "data_refresh_window_days": 2543, + "disabled": True, + "update_time": {}, + "next_run_time": {}, + "state": 2, + "user_id": 747, + "dataset_region": "dataset_region_value", + "notification_pubsub_topic": "notification_pubsub_topic_value", + "email_preferences": {"enable_failure_email": True}, + "owner_info": {"email": "email_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_transfer_config(request) + + +def test_create_transfer_config_rest_flattened(): + client = DataTransferServiceClient( + 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 = transfer.TransferConfig() + + # 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", + transfer_config=transfer.TransferConfig(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 = transfer.TransferConfig.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.create_transfer_config(**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/*}/transferConfigs" + % client.transport._host, + args[1], + ) + + +def test_create_transfer_config_rest_flattened_error(transport: str = "rest"): + client = DataTransferServiceClient( + 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_transfer_config( + datatransfer.CreateTransferConfigRequest(), + parent="parent_value", + transfer_config=transfer.TransferConfig(name="name_value"), + ) + + +def test_create_transfer_config_rest_error(): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + datatransfer.UpdateTransferConfigRequest, + dict, + ], +) +def test_update_transfer_config_rest(request_type): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "transfer_config": { + "name": "projects/sample1/locations/sample2/transferConfigs/sample3" + } + } + request_init["transfer_config"] = { + "name": "projects/sample1/locations/sample2/transferConfigs/sample3", + "destination_dataset_id": "destination_dataset_id_value", + "display_name": "display_name_value", + "data_source_id": "data_source_id_value", + "params": {"fields": {}}, + "schedule": "schedule_value", + "schedule_options": { + "disable_auto_scheduling": True, + "start_time": {"seconds": 751, "nanos": 543}, + "end_time": {}, + }, + "data_refresh_window_days": 2543, + "disabled": True, + "update_time": {}, + "next_run_time": {}, + "state": 2, + "user_id": 747, + "dataset_region": "dataset_region_value", + "notification_pubsub_topic": "notification_pubsub_topic_value", + "email_preferences": {"enable_failure_email": True}, + "owner_info": {"email": "email_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 = transfer.TransferConfig( + name="name_value", + display_name="display_name_value", + data_source_id="data_source_id_value", + schedule="schedule_value", + data_refresh_window_days=2543, + disabled=True, + state=transfer.TransferState.PENDING, + user_id=747, + dataset_region="dataset_region_value", + notification_pubsub_topic="notification_pubsub_topic_value", + destination_dataset_id="destination_dataset_id_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = transfer.TransferConfig.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.update_transfer_config(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, transfer.TransferConfig) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.data_source_id == "data_source_id_value" + assert response.schedule == "schedule_value" + assert response.data_refresh_window_days == 2543 + assert response.disabled is True + assert response.state == transfer.TransferState.PENDING + assert response.user_id == 747 + assert response.dataset_region == "dataset_region_value" + assert response.notification_pubsub_topic == "notification_pubsub_topic_value" + + +def test_update_transfer_config_rest_required_fields( + request_type=datatransfer.UpdateTransferConfigRequest, +): + transport_class = transports.DataTransferServiceRestTransport + + 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_transfer_config._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_transfer_config._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "authorization_code", + "service_account_name", + "update_mask", + "version_info", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = transfer.TransferConfig() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "patch", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = transfer.TransferConfig.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.update_transfer_config(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_update_transfer_config_rest_unset_required_fields(): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.update_transfer_config._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "authorizationCode", + "serviceAccountName", + "updateMask", + "versionInfo", + ) + ) + & set( + ( + "transferConfig", + "updateMask", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_update_transfer_config_rest_interceptors(null_interceptor): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.DataTransferServiceRestInterceptor(), + ) + client = DataTransferServiceClient(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.DataTransferServiceRestInterceptor, "post_update_transfer_config" + ) as post, mock.patch.object( + transports.DataTransferServiceRestInterceptor, "pre_update_transfer_config" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = datatransfer.UpdateTransferConfigRequest.pb( + datatransfer.UpdateTransferConfigRequest() + ) + 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 = transfer.TransferConfig.to_json( + transfer.TransferConfig() + ) + + request = datatransfer.UpdateTransferConfigRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = transfer.TransferConfig() + + client.update_transfer_config( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_update_transfer_config_rest_bad_request( + transport: str = "rest", request_type=datatransfer.UpdateTransferConfigRequest +): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "transfer_config": { + "name": "projects/sample1/locations/sample2/transferConfigs/sample3" + } + } + request_init["transfer_config"] = { + "name": "projects/sample1/locations/sample2/transferConfigs/sample3", + "destination_dataset_id": "destination_dataset_id_value", + "display_name": "display_name_value", + "data_source_id": "data_source_id_value", + "params": {"fields": {}}, + "schedule": "schedule_value", + "schedule_options": { + "disable_auto_scheduling": True, + "start_time": {"seconds": 751, "nanos": 543}, + "end_time": {}, + }, + "data_refresh_window_days": 2543, + "disabled": True, + "update_time": {}, + "next_run_time": {}, + "state": 2, + "user_id": 747, + "dataset_region": "dataset_region_value", + "notification_pubsub_topic": "notification_pubsub_topic_value", + "email_preferences": {"enable_failure_email": True}, + "owner_info": {"email": "email_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_transfer_config(request) + + +def test_update_transfer_config_rest_flattened(): + client = DataTransferServiceClient( + 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 = transfer.TransferConfig() + + # get arguments that satisfy an http rule for this method + sample_request = { + "transfer_config": { + "name": "projects/sample1/locations/sample2/transferConfigs/sample3" + } + } + + # get truthy value for each flattened field + mock_args = dict( + transfer_config=transfer.TransferConfig(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 + pb_return_value = transfer.TransferConfig.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.update_transfer_config(**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/{transfer_config.name=projects/*/locations/*/transferConfigs/*}" + % client.transport._host, + args[1], + ) + + +def test_update_transfer_config_rest_flattened_error(transport: str = "rest"): + client = DataTransferServiceClient( + 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_transfer_config( + datatransfer.UpdateTransferConfigRequest(), + transfer_config=transfer.TransferConfig(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + +def test_update_transfer_config_rest_error(): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + datatransfer.DeleteTransferConfigRequest, + dict, + ], +) +def test_delete_transfer_config_rest(request_type): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/transferConfigs/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 = None + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = "" + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.delete_transfer_config(request) + + # Establish that the response is the type that we expect. + assert response is None + + +def test_delete_transfer_config_rest_required_fields( + request_type=datatransfer.DeleteTransferConfigRequest, +): + transport_class = transports.DataTransferServiceRestTransport + + 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_transfer_config._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_transfer_config._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 = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = None + # 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 = "" + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.delete_transfer_config(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_transfer_config_rest_unset_required_fields(): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_transfer_config._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_delete_transfer_config_rest_interceptors(null_interceptor): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.DataTransferServiceRestInterceptor(), + ) + client = DataTransferServiceClient(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.DataTransferServiceRestInterceptor, "pre_delete_transfer_config" + ) as pre: + pre.assert_not_called() + pb_message = datatransfer.DeleteTransferConfigRequest.pb( + datatransfer.DeleteTransferConfigRequest() + ) + 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() + + request = datatransfer.DeleteTransferConfigRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + + client.delete_transfer_config( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + + +def test_delete_transfer_config_rest_bad_request( + transport: str = "rest", request_type=datatransfer.DeleteTransferConfigRequest +): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/transferConfigs/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_transfer_config(request) + + +def test_delete_transfer_config_rest_flattened(): + client = DataTransferServiceClient( + 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 = None + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/transferConfigs/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 = "" + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.delete_transfer_config(**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/*/transferConfigs/*}" + % client.transport._host, + args[1], + ) + + +def test_delete_transfer_config_rest_flattened_error(transport: str = "rest"): + client = DataTransferServiceClient( + 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_transfer_config( + datatransfer.DeleteTransferConfigRequest(), + name="name_value", + ) + + +def test_delete_transfer_config_rest_error(): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + datatransfer.GetTransferConfigRequest, + dict, + ], +) +def test_get_transfer_config_rest(request_type): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/transferConfigs/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 = transfer.TransferConfig( + name="name_value", + display_name="display_name_value", + data_source_id="data_source_id_value", + schedule="schedule_value", + data_refresh_window_days=2543, + disabled=True, + state=transfer.TransferState.PENDING, + user_id=747, + dataset_region="dataset_region_value", + notification_pubsub_topic="notification_pubsub_topic_value", + destination_dataset_id="destination_dataset_id_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = transfer.TransferConfig.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_transfer_config(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, transfer.TransferConfig) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.data_source_id == "data_source_id_value" + assert response.schedule == "schedule_value" + assert response.data_refresh_window_days == 2543 + assert response.disabled is True + assert response.state == transfer.TransferState.PENDING + assert response.user_id == 747 + assert response.dataset_region == "dataset_region_value" + assert response.notification_pubsub_topic == "notification_pubsub_topic_value" + + +def test_get_transfer_config_rest_required_fields( + request_type=datatransfer.GetTransferConfigRequest, +): + transport_class = transports.DataTransferServiceRestTransport + + 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_transfer_config._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_transfer_config._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 = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = transfer.TransferConfig() + # 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 = transfer.TransferConfig.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_transfer_config(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_transfer_config_rest_unset_required_fields(): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_transfer_config._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_transfer_config_rest_interceptors(null_interceptor): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.DataTransferServiceRestInterceptor(), + ) + client = DataTransferServiceClient(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.DataTransferServiceRestInterceptor, "post_get_transfer_config" + ) as post, mock.patch.object( + transports.DataTransferServiceRestInterceptor, "pre_get_transfer_config" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = datatransfer.GetTransferConfigRequest.pb( + datatransfer.GetTransferConfigRequest() + ) + 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 = transfer.TransferConfig.to_json( + transfer.TransferConfig() + ) + + request = datatransfer.GetTransferConfigRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = transfer.TransferConfig() + + client.get_transfer_config( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_get_transfer_config_rest_bad_request( + transport: str = "rest", request_type=datatransfer.GetTransferConfigRequest +): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/transferConfigs/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_transfer_config(request) + + +def test_get_transfer_config_rest_flattened(): + client = DataTransferServiceClient( + 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 = transfer.TransferConfig() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/transferConfigs/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 = transfer.TransferConfig.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_transfer_config(**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/*/transferConfigs/*}" + % client.transport._host, + args[1], + ) + + +def test_get_transfer_config_rest_flattened_error(transport: str = "rest"): + client = DataTransferServiceClient( + 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_transfer_config( + datatransfer.GetTransferConfigRequest(), + name="name_value", + ) + + +def test_get_transfer_config_rest_error(): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + datatransfer.ListTransferConfigsRequest, + dict, + ], +) +def test_list_transfer_configs_rest(request_type): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = datatransfer.ListTransferConfigsResponse( + next_page_token="next_page_token_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = datatransfer.ListTransferConfigsResponse.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_transfer_configs(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListTransferConfigsPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_transfer_configs_rest_required_fields( + request_type=datatransfer.ListTransferConfigsRequest, +): + transport_class = transports.DataTransferServiceRestTransport + + 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_transfer_configs._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_transfer_configs._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "data_source_ids", + "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 = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = datatransfer.ListTransferConfigsResponse() + # 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 = datatransfer.ListTransferConfigsResponse.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_transfer_configs(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_transfer_configs_rest_unset_required_fields(): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_transfer_configs._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "dataSourceIds", + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_transfer_configs_rest_interceptors(null_interceptor): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.DataTransferServiceRestInterceptor(), + ) + client = DataTransferServiceClient(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.DataTransferServiceRestInterceptor, "post_list_transfer_configs" + ) as post, mock.patch.object( + transports.DataTransferServiceRestInterceptor, "pre_list_transfer_configs" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = datatransfer.ListTransferConfigsRequest.pb( + datatransfer.ListTransferConfigsRequest() + ) + 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 = datatransfer.ListTransferConfigsResponse.to_json( + datatransfer.ListTransferConfigsResponse() + ) + + request = datatransfer.ListTransferConfigsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = datatransfer.ListTransferConfigsResponse() + + client.list_transfer_configs( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_list_transfer_configs_rest_bad_request( + transport: str = "rest", request_type=datatransfer.ListTransferConfigsRequest +): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_transfer_configs(request) + + +def test_list_transfer_configs_rest_flattened(): + client = DataTransferServiceClient( + 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 = datatransfer.ListTransferConfigsResponse() + + # 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 = datatransfer.ListTransferConfigsResponse.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_transfer_configs(**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/*}/transferConfigs" + % client.transport._host, + args[1], + ) + + +def test_list_transfer_configs_rest_flattened_error(transport: str = "rest"): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_transfer_configs( + datatransfer.ListTransferConfigsRequest(), + parent="parent_value", + ) + + +def test_list_transfer_configs_rest_pager(transport: str = "rest"): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + datatransfer.ListTransferConfigsResponse( + transfer_configs=[ + transfer.TransferConfig(), + transfer.TransferConfig(), + transfer.TransferConfig(), + ], + next_page_token="abc", + ), + datatransfer.ListTransferConfigsResponse( + transfer_configs=[], + next_page_token="def", + ), + datatransfer.ListTransferConfigsResponse( + transfer_configs=[ + transfer.TransferConfig(), + ], + next_page_token="ghi", + ), + datatransfer.ListTransferConfigsResponse( + transfer_configs=[ + transfer.TransferConfig(), + transfer.TransferConfig(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + datatransfer.ListTransferConfigsResponse.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_transfer_configs(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, transfer.TransferConfig) for i in results) + + pages = list(client.list_transfer_configs(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + datatransfer.ScheduleTransferRunsRequest, + dict, + ], +) +def test_schedule_transfer_runs_rest(request_type): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "parent": "projects/sample1/locations/sample2/transferConfigs/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 = datatransfer.ScheduleTransferRunsResponse() + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = datatransfer.ScheduleTransferRunsResponse.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.schedule_transfer_runs(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, datatransfer.ScheduleTransferRunsResponse) + + +def test_schedule_transfer_runs_rest_required_fields( + request_type=datatransfer.ScheduleTransferRunsRequest, +): + transport_class = transports.DataTransferServiceRestTransport + + 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() + ).schedule_transfer_runs._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() + ).schedule_transfer_runs._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = datatransfer.ScheduleTransferRunsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = datatransfer.ScheduleTransferRunsResponse.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.schedule_transfer_runs(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_schedule_transfer_runs_rest_unset_required_fields(): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.schedule_transfer_runs._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(()) + & set( + ( + "parent", + "startTime", + "endTime", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_schedule_transfer_runs_rest_interceptors(null_interceptor): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.DataTransferServiceRestInterceptor(), + ) + client = DataTransferServiceClient(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.DataTransferServiceRestInterceptor, "post_schedule_transfer_runs" + ) as post, mock.patch.object( + transports.DataTransferServiceRestInterceptor, "pre_schedule_transfer_runs" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = datatransfer.ScheduleTransferRunsRequest.pb( + datatransfer.ScheduleTransferRunsRequest() + ) + 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 = datatransfer.ScheduleTransferRunsResponse.to_json( + datatransfer.ScheduleTransferRunsResponse() + ) + + request = datatransfer.ScheduleTransferRunsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = datatransfer.ScheduleTransferRunsResponse() + + client.schedule_transfer_runs( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_schedule_transfer_runs_rest_bad_request( + transport: str = "rest", request_type=datatransfer.ScheduleTransferRunsRequest +): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "parent": "projects/sample1/locations/sample2/transferConfigs/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.schedule_transfer_runs(request) + + +def test_schedule_transfer_runs_rest_flattened(): + client = DataTransferServiceClient( + 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 = datatransfer.ScheduleTransferRunsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = { + "parent": "projects/sample1/locations/sample2/transferConfigs/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + start_time=timestamp_pb2.Timestamp(seconds=751), + end_time=timestamp_pb2.Timestamp(seconds=751), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = datatransfer.ScheduleTransferRunsResponse.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.schedule_transfer_runs(**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/*/transferConfigs/*}:scheduleRuns" + % client.transport._host, + args[1], + ) + + +def test_schedule_transfer_runs_rest_flattened_error(transport: str = "rest"): + client = DataTransferServiceClient( + 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.schedule_transfer_runs( + datatransfer.ScheduleTransferRunsRequest(), + parent="parent_value", + start_time=timestamp_pb2.Timestamp(seconds=751), + end_time=timestamp_pb2.Timestamp(seconds=751), + ) + + +def test_schedule_transfer_runs_rest_error(): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + datatransfer.StartManualTransferRunsRequest, + dict, + ], +) +def test_start_manual_transfer_runs_rest(request_type): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "parent": "projects/sample1/locations/sample2/transferConfigs/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 = datatransfer.StartManualTransferRunsResponse() + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = datatransfer.StartManualTransferRunsResponse.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.start_manual_transfer_runs(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, datatransfer.StartManualTransferRunsResponse) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_start_manual_transfer_runs_rest_interceptors(null_interceptor): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.DataTransferServiceRestInterceptor(), + ) + client = DataTransferServiceClient(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.DataTransferServiceRestInterceptor, "post_start_manual_transfer_runs" + ) as post, mock.patch.object( + transports.DataTransferServiceRestInterceptor, "pre_start_manual_transfer_runs" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = datatransfer.StartManualTransferRunsRequest.pb( + datatransfer.StartManualTransferRunsRequest() + ) + 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 = ( + datatransfer.StartManualTransferRunsResponse.to_json( + datatransfer.StartManualTransferRunsResponse() + ) + ) + + request = datatransfer.StartManualTransferRunsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = datatransfer.StartManualTransferRunsResponse() + + client.start_manual_transfer_runs( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_start_manual_transfer_runs_rest_bad_request( + transport: str = "rest", request_type=datatransfer.StartManualTransferRunsRequest +): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "parent": "projects/sample1/locations/sample2/transferConfigs/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.start_manual_transfer_runs(request) + + +def test_start_manual_transfer_runs_rest_error(): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + datatransfer.GetTransferRunRequest, + dict, + ], +) +def test_get_transfer_run_rest(request_type): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/transferConfigs/sample3/runs/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = transfer.TransferRun( + name="name_value", + data_source_id="data_source_id_value", + state=transfer.TransferState.PENDING, + user_id=747, + schedule="schedule_value", + notification_pubsub_topic="notification_pubsub_topic_value", + destination_dataset_id="destination_dataset_id_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = transfer.TransferRun.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_transfer_run(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, transfer.TransferRun) + assert response.name == "name_value" + assert response.data_source_id == "data_source_id_value" + assert response.state == transfer.TransferState.PENDING + assert response.user_id == 747 + assert response.schedule == "schedule_value" + assert response.notification_pubsub_topic == "notification_pubsub_topic_value" + + +def test_get_transfer_run_rest_required_fields( + request_type=datatransfer.GetTransferRunRequest, +): + transport_class = transports.DataTransferServiceRestTransport + + 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_transfer_run._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_transfer_run._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 = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = transfer.TransferRun() + # 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 = transfer.TransferRun.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_transfer_run(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_transfer_run_rest_unset_required_fields(): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_transfer_run._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_transfer_run_rest_interceptors(null_interceptor): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.DataTransferServiceRestInterceptor(), + ) + client = DataTransferServiceClient(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.DataTransferServiceRestInterceptor, "post_get_transfer_run" + ) as post, mock.patch.object( + transports.DataTransferServiceRestInterceptor, "pre_get_transfer_run" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = datatransfer.GetTransferRunRequest.pb( + datatransfer.GetTransferRunRequest() + ) + 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 = transfer.TransferRun.to_json(transfer.TransferRun()) + + request = datatransfer.GetTransferRunRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = transfer.TransferRun() + + client.get_transfer_run( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_get_transfer_run_rest_bad_request( + transport: str = "rest", request_type=datatransfer.GetTransferRunRequest +): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/transferConfigs/sample3/runs/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.get_transfer_run(request) + + +def test_get_transfer_run_rest_flattened(): + client = DataTransferServiceClient( + 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 = transfer.TransferRun() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/transferConfigs/sample3/runs/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = transfer.TransferRun.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_transfer_run(**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/*/transferConfigs/*/runs/*}" + % client.transport._host, + args[1], + ) + + +def test_get_transfer_run_rest_flattened_error(transport: str = "rest"): + client = DataTransferServiceClient( + 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_transfer_run( + datatransfer.GetTransferRunRequest(), + name="name_value", + ) + + +def test_get_transfer_run_rest_error(): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + datatransfer.DeleteTransferRunRequest, + dict, + ], +) +def test_delete_transfer_run_rest(request_type): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/transferConfigs/sample3/runs/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = "" + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.delete_transfer_run(request) + + # Establish that the response is the type that we expect. + assert response is None + + +def test_delete_transfer_run_rest_required_fields( + request_type=datatransfer.DeleteTransferRunRequest, +): + transport_class = transports.DataTransferServiceRestTransport + + 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_transfer_run._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_transfer_run._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 = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = None + # 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 = "" + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.delete_transfer_run(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_transfer_run_rest_unset_required_fields(): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_transfer_run._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_delete_transfer_run_rest_interceptors(null_interceptor): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.DataTransferServiceRestInterceptor(), + ) + client = DataTransferServiceClient(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.DataTransferServiceRestInterceptor, "pre_delete_transfer_run" + ) as pre: + pre.assert_not_called() + pb_message = datatransfer.DeleteTransferRunRequest.pb( + datatransfer.DeleteTransferRunRequest() + ) + 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() + + request = datatransfer.DeleteTransferRunRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + + client.delete_transfer_run( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + + +def test_delete_transfer_run_rest_bad_request( + transport: str = "rest", request_type=datatransfer.DeleteTransferRunRequest +): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/transferConfigs/sample3/runs/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.delete_transfer_run(request) + + +def test_delete_transfer_run_rest_flattened(): + client = DataTransferServiceClient( + 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 = None + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/transferConfigs/sample3/runs/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = "" + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.delete_transfer_run(**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/*/transferConfigs/*/runs/*}" + % client.transport._host, + args[1], + ) + + +def test_delete_transfer_run_rest_flattened_error(transport: str = "rest"): + client = DataTransferServiceClient( + 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_transfer_run( + datatransfer.DeleteTransferRunRequest(), + name="name_value", + ) + + +def test_delete_transfer_run_rest_error(): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + datatransfer.ListTransferRunsRequest, + dict, + ], +) +def test_list_transfer_runs_rest(request_type): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "parent": "projects/sample1/locations/sample2/transferConfigs/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 = datatransfer.ListTransferRunsResponse( + next_page_token="next_page_token_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = datatransfer.ListTransferRunsResponse.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_transfer_runs(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListTransferRunsPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_transfer_runs_rest_required_fields( + request_type=datatransfer.ListTransferRunsRequest, +): + transport_class = transports.DataTransferServiceRestTransport + + 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_transfer_runs._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_transfer_runs._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "page_size", + "page_token", + "run_attempt", + "states", + ) + ) + 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 = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = datatransfer.ListTransferRunsResponse() + # 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 = datatransfer.ListTransferRunsResponse.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_transfer_runs(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_transfer_runs_rest_unset_required_fields(): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_transfer_runs._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "pageSize", + "pageToken", + "runAttempt", + "states", + ) + ) + & set(("parent",)) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_transfer_runs_rest_interceptors(null_interceptor): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.DataTransferServiceRestInterceptor(), + ) + client = DataTransferServiceClient(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.DataTransferServiceRestInterceptor, "post_list_transfer_runs" + ) as post, mock.patch.object( + transports.DataTransferServiceRestInterceptor, "pre_list_transfer_runs" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = datatransfer.ListTransferRunsRequest.pb( + datatransfer.ListTransferRunsRequest() + ) + 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 = datatransfer.ListTransferRunsResponse.to_json( + datatransfer.ListTransferRunsResponse() + ) + + request = datatransfer.ListTransferRunsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = datatransfer.ListTransferRunsResponse() + + client.list_transfer_runs( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_list_transfer_runs_rest_bad_request( + transport: str = "rest", request_type=datatransfer.ListTransferRunsRequest +): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "parent": "projects/sample1/locations/sample2/transferConfigs/sample3" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_transfer_runs(request) + + +def test_list_transfer_runs_rest_flattened(): + client = DataTransferServiceClient( + 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 = datatransfer.ListTransferRunsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = { + "parent": "projects/sample1/locations/sample2/transferConfigs/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = datatransfer.ListTransferRunsResponse.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_transfer_runs(**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/*/transferConfigs/*}/runs" + % client.transport._host, + args[1], + ) + + +def test_list_transfer_runs_rest_flattened_error(transport: str = "rest"): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_transfer_runs( + datatransfer.ListTransferRunsRequest(), + parent="parent_value", + ) + + +def test_list_transfer_runs_rest_pager(transport: str = "rest"): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + datatransfer.ListTransferRunsResponse( + transfer_runs=[ + transfer.TransferRun(), + transfer.TransferRun(), + transfer.TransferRun(), + ], + next_page_token="abc", + ), + datatransfer.ListTransferRunsResponse( + transfer_runs=[], + next_page_token="def", + ), + datatransfer.ListTransferRunsResponse( + transfer_runs=[ + transfer.TransferRun(), + ], + next_page_token="ghi", + ), + datatransfer.ListTransferRunsResponse( + transfer_runs=[ + transfer.TransferRun(), + transfer.TransferRun(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + datatransfer.ListTransferRunsResponse.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/transferConfigs/sample3" + } + + pager = client.list_transfer_runs(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, transfer.TransferRun) for i in results) + + pages = list(client.list_transfer_runs(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + datatransfer.ListTransferLogsRequest, + dict, + ], +) +def test_list_transfer_logs_rest(request_type): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "parent": "projects/sample1/locations/sample2/transferConfigs/sample3/runs/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = datatransfer.ListTransferLogsResponse( + next_page_token="next_page_token_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = datatransfer.ListTransferLogsResponse.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_transfer_logs(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListTransferLogsPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_transfer_logs_rest_required_fields( + request_type=datatransfer.ListTransferLogsRequest, +): + transport_class = transports.DataTransferServiceRestTransport + + 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_transfer_logs._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_transfer_logs._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "message_types", + "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 = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = datatransfer.ListTransferLogsResponse() + # 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 = datatransfer.ListTransferLogsResponse.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_transfer_logs(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_transfer_logs_rest_unset_required_fields(): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_transfer_logs._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "messageTypes", + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_transfer_logs_rest_interceptors(null_interceptor): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.DataTransferServiceRestInterceptor(), + ) + client = DataTransferServiceClient(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.DataTransferServiceRestInterceptor, "post_list_transfer_logs" + ) as post, mock.patch.object( + transports.DataTransferServiceRestInterceptor, "pre_list_transfer_logs" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = datatransfer.ListTransferLogsRequest.pb( + datatransfer.ListTransferLogsRequest() + ) + 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 = datatransfer.ListTransferLogsResponse.to_json( + datatransfer.ListTransferLogsResponse() + ) + + request = datatransfer.ListTransferLogsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = datatransfer.ListTransferLogsResponse() + + client.list_transfer_logs( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_list_transfer_logs_rest_bad_request( + transport: str = "rest", request_type=datatransfer.ListTransferLogsRequest +): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "parent": "projects/sample1/locations/sample2/transferConfigs/sample3/runs/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_transfer_logs(request) + + +def test_list_transfer_logs_rest_flattened(): + client = DataTransferServiceClient( + 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 = datatransfer.ListTransferLogsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = { + "parent": "projects/sample1/locations/sample2/transferConfigs/sample3/runs/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = datatransfer.ListTransferLogsResponse.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_transfer_logs(**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/*/transferConfigs/*/runs/*}/transferLogs" + % client.transport._host, + args[1], + ) + + +def test_list_transfer_logs_rest_flattened_error(transport: str = "rest"): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_transfer_logs( + datatransfer.ListTransferLogsRequest(), + parent="parent_value", + ) + + +def test_list_transfer_logs_rest_pager(transport: str = "rest"): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + datatransfer.ListTransferLogsResponse( + transfer_messages=[ + transfer.TransferMessage(), + transfer.TransferMessage(), + transfer.TransferMessage(), + ], + next_page_token="abc", + ), + datatransfer.ListTransferLogsResponse( + transfer_messages=[], + next_page_token="def", + ), + datatransfer.ListTransferLogsResponse( + transfer_messages=[ + transfer.TransferMessage(), + ], + next_page_token="ghi", + ), + datatransfer.ListTransferLogsResponse( + transfer_messages=[ + transfer.TransferMessage(), + transfer.TransferMessage(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + datatransfer.ListTransferLogsResponse.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/transferConfigs/sample3/runs/sample4" + } + + pager = client.list_transfer_logs(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, transfer.TransferMessage) for i in results) + + pages = list(client.list_transfer_logs(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + datatransfer.CheckValidCredsRequest, + dict, + ], +) +def test_check_valid_creds_rest(request_type): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/dataSources/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 = datatransfer.CheckValidCredsResponse( + has_valid_creds=True, + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = datatransfer.CheckValidCredsResponse.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.check_valid_creds(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, datatransfer.CheckValidCredsResponse) + assert response.has_valid_creds is True + + +def test_check_valid_creds_rest_required_fields( + request_type=datatransfer.CheckValidCredsRequest, +): + transport_class = transports.DataTransferServiceRestTransport + + 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() + ).check_valid_creds._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() + ).check_valid_creds._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 = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = datatransfer.CheckValidCredsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = datatransfer.CheckValidCredsResponse.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.check_valid_creds(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_check_valid_creds_rest_unset_required_fields(): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.check_valid_creds._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_check_valid_creds_rest_interceptors(null_interceptor): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.DataTransferServiceRestInterceptor(), + ) + client = DataTransferServiceClient(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.DataTransferServiceRestInterceptor, "post_check_valid_creds" + ) as post, mock.patch.object( + transports.DataTransferServiceRestInterceptor, "pre_check_valid_creds" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = datatransfer.CheckValidCredsRequest.pb( + datatransfer.CheckValidCredsRequest() + ) + 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 = datatransfer.CheckValidCredsResponse.to_json( + datatransfer.CheckValidCredsResponse() + ) + + request = datatransfer.CheckValidCredsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = datatransfer.CheckValidCredsResponse() + + client.check_valid_creds( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_check_valid_creds_rest_bad_request( + transport: str = "rest", request_type=datatransfer.CheckValidCredsRequest +): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/dataSources/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.check_valid_creds(request) + + +def test_check_valid_creds_rest_flattened(): + client = DataTransferServiceClient( + 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 = datatransfer.CheckValidCredsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/dataSources/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 = datatransfer.CheckValidCredsResponse.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.check_valid_creds(**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/*/dataSources/*}:checkValidCreds" + % client.transport._host, + args[1], + ) + + +def test_check_valid_creds_rest_flattened_error(transport: str = "rest"): + client = DataTransferServiceClient( + 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.check_valid_creds( + datatransfer.CheckValidCredsRequest(), + name="name_value", + ) + + +def test_check_valid_creds_rest_error(): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + datatransfer.EnrollDataSourcesRequest, + dict, + ], +) +def test_enroll_data_sources_rest(request_type): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "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 = None + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = "" + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.enroll_data_sources(request) + + # Establish that the response is the type that we expect. + assert response is None + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_enroll_data_sources_rest_interceptors(null_interceptor): + transport = transports.DataTransferServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.DataTransferServiceRestInterceptor(), + ) + client = DataTransferServiceClient(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.DataTransferServiceRestInterceptor, "pre_enroll_data_sources" + ) as pre: + pre.assert_not_called() + pb_message = datatransfer.EnrollDataSourcesRequest.pb( + datatransfer.EnrollDataSourcesRequest() + ) + 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() + + request = datatransfer.EnrollDataSourcesRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + + client.enroll_data_sources( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + + +def test_enroll_data_sources_rest_bad_request( + transport: str = "rest", request_type=datatransfer.EnrollDataSourcesRequest +): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.enroll_data_sources(request) + + +def test_enroll_data_sources_rest_error(): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +def test_credentials_transport_error(): + # It is an error to provide credentials and a transport instance. + transport = transports.DataTransferServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # It is an error to provide a credentials file and a transport instance. + transport = transports.DataTransferServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = DataTransferServiceClient( + client_options={"credentials_file": "credentials.json"}, + transport=transport, + ) + + # It is an error to provide an api_key and a transport instance. + transport = transports.DataTransferServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = DataTransferServiceClient( + 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 = DataTransferServiceClient( + client_options=options, credentials=ga_credentials.AnonymousCredentials() + ) + + # It is an error to provide scopes and a transport instance. + transport = transports.DataTransferServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = DataTransferServiceClient( + client_options={"scopes": ["1", "2"]}, + transport=transport, + ) + + +def test_transport_instance(): + # A client may be instantiated with a custom transport instance. + transport = transports.DataTransferServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + client = DataTransferServiceClient(transport=transport) + assert client.transport is transport + + +def test_transport_get_channel(): + # A client may be instantiated with a custom transport instance. + transport = transports.DataTransferServiceGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + transport = transports.DataTransferServiceGrpcAsyncIOTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.DataTransferServiceGrpcTransport, + transports.DataTransferServiceGrpcAsyncIOTransport, + transports.DataTransferServiceRestTransport, + ], +) +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 = DataTransferServiceClient.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 = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + assert isinstance( + client.transport, + transports.DataTransferServiceGrpcTransport, + ) + + +def test_data_transfer_service_base_transport_error(): + # Passing both a credentials object and credentials_file should raise an error + with pytest.raises(core_exceptions.DuplicateCredentialArgs): + transport = transports.DataTransferServiceTransport( + credentials=ga_credentials.AnonymousCredentials(), + credentials_file="credentials.json", + ) + + +def test_data_transfer_service_base_transport(): + # Instantiate the base transport. + with mock.patch( + "google.cloud.bigquery_datatransfer_v1.services.data_transfer_service.transports.DataTransferServiceTransport.__init__" + ) as Transport: + Transport.return_value = None + transport = transports.DataTransferServiceTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Every method on the transport should just blindly + # raise NotImplementedError. + methods = ( + "get_data_source", + "list_data_sources", + "create_transfer_config", + "update_transfer_config", + "delete_transfer_config", "get_transfer_config", "list_transfer_configs", "schedule_transfer_runs", @@ -5427,6 +9782,7 @@ def test_data_transfer_service_transport_auth_adc(transport_class): [ transports.DataTransferServiceGrpcTransport, transports.DataTransferServiceGrpcAsyncIOTransport, + transports.DataTransferServiceRestTransport, ], ) def test_data_transfer_service_transport_auth_gdch_credentials(transport_class): @@ -5526,11 +9882,23 @@ def test_data_transfer_service_grpc_transport_client_cert_source_for_mtls( ) +def test_data_transfer_service_http_transport_client_cert_source_for_mtls(): + cred = ga_credentials.AnonymousCredentials() + with mock.patch( + "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" + ) as mock_configure_mtls_channel: + transports.DataTransferServiceRestTransport( + credentials=cred, client_cert_source_for_mtls=client_cert_source_callback + ) + mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) + + @pytest.mark.parametrize( "transport_name", [ "grpc", "grpc_asyncio", + "rest", ], ) def test_data_transfer_service_host_no_port(transport_name): @@ -5541,7 +9909,11 @@ def test_data_transfer_service_host_no_port(transport_name): ), transport=transport_name, ) - assert client.transport._host == ("bigquerydatatransfer.googleapis.com:443") + assert client.transport._host == ( + "bigquerydatatransfer.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://bigquerydatatransfer.googleapis.com" + ) @pytest.mark.parametrize( @@ -5549,6 +9921,7 @@ def test_data_transfer_service_host_no_port(transport_name): [ "grpc", "grpc_asyncio", + "rest", ], ) def test_data_transfer_service_host_with_port(transport_name): @@ -5559,7 +9932,75 @@ def test_data_transfer_service_host_with_port(transport_name): ), transport=transport_name, ) - assert client.transport._host == ("bigquerydatatransfer.googleapis.com:8000") + assert client.transport._host == ( + "bigquerydatatransfer.googleapis.com:8000" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://bigquerydatatransfer.googleapis.com:8000" + ) + + +@pytest.mark.parametrize( + "transport_name", + [ + "rest", + ], +) +def test_data_transfer_service_client_transport_session_collision(transport_name): + creds1 = ga_credentials.AnonymousCredentials() + creds2 = ga_credentials.AnonymousCredentials() + client1 = DataTransferServiceClient( + credentials=creds1, + transport=transport_name, + ) + client2 = DataTransferServiceClient( + credentials=creds2, + transport=transport_name, + ) + session1 = client1.transport.get_data_source._session + session2 = client2.transport.get_data_source._session + assert session1 != session2 + session1 = client1.transport.list_data_sources._session + session2 = client2.transport.list_data_sources._session + assert session1 != session2 + session1 = client1.transport.create_transfer_config._session + session2 = client2.transport.create_transfer_config._session + assert session1 != session2 + session1 = client1.transport.update_transfer_config._session + session2 = client2.transport.update_transfer_config._session + assert session1 != session2 + session1 = client1.transport.delete_transfer_config._session + session2 = client2.transport.delete_transfer_config._session + assert session1 != session2 + session1 = client1.transport.get_transfer_config._session + session2 = client2.transport.get_transfer_config._session + assert session1 != session2 + session1 = client1.transport.list_transfer_configs._session + session2 = client2.transport.list_transfer_configs._session + assert session1 != session2 + session1 = client1.transport.schedule_transfer_runs._session + session2 = client2.transport.schedule_transfer_runs._session + assert session1 != session2 + session1 = client1.transport.start_manual_transfer_runs._session + session2 = client2.transport.start_manual_transfer_runs._session + assert session1 != session2 + session1 = client1.transport.get_transfer_run._session + session2 = client2.transport.get_transfer_run._session + assert session1 != session2 + session1 = client1.transport.delete_transfer_run._session + session2 = client2.transport.delete_transfer_run._session + assert session1 != session2 + session1 = client1.transport.list_transfer_runs._session + session2 = client2.transport.list_transfer_runs._session + assert session1 != session2 + session1 = client1.transport.list_transfer_logs._session + session2 = client2.transport.list_transfer_logs._session + assert session1 != session2 + session1 = client1.transport.check_valid_creds._session + session2 = client2.transport.check_valid_creds._session + assert session1 != session2 + session1 = client1.transport.enroll_data_sources._session + session2 = client2.transport.enroll_data_sources._session + assert session1 != session2 def test_data_transfer_service_grpc_transport_channel(): @@ -5900,6 +10341,120 @@ async def test_transport_close_async(): close.assert_called_once() +def test_get_location_rest_bad_request( + transport: str = "rest", request_type=locations_pb2.GetLocationRequest +): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2"}, request + ) + + # 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_location(request) + + +@pytest.mark.parametrize( + "request_type", + [ + locations_pb2.GetLocationRequest, + dict, + ], +) +def test_get_location_rest(request_type): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request_init = {"name": "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 = locations_pb2.Location() + + # 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.get_location(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) + + +def test_list_locations_rest_bad_request( + transport: str = "rest", request_type=locations_pb2.ListLocationsRequest +): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + request = request_type() + request = json_format.ParseDict({"name": "projects/sample1"}, request) + + # 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_locations(request) + + +@pytest.mark.parametrize( + "request_type", + [ + locations_pb2.ListLocationsRequest, + dict, + ], +) +def test_list_locations_rest(request_type): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request_init = {"name": "projects/sample1"} + 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 = locations_pb2.ListLocationsResponse() + + # 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.list_locations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) + + def test_list_locations(transport: str = "grpc"): client = DataTransferServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -6192,6 +10747,7 @@ async def test_get_location_from_dict_async(): def test_transport_close(): transports = { + "rest": "_session", "grpc": "_grpc_channel", } @@ -6209,6 +10765,7 @@ def test_transport_close(): def test_client_ctx(): transports = [ + "rest", "grpc", ] for transport in transports: