From cba3dad55db8a00f95503b9c94d4ca21ed462a4f Mon Sep 17 00:00:00 2001 From: "gcf-owl-bot[bot]" <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Date: Fri, 8 Oct 2021 14:14:11 +0000 Subject: [PATCH] feat: add context manager support in client (#234) - [ ] Regenerate this pull request now. chore: fix docstring for first attribute of protos committer: @busunkim96 PiperOrigin-RevId: 401271153 Source-Link: https://github.com/googleapis/googleapis/commit/787f8c9a731f44e74a90b9847d48659ca9462d10 Source-Link: https://github.com/googleapis/googleapis-gen/commit/81decffe9fc72396a8153e756d1d67a6eecfd620 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9 --- .../data_transfer_service/async_client.py | 6 +++ .../services/data_transfer_service/client.py | 18 +++++-- .../data_transfer_service/transports/base.py | 9 ++++ .../data_transfer_service/transports/grpc.py | 3 ++ .../transports/grpc_asyncio.py | 3 ++ .../types/datatransfer.py | 12 +++++ .../types/transfer.py | 2 + .../test_data_transfer_service.py | 50 +++++++++++++++++++ 8 files changed, 99 insertions(+), 4 deletions(-) diff --git a/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/async_client.py b/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/async_client.py index fc73542b..4a9b81a5 100644 --- a/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/async_client.py +++ b/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/async_client.py @@ -1403,6 +1403,12 @@ async def check_valid_creds( # Done; return the response. return response + async def __aenter__(self): + return self + + async def __aexit__(self, exc_type, exc, tb): + await self.transport.close() + try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/client.py b/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/client.py index 3f86cb3a..04a617c4 100644 --- a/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/client.py +++ b/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/client.py @@ -388,10 +388,7 @@ def __init__( client_cert_source_for_mtls=client_cert_source_func, quota_project_id=client_options.quota_project_id, client_info=client_info, - always_use_jwt_access=( - Transport == type(self).get_transport_class("grpc") - or Transport == type(self).get_transport_class("grpc_asyncio") - ), + always_use_jwt_access=True, ) def get_data_source( @@ -1519,6 +1516,19 @@ def check_valid_creds( # Done; return the response. return response + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + """Releases underlying transport's resources. + + .. warning:: + ONLY use as a context manager if the transport is NOT shared + with other clients! Exiting the with block will CLOSE the transport + and may cause errors in other clients! + """ + self.transport.close() + try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/transports/base.py b/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/transports/base.py index b74eab8e..1e9a77f3 100644 --- a/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/transports/base.py +++ b/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/transports/base.py @@ -328,6 +328,15 @@ def _prep_wrapped_messages(self, client_info): ), } + def close(self): + """Closes resources associated with the transport. + + .. warning:: + Only call this method if the transport is NOT shared + with other clients - this may cause errors in other clients! + """ + raise NotImplementedError() + @property def get_data_source( self, diff --git a/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/transports/grpc.py b/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/transports/grpc.py index fb8ae0b5..a18df8a2 100644 --- a/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/transports/grpc.py +++ b/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/transports/grpc.py @@ -631,5 +631,8 @@ def check_valid_creds( ) return self._stubs["check_valid_creds"] + def close(self): + self.grpc_channel.close() + __all__ = ("DataTransferServiceGrpcTransport",) diff --git a/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/transports/grpc_asyncio.py b/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/transports/grpc_asyncio.py index 65a51fba..fdff9e94 100644 --- a/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/transports/grpc_asyncio.py +++ b/google/cloud/bigquery_datatransfer_v1/services/data_transfer_service/transports/grpc_asyncio.py @@ -650,5 +650,8 @@ def check_valid_creds( ) return self._stubs["check_valid_creds"] + def close(self): + return self.grpc_channel.close() + __all__ = ("DataTransferServiceGrpcAsyncIOTransport",) diff --git a/google/cloud/bigquery_datatransfer_v1/types/datatransfer.py b/google/cloud/bigquery_datatransfer_v1/types/datatransfer.py index 0e043788..656fd0a5 100644 --- a/google/cloud/bigquery_datatransfer_v1/types/datatransfer.py +++ b/google/cloud/bigquery_datatransfer_v1/types/datatransfer.py @@ -236,6 +236,7 @@ class DataRefreshType(proto.Enum): class GetDataSourceRequest(proto.Message): r"""A request to get data source info. + Attributes: name (str): Required. The field will contain name of the resource @@ -275,6 +276,7 @@ class ListDataSourcesRequest(proto.Message): class ListDataSourcesResponse(proto.Message): r"""Returns list of supported data sources and their metadata. + Attributes: data_sources (Sequence[google.cloud.bigquery_datatransfer_v1.types.DataSource]): List of supported data sources and their @@ -419,6 +421,7 @@ class UpdateTransferConfigRequest(proto.Message): class GetTransferConfigRequest(proto.Message): r"""A request to get data transfer information. + Attributes: name (str): Required. The field will contain name of the resource @@ -447,6 +450,7 @@ class DeleteTransferConfigRequest(proto.Message): class GetTransferRunRequest(proto.Message): r"""A request to get data transfer run information. + Attributes: name (str): Required. The field will contain name of the resource @@ -461,6 +465,7 @@ class GetTransferRunRequest(proto.Message): class DeleteTransferRunRequest(proto.Message): r"""A request to delete data transfer run information. + Attributes: name (str): Required. The field will contain name of the resource @@ -504,6 +509,7 @@ class ListTransferConfigsRequest(proto.Message): class ListTransferConfigsResponse(proto.Message): r"""The returned list of pipelines in the project. + Attributes: transfer_configs (Sequence[google.cloud.bigquery_datatransfer_v1.types.TransferConfig]): Output only. The stored pipeline transfer @@ -568,6 +574,7 @@ class RunAttempt(proto.Enum): class ListTransferRunsResponse(proto.Message): r"""The returned list of pipelines in the project. + Attributes: transfer_runs (Sequence[google.cloud.bigquery_datatransfer_v1.types.TransferRun]): Output only. The stored pipeline transfer @@ -624,6 +631,7 @@ class ListTransferLogsRequest(proto.Message): class ListTransferLogsResponse(proto.Message): r"""The returned list transfer run messages. + Attributes: transfer_messages (Sequence[google.cloud.bigquery_datatransfer_v1.types.TransferMessage]): Output only. The stored pipeline transfer @@ -678,6 +686,7 @@ class CheckValidCredsResponse(proto.Message): class ScheduleTransferRunsRequest(proto.Message): r"""A request to schedule transfer runs for a time range. + Attributes: parent (str): Required. Transfer configuration name in the form: @@ -698,6 +707,7 @@ class ScheduleTransferRunsRequest(proto.Message): class ScheduleTransferRunsResponse(proto.Message): r"""A response to schedule transfer runs for a time range. + Attributes: runs (Sequence[google.cloud.bigquery_datatransfer_v1.types.TransferRun]): The transfer runs that were scheduled. @@ -708,6 +718,7 @@ class ScheduleTransferRunsResponse(proto.Message): class StartManualTransferRunsRequest(proto.Message): r"""A request to start manual transfer runs. + Attributes: parent (str): Transfer configuration name in the form: @@ -759,6 +770,7 @@ class TimeRange(proto.Message): class StartManualTransferRunsResponse(proto.Message): r"""A response to start manual transfer runs. + Attributes: runs (Sequence[google.cloud.bigquery_datatransfer_v1.types.TransferRun]): The transfer runs that were created. diff --git a/google/cloud/bigquery_datatransfer_v1/types/transfer.py b/google/cloud/bigquery_datatransfer_v1/types/transfer.py index 76a5856f..2feaf5ae 100644 --- a/google/cloud/bigquery_datatransfer_v1/types/transfer.py +++ b/google/cloud/bigquery_datatransfer_v1/types/transfer.py @@ -67,6 +67,7 @@ class EmailPreferences(proto.Message): class ScheduleOptions(proto.Message): r"""Options customizing the data transfer schedule. + Attributes: disable_auto_scheduling (bool): If true, automatic scheduling of data @@ -205,6 +206,7 @@ class TransferConfig(proto.Message): class TransferRun(proto.Message): r"""Represents a data transfer run. + Attributes: name (str): The resource name of the transfer run. Transfer run names diff --git a/tests/unit/gapic/bigquery_datatransfer_v1/test_data_transfer_service.py b/tests/unit/gapic/bigquery_datatransfer_v1/test_data_transfer_service.py index 6328d1f8..8fa41e57 100644 --- a/tests/unit/gapic/bigquery_datatransfer_v1/test_data_transfer_service.py +++ b/tests/unit/gapic/bigquery_datatransfer_v1/test_data_transfer_service.py @@ -29,6 +29,7 @@ from google.api_core import gapic_v1 from google.api_core import grpc_helpers from google.api_core import grpc_helpers_async +from google.api_core import path_template from google.auth import credentials as ga_credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.bigquery_datatransfer_v1.services.data_transfer_service import ( @@ -4589,6 +4590,9 @@ def test_data_transfer_service_base_transport(): with pytest.raises(NotImplementedError): getattr(transport, method)(request=object()) + with pytest.raises(NotImplementedError): + transport.close() + @requires_google_auth_gte_1_25_0 def test_data_transfer_service_base_transport_with_credentials_file(): @@ -5117,3 +5121,49 @@ def test_client_withDEFAULT_CLIENT_INFO(): credentials=ga_credentials.AnonymousCredentials(), client_info=client_info, ) prep.assert_called_once_with(client_info) + + +@pytest.mark.asyncio +async def test_transport_close_async(): + client = DataTransferServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc_asyncio", + ) + with mock.patch.object( + type(getattr(client.transport, "grpc_channel")), "close" + ) as close: + async with client: + close.assert_not_called() + close.assert_called_once() + + +def test_transport_close(): + transports = { + "grpc": "_grpc_channel", + } + + for transport, close_name in transports.items(): + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport=transport + ) + with mock.patch.object( + type(getattr(client.transport, close_name)), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + + +def test_client_ctx(): + transports = [ + "grpc", + ] + for transport in transports: + client = DataTransferServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport=transport + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "close") as close: + close.assert_not_called() + with client: + pass + close.assert_called()