From 6ce5227eec47542014817ce912e64c39e6723676 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 10:12:11 +0000 Subject: [PATCH] feat: add context manager support in client (#51) - [ ] 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/private_catalog/async_client.py | 6 +++ .../services/private_catalog/client.py | 18 +++++-- .../private_catalog/transports/base.py | 9 ++++ .../private_catalog/transports/grpc.py | 3 ++ .../transports/grpc_asyncio.py | 3 ++ .../types/private_catalog.py | 4 ++ .../test_private_catalog.py | 50 +++++++++++++++++++ 7 files changed, 89 insertions(+), 4 deletions(-) diff --git a/google/cloud/privatecatalog_v1beta1/services/private_catalog/async_client.py b/google/cloud/privatecatalog_v1beta1/services/private_catalog/async_client.py index 3010ff6..42c060a 100644 --- a/google/cloud/privatecatalog_v1beta1/services/private_catalog/async_client.py +++ b/google/cloud/privatecatalog_v1beta1/services/private_catalog/async_client.py @@ -367,6 +367,12 @@ async def search_versions( # 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/privatecatalog_v1beta1/services/private_catalog/client.py b/google/cloud/privatecatalog_v1beta1/services/private_catalog/client.py index 753c980..63b4e16 100644 --- a/google/cloud/privatecatalog_v1beta1/services/private_catalog/client.py +++ b/google/cloud/privatecatalog_v1beta1/services/private_catalog/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 search_catalogs( @@ -577,6 +574,19 @@ def search_versions( # 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/privatecatalog_v1beta1/services/private_catalog/transports/base.py b/google/cloud/privatecatalog_v1beta1/services/private_catalog/transports/base.py index 6d6a48e..fba4aed 100644 --- a/google/cloud/privatecatalog_v1beta1/services/private_catalog/transports/base.py +++ b/google/cloud/privatecatalog_v1beta1/services/private_catalog/transports/base.py @@ -165,6 +165,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 search_catalogs( self, diff --git a/google/cloud/privatecatalog_v1beta1/services/private_catalog/transports/grpc.py b/google/cloud/privatecatalog_v1beta1/services/private_catalog/transports/grpc.py index bc8ca55..597ae95 100644 --- a/google/cloud/privatecatalog_v1beta1/services/private_catalog/transports/grpc.py +++ b/google/cloud/privatecatalog_v1beta1/services/private_catalog/transports/grpc.py @@ -336,5 +336,8 @@ def search_versions( ) return self._stubs["search_versions"] + def close(self): + self.grpc_channel.close() + __all__ = ("PrivateCatalogGrpcTransport",) diff --git a/google/cloud/privatecatalog_v1beta1/services/private_catalog/transports/grpc_asyncio.py b/google/cloud/privatecatalog_v1beta1/services/private_catalog/transports/grpc_asyncio.py index 7b72996..ba41343 100644 --- a/google/cloud/privatecatalog_v1beta1/services/private_catalog/transports/grpc_asyncio.py +++ b/google/cloud/privatecatalog_v1beta1/services/private_catalog/transports/grpc_asyncio.py @@ -342,5 +342,8 @@ def search_versions( ) return self._stubs["search_versions"] + def close(self): + return self.grpc_channel.close() + __all__ = ("PrivateCatalogGrpcAsyncIOTransport",) diff --git a/google/cloud/privatecatalog_v1beta1/types/private_catalog.py b/google/cloud/privatecatalog_v1beta1/types/private_catalog.py index 0e453dd..ff8daa1 100644 --- a/google/cloud/privatecatalog_v1beta1/types/private_catalog.py +++ b/google/cloud/privatecatalog_v1beta1/types/private_catalog.py @@ -435,6 +435,7 @@ class Product(proto.Message): class AssetReference(proto.Message): r"""Defines the reference of an asset belonging to a product. + Attributes: id (str): Output only. A unique identifier among asset @@ -502,6 +503,7 @@ class AssetValidationState(proto.Enum): class Inputs(proto.Message): r"""Defines definition of input parameters of asset templates. + Attributes: parameters (google.protobuf.struct_pb2.Struct): Output only. The JSON schema defining the @@ -513,6 +515,7 @@ class Inputs(proto.Message): class GcsSource(proto.Message): r"""Defines how to access Cloud Storage source. + Attributes: gcs_path (str): Output only. the cloud storage object path. @@ -532,6 +535,7 @@ class GcsSource(proto.Message): class GitSource(proto.Message): r"""Defines how to access a Git Source. + Attributes: repo (str): Location of the Git repo to build. diff --git a/tests/unit/gapic/privatecatalog_v1beta1/test_private_catalog.py b/tests/unit/gapic/privatecatalog_v1beta1/test_private_catalog.py index 6b457d3..62ac324 100644 --- a/tests/unit/gapic/privatecatalog_v1beta1/test_private_catalog.py +++ b/tests/unit/gapic/privatecatalog_v1beta1/test_private_catalog.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.privatecatalog_v1beta1.services.private_catalog import ( @@ -1431,6 +1432,9 @@ def test_private_catalog_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_private_catalog_base_transport_with_credentials_file(): @@ -1949,3 +1953,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 = PrivateCatalogAsyncClient( + 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 = PrivateCatalogClient( + 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 = PrivateCatalogClient( + 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()