From 0f631cc8bd8ff0928d5403e756d2206f1842c35c 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:22:25 +0000 Subject: [PATCH] feat: add context manager support in client (#88) - [ ] 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 --- .../artifact_registry/async_client.py | 6 +++ .../services/artifact_registry/client.py | 18 +++++-- .../artifact_registry/transports/base.py | 9 ++++ .../artifact_registry/transports/grpc.py | 3 ++ .../transports/grpc_asyncio.py | 3 ++ .../artifact_registry/async_client.py | 6 +++ .../services/artifact_registry/client.py | 18 +++++-- .../artifact_registry/transports/base.py | 9 ++++ .../artifact_registry/transports/grpc.py | 3 ++ .../transports/grpc_asyncio.py | 3 ++ .../artifactregistry_v1beta2/types/service.py | 3 +- .../test_artifact_registry.py | 50 +++++++++++++++++++ .../test_artifact_registry.py | 50 +++++++++++++++++++ 13 files changed, 172 insertions(+), 9 deletions(-) diff --git a/google/cloud/artifactregistry_v1/services/artifact_registry/async_client.py b/google/cloud/artifactregistry_v1/services/artifact_registry/async_client.py index 6fe8cff..357bbf8 100644 --- a/google/cloud/artifactregistry_v1/services/artifact_registry/async_client.py +++ b/google/cloud/artifactregistry_v1/services/artifact_registry/async_client.py @@ -411,6 +411,12 @@ async def get_repository( # 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/artifactregistry_v1/services/artifact_registry/client.py b/google/cloud/artifactregistry_v1/services/artifact_registry/client.py index c90fdb6..ab51f6b 100644 --- a/google/cloud/artifactregistry_v1/services/artifact_registry/client.py +++ b/google/cloud/artifactregistry_v1/services/artifact_registry/client.py @@ -380,10 +380,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_docker_images( @@ -617,6 +614,19 @@ def get_repository( # 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/artifactregistry_v1/services/artifact_registry/transports/base.py b/google/cloud/artifactregistry_v1/services/artifact_registry/transports/base.py index 82b26eb..75ea1e4 100644 --- a/google/cloud/artifactregistry_v1/services/artifact_registry/transports/base.py +++ b/google/cloud/artifactregistry_v1/services/artifact_registry/transports/base.py @@ -169,6 +169,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 list_docker_images( self, diff --git a/google/cloud/artifactregistry_v1/services/artifact_registry/transports/grpc.py b/google/cloud/artifactregistry_v1/services/artifact_registry/transports/grpc.py index f86aaa9..727dec5 100644 --- a/google/cloud/artifactregistry_v1/services/artifact_registry/transports/grpc.py +++ b/google/cloud/artifactregistry_v1/services/artifact_registry/transports/grpc.py @@ -320,5 +320,8 @@ def get_repository( ) return self._stubs["get_repository"] + def close(self): + self.grpc_channel.close() + __all__ = ("ArtifactRegistryGrpcTransport",) diff --git a/google/cloud/artifactregistry_v1/services/artifact_registry/transports/grpc_asyncio.py b/google/cloud/artifactregistry_v1/services/artifact_registry/transports/grpc_asyncio.py index d7b4f71..adbc940 100644 --- a/google/cloud/artifactregistry_v1/services/artifact_registry/transports/grpc_asyncio.py +++ b/google/cloud/artifactregistry_v1/services/artifact_registry/transports/grpc_asyncio.py @@ -324,5 +324,8 @@ def get_repository( ) return self._stubs["get_repository"] + def close(self): + return self.grpc_channel.close() + __all__ = ("ArtifactRegistryGrpcAsyncIOTransport",) diff --git a/google/cloud/artifactregistry_v1beta2/services/artifact_registry/async_client.py b/google/cloud/artifactregistry_v1beta2/services/artifact_registry/async_client.py index af3a6a7..ac8db1b 100644 --- a/google/cloud/artifactregistry_v1beta2/services/artifact_registry/async_client.py +++ b/google/cloud/artifactregistry_v1beta2/services/artifact_registry/async_client.py @@ -2018,6 +2018,12 @@ async def test_iam_permissions( # 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/artifactregistry_v1beta2/services/artifact_registry/client.py b/google/cloud/artifactregistry_v1beta2/services/artifact_registry/client.py index 2470122..02cbfe7 100644 --- a/google/cloud/artifactregistry_v1beta2/services/artifact_registry/client.py +++ b/google/cloud/artifactregistry_v1beta2/services/artifact_registry/client.py @@ -387,10 +387,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_repositories( @@ -2083,6 +2080,19 @@ def test_iam_permissions( # 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/artifactregistry_v1beta2/services/artifact_registry/transports/base.py b/google/cloud/artifactregistry_v1beta2/services/artifact_registry/transports/base.py index 9ffcd1d..4ca4c89 100644 --- a/google/cloud/artifactregistry_v1beta2/services/artifact_registry/transports/base.py +++ b/google/cloud/artifactregistry_v1beta2/services/artifact_registry/transports/base.py @@ -400,6 +400,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/artifactregistry_v1beta2/services/artifact_registry/transports/grpc.py b/google/cloud/artifactregistry_v1beta2/services/artifact_registry/transports/grpc.py index de44d13..41d82fe 100644 --- a/google/cloud/artifactregistry_v1beta2/services/artifact_registry/transports/grpc.py +++ b/google/cloud/artifactregistry_v1beta2/services/artifact_registry/transports/grpc.py @@ -806,5 +806,8 @@ def test_iam_permissions( ) return self._stubs["test_iam_permissions"] + def close(self): + self.grpc_channel.close() + __all__ = ("ArtifactRegistryGrpcTransport",) diff --git a/google/cloud/artifactregistry_v1beta2/services/artifact_registry/transports/grpc_asyncio.py b/google/cloud/artifactregistry_v1beta2/services/artifact_registry/transports/grpc_asyncio.py index e389c4a..936bd40 100644 --- a/google/cloud/artifactregistry_v1beta2/services/artifact_registry/transports/grpc_asyncio.py +++ b/google/cloud/artifactregistry_v1beta2/services/artifact_registry/transports/grpc_asyncio.py @@ -836,5 +836,8 @@ def test_iam_permissions( ) return self._stubs["test_iam_permissions"] + def close(self): + return self.grpc_channel.close() + __all__ = ("ArtifactRegistryGrpcAsyncIOTransport",) diff --git a/google/cloud/artifactregistry_v1beta2/types/service.py b/google/cloud/artifactregistry_v1beta2/types/service.py index 439dc9c..3ec7994 100644 --- a/google/cloud/artifactregistry_v1beta2/types/service.py +++ b/google/cloud/artifactregistry_v1beta2/types/service.py @@ -22,7 +22,8 @@ class OperationMetadata(proto.Message): - r"""Metadata type for longrunning-operations, currently empty. """ + r"""Metadata type for longrunning-operations, currently empty. + """ __all__ = tuple(sorted(__protobuf__.manifest)) diff --git a/tests/unit/gapic/artifactregistry_v1/test_artifact_registry.py b/tests/unit/gapic/artifactregistry_v1/test_artifact_registry.py index 9ef2cc1..cb62d65 100644 --- a/tests/unit/gapic/artifactregistry_v1/test_artifact_registry.py +++ b/tests/unit/gapic/artifactregistry_v1/test_artifact_registry.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.artifactregistry_v1.services.artifact_registry import ( @@ -1580,6 +1581,9 @@ def test_artifact_registry_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_artifact_registry_base_transport_with_credentials_file(): @@ -2114,3 +2118,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 = ArtifactRegistryAsyncClient( + 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 = ArtifactRegistryClient( + 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 = ArtifactRegistryClient( + 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/artifactregistry_v1beta2/test_artifact_registry.py b/tests/unit/gapic/artifactregistry_v1beta2/test_artifact_registry.py index 2ff4f3e..17e14b8 100644 --- a/tests/unit/gapic/artifactregistry_v1beta2/test_artifact_registry.py +++ b/tests/unit/gapic/artifactregistry_v1beta2/test_artifact_registry.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.artifactregistry_v1beta2.services.artifact_registry import ( @@ -5497,6 +5498,9 @@ def test_artifact_registry_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): @@ -6057,3 +6061,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 = ArtifactRegistryAsyncClient( + 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 = ArtifactRegistryClient( + 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 = ArtifactRegistryClient( + 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()