From 4324911a80baaaa96065e735631bd6c446075f5c Mon Sep 17 00:00:00 2001 From: "gcf-owl-bot[bot]" <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Date: Thu, 7 Oct 2021 00:34:10 +0000 Subject: [PATCH] feat: add context manager support in client (#123) - [ ] 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 --- .../services/cloud_redis/async_client.py | 6 +++ .../redis_v1/services/cloud_redis/client.py | 18 +++++-- .../services/cloud_redis/transports/base.py | 9 ++++ .../services/cloud_redis/transports/grpc.py | 3 ++ .../cloud_redis/transports/grpc_asyncio.py | 3 ++ google/cloud/redis_v1/types/cloud_redis.py | 9 +++- .../services/cloud_redis/async_client.py | 6 +++ .../services/cloud_redis/client.py | 18 +++++-- .../services/cloud_redis/transports/base.py | 9 ++++ .../services/cloud_redis/transports/grpc.py | 3 ++ .../cloud_redis/transports/grpc_asyncio.py | 3 ++ .../cloud/redis_v1beta1/types/cloud_redis.py | 8 ++- tests/unit/gapic/redis_v1/test_cloud_redis.py | 50 +++++++++++++++++++ .../gapic/redis_v1beta1/test_cloud_redis.py | 50 +++++++++++++++++++ 14 files changed, 185 insertions(+), 10 deletions(-) diff --git a/google/cloud/redis_v1/services/cloud_redis/async_client.py b/google/cloud/redis_v1/services/cloud_redis/async_client.py index 28839c0..f0c9bee 100644 --- a/google/cloud/redis_v1/services/cloud_redis/async_client.py +++ b/google/cloud/redis_v1/services/cloud_redis/async_client.py @@ -1052,6 +1052,12 @@ async def delete_instance( # 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/redis_v1/services/cloud_redis/client.py b/google/cloud/redis_v1/services/cloud_redis/client.py index 11e822c..b29a9d9 100644 --- a/google/cloud/redis_v1/services/cloud_redis/client.py +++ b/google/cloud/redis_v1/services/cloud_redis/client.py @@ -368,10 +368,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 list_instances( @@ -1245,6 +1242,19 @@ def delete_instance( # 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/redis_v1/services/cloud_redis/transports/base.py b/google/cloud/redis_v1/services/cloud_redis/transports/base.py index eef4bfb..12ff338 100644 --- a/google/cloud/redis_v1/services/cloud_redis/transports/base.py +++ b/google/cloud/redis_v1/services/cloud_redis/transports/base.py @@ -183,6 +183,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 operations_client(self) -> operations_v1.OperationsClient: """Return the client designed to process long-running operations.""" diff --git a/google/cloud/redis_v1/services/cloud_redis/transports/grpc.py b/google/cloud/redis_v1/services/cloud_redis/transports/grpc.py index 3f2f306..d7c7723 100644 --- a/google/cloud/redis_v1/services/cloud_redis/transports/grpc.py +++ b/google/cloud/redis_v1/services/cloud_redis/transports/grpc.py @@ -541,5 +541,8 @@ def delete_instance( ) return self._stubs["delete_instance"] + def close(self): + self.grpc_channel.close() + __all__ = ("CloudRedisGrpcTransport",) diff --git a/google/cloud/redis_v1/services/cloud_redis/transports/grpc_asyncio.py b/google/cloud/redis_v1/services/cloud_redis/transports/grpc_asyncio.py index 5476412..40a2aad 100644 --- a/google/cloud/redis_v1/services/cloud_redis/transports/grpc_asyncio.py +++ b/google/cloud/redis_v1/services/cloud_redis/transports/grpc_asyncio.py @@ -560,5 +560,8 @@ def delete_instance( ) return self._stubs["delete_instance"] + def close(self): + return self.grpc_channel.close() + __all__ = ("CloudRedisGrpcAsyncIOTransport",) diff --git a/google/cloud/redis_v1/types/cloud_redis.py b/google/cloud/redis_v1/types/cloud_redis.py index 2da8a95..5b9a150 100644 --- a/google/cloud/redis_v1/types/cloud_redis.py +++ b/google/cloud/redis_v1/types/cloud_redis.py @@ -46,6 +46,7 @@ class Instance(proto.Message): r"""A Google Cloud Redis instance. + Attributes: name (str): Required. Unique name of the resource in this scope @@ -377,6 +378,7 @@ class DeleteInstanceRequest(proto.Message): class GcsSource(proto.Message): r"""The Cloud Storage location for the input content + Attributes: uri (str): Required. Source data URI. (e.g. @@ -388,6 +390,7 @@ class GcsSource(proto.Message): class InputConfig(proto.Message): r"""The input content + Attributes: gcs_source (google.cloud.redis_v1.types.GcsSource): Google Cloud Storage location where input @@ -418,6 +421,7 @@ class ImportInstanceRequest(proto.Message): class GcsDestination(proto.Message): r"""The Cloud Storage location for the output content + Attributes: uri (str): Required. Data destination URI (e.g. @@ -430,6 +434,7 @@ class GcsDestination(proto.Message): class OutputConfig(proto.Message): r"""The output content + Attributes: gcs_destination (google.cloud.redis_v1.types.GcsDestination): Google Cloud Storage destination for output @@ -487,6 +492,7 @@ class DataProtectionMode(proto.Enum): class OperationMetadata(proto.Message): r"""Represents the v1 metadata of the long-running operation. + Attributes: create_time (google.protobuf.timestamp_pb2.Timestamp): Creation timestamp. @@ -537,7 +543,8 @@ class LocationMetadata(proto.Message): class ZoneMetadata(proto.Message): r"""Defines specific information for a particular zone. Currently empty and reserved for future use only. - """ + + """ __all__ = tuple(sorted(__protobuf__.manifest)) diff --git a/google/cloud/redis_v1beta1/services/cloud_redis/async_client.py b/google/cloud/redis_v1beta1/services/cloud_redis/async_client.py index bd4ace9..f0e9c83 100644 --- a/google/cloud/redis_v1beta1/services/cloud_redis/async_client.py +++ b/google/cloud/redis_v1beta1/services/cloud_redis/async_client.py @@ -1053,6 +1053,12 @@ async def delete_instance( # 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/redis_v1beta1/services/cloud_redis/client.py b/google/cloud/redis_v1beta1/services/cloud_redis/client.py index ea7f431..5b3bf14 100644 --- a/google/cloud/redis_v1beta1/services/cloud_redis/client.py +++ b/google/cloud/redis_v1beta1/services/cloud_redis/client.py @@ -369,10 +369,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 list_instances( @@ -1246,6 +1243,19 @@ def delete_instance( # 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/redis_v1beta1/services/cloud_redis/transports/base.py b/google/cloud/redis_v1beta1/services/cloud_redis/transports/base.py index 9f93444..4cdbe33 100644 --- a/google/cloud/redis_v1beta1/services/cloud_redis/transports/base.py +++ b/google/cloud/redis_v1beta1/services/cloud_redis/transports/base.py @@ -183,6 +183,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 operations_client(self) -> operations_v1.OperationsClient: """Return the client designed to process long-running operations.""" diff --git a/google/cloud/redis_v1beta1/services/cloud_redis/transports/grpc.py b/google/cloud/redis_v1beta1/services/cloud_redis/transports/grpc.py index 69e54ca..0647ac3 100644 --- a/google/cloud/redis_v1beta1/services/cloud_redis/transports/grpc.py +++ b/google/cloud/redis_v1beta1/services/cloud_redis/transports/grpc.py @@ -541,5 +541,8 @@ def delete_instance( ) return self._stubs["delete_instance"] + def close(self): + self.grpc_channel.close() + __all__ = ("CloudRedisGrpcTransport",) diff --git a/google/cloud/redis_v1beta1/services/cloud_redis/transports/grpc_asyncio.py b/google/cloud/redis_v1beta1/services/cloud_redis/transports/grpc_asyncio.py index 44521b1..d61eb01 100644 --- a/google/cloud/redis_v1beta1/services/cloud_redis/transports/grpc_asyncio.py +++ b/google/cloud/redis_v1beta1/services/cloud_redis/transports/grpc_asyncio.py @@ -560,5 +560,8 @@ def delete_instance( ) return self._stubs["delete_instance"] + def close(self): + return self.grpc_channel.close() + __all__ = ("CloudRedisGrpcAsyncIOTransport",) diff --git a/google/cloud/redis_v1beta1/types/cloud_redis.py b/google/cloud/redis_v1beta1/types/cloud_redis.py index 78e7f32..3ded644 100644 --- a/google/cloud/redis_v1beta1/types/cloud_redis.py +++ b/google/cloud/redis_v1beta1/types/cloud_redis.py @@ -45,6 +45,7 @@ class Instance(proto.Message): r"""A Google Cloud Redis instance. + Attributes: name (str): Required. Unique name of the resource in this scope @@ -378,6 +379,7 @@ class DeleteInstanceRequest(proto.Message): class GcsSource(proto.Message): r"""The Cloud Storage location for the input content + Attributes: uri (str): Required. Source data URI. (e.g. @@ -389,6 +391,7 @@ class GcsSource(proto.Message): class InputConfig(proto.Message): r"""The input content + Attributes: gcs_source (google.cloud.redis_v1beta1.types.GcsSource): Google Cloud Storage location where input @@ -419,6 +422,7 @@ class ImportInstanceRequest(proto.Message): class GcsDestination(proto.Message): r"""The Cloud Storage location for the output content + Attributes: uri (str): Required. Data destination URI (e.g. @@ -431,6 +435,7 @@ class GcsDestination(proto.Message): class OutputConfig(proto.Message): r"""The output content + Attributes: gcs_destination (google.cloud.redis_v1beta1.types.GcsDestination): Google Cloud Storage destination for output @@ -509,7 +514,8 @@ class LocationMetadata(proto.Message): class ZoneMetadata(proto.Message): r"""Defines specific information for a particular zone. Currently empty and reserved for future use only. - """ + + """ __all__ = tuple(sorted(__protobuf__.manifest)) diff --git a/tests/unit/gapic/redis_v1/test_cloud_redis.py b/tests/unit/gapic/redis_v1/test_cloud_redis.py index 80e4c36..9aca381 100644 --- a/tests/unit/gapic/redis_v1/test_cloud_redis.py +++ b/tests/unit/gapic/redis_v1/test_cloud_redis.py @@ -32,6 +32,7 @@ from google.api_core import grpc_helpers_async from google.api_core import operation_async # type: ignore from google.api_core import operations_v1 +from google.api_core import path_template from google.auth import credentials as ga_credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.redis_v1.services.cloud_redis import CloudRedisAsyncClient @@ -2678,6 +2679,9 @@ def test_cloud_redis_base_transport(): with pytest.raises(NotImplementedError): getattr(transport, method)(request=object()) + with pytest.raises(NotImplementedError): + transport.close() + # Additionally, the LRO client (a property) should # also raise NotImplementedError with pytest.raises(NotImplementedError): @@ -3174,3 +3178,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 = CloudRedisAsyncClient( + 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 = CloudRedisClient( + 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 = CloudRedisClient( + 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() diff --git a/tests/unit/gapic/redis_v1beta1/test_cloud_redis.py b/tests/unit/gapic/redis_v1beta1/test_cloud_redis.py index 44a6247..0cfd13c 100644 --- a/tests/unit/gapic/redis_v1beta1/test_cloud_redis.py +++ b/tests/unit/gapic/redis_v1beta1/test_cloud_redis.py @@ -32,6 +32,7 @@ from google.api_core import grpc_helpers_async from google.api_core import operation_async # type: ignore from google.api_core import operations_v1 +from google.api_core import path_template from google.auth import credentials as ga_credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.redis_v1beta1.services.cloud_redis import CloudRedisAsyncClient @@ -2679,6 +2680,9 @@ def test_cloud_redis_base_transport(): with pytest.raises(NotImplementedError): getattr(transport, method)(request=object()) + with pytest.raises(NotImplementedError): + transport.close() + # Additionally, the LRO client (a property) should # also raise NotImplementedError with pytest.raises(NotImplementedError): @@ -3175,3 +3179,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 = CloudRedisAsyncClient( + 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 = CloudRedisClient( + 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 = CloudRedisClient( + 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()