diff --git a/google/cloud/apigeeconnect_v1/services/connection_service/async_client.py b/google/cloud/apigeeconnect_v1/services/connection_service/async_client.py index 9469611..7509b06 100644 --- a/google/cloud/apigeeconnect_v1/services/connection_service/async_client.py +++ b/google/cloud/apigeeconnect_v1/services/connection_service/async_client.py @@ -253,6 +253,12 @@ async def list_connections( # 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/apigeeconnect_v1/services/connection_service/client.py b/google/cloud/apigeeconnect_v1/services/connection_service/client.py index 86dc687..73ec1c8 100644 --- a/google/cloud/apigeeconnect_v1/services/connection_service/client.py +++ b/google/cloud/apigeeconnect_v1/services/connection_service/client.py @@ -345,10 +345,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_connections( @@ -432,6 +429,19 @@ def list_connections( # 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/apigeeconnect_v1/services/connection_service/transports/base.py b/google/cloud/apigeeconnect_v1/services/connection_service/transports/base.py index 4f299a0..d97e737 100644 --- a/google/cloud/apigeeconnect_v1/services/connection_service/transports/base.py +++ b/google/cloud/apigeeconnect_v1/services/connection_service/transports/base.py @@ -170,6 +170,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_connections( self, diff --git a/google/cloud/apigeeconnect_v1/services/connection_service/transports/grpc.py b/google/cloud/apigeeconnect_v1/services/connection_service/transports/grpc.py index 2f7bd41..ac9afe8 100644 --- a/google/cloud/apigeeconnect_v1/services/connection_service/transports/grpc.py +++ b/google/cloud/apigeeconnect_v1/services/connection_service/transports/grpc.py @@ -255,5 +255,8 @@ def list_connections( ) return self._stubs["list_connections"] + def close(self): + self.grpc_channel.close() + __all__ = ("ConnectionServiceGrpcTransport",) diff --git a/google/cloud/apigeeconnect_v1/services/connection_service/transports/grpc_asyncio.py b/google/cloud/apigeeconnect_v1/services/connection_service/transports/grpc_asyncio.py index 0feca49..4bb8b5f 100644 --- a/google/cloud/apigeeconnect_v1/services/connection_service/transports/grpc_asyncio.py +++ b/google/cloud/apigeeconnect_v1/services/connection_service/transports/grpc_asyncio.py @@ -259,5 +259,8 @@ def list_connections( ) return self._stubs["list_connections"] + def close(self): + return self.grpc_channel.close() + __all__ = ("ConnectionServiceGrpcAsyncIOTransport",) diff --git a/google/cloud/apigeeconnect_v1/services/tether/async_client.py b/google/cloud/apigeeconnect_v1/services/tether/async_client.py index 42d732a..2d0edd2 100644 --- a/google/cloud/apigeeconnect_v1/services/tether/async_client.py +++ b/google/cloud/apigeeconnect_v1/services/tether/async_client.py @@ -208,6 +208,12 @@ def egress( # 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/apigeeconnect_v1/services/tether/client.py b/google/cloud/apigeeconnect_v1/services/tether/client.py index d2aeb89..5a98d9e 100644 --- a/google/cloud/apigeeconnect_v1/services/tether/client.py +++ b/google/cloud/apigeeconnect_v1/services/tether/client.py @@ -330,10 +330,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 egress( @@ -378,6 +375,19 @@ def egress( # 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/apigeeconnect_v1/services/tether/transports/base.py b/google/cloud/apigeeconnect_v1/services/tether/transports/base.py index 0afb887..65ff8e1 100644 --- a/google/cloud/apigeeconnect_v1/services/tether/transports/base.py +++ b/google/cloud/apigeeconnect_v1/services/tether/transports/base.py @@ -159,6 +159,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 egress( self, diff --git a/google/cloud/apigeeconnect_v1/services/tether/transports/grpc.py b/google/cloud/apigeeconnect_v1/services/tether/transports/grpc.py index 808e219..2fe1b3a 100644 --- a/google/cloud/apigeeconnect_v1/services/tether/transports/grpc.py +++ b/google/cloud/apigeeconnect_v1/services/tether/transports/grpc.py @@ -260,5 +260,8 @@ def egress(self) -> Callable[[tether.EgressResponse], tether.EgressRequest]: ) return self._stubs["egress"] + def close(self): + self.grpc_channel.close() + __all__ = ("TetherGrpcTransport",) diff --git a/google/cloud/apigeeconnect_v1/services/tether/transports/grpc_asyncio.py b/google/cloud/apigeeconnect_v1/services/tether/transports/grpc_asyncio.py index 73cf521..d453f47 100644 --- a/google/cloud/apigeeconnect_v1/services/tether/transports/grpc_asyncio.py +++ b/google/cloud/apigeeconnect_v1/services/tether/transports/grpc_asyncio.py @@ -265,5 +265,8 @@ def egress( ) return self._stubs["egress"] + def close(self): + return self.grpc_channel.close() + __all__ = ("TetherGrpcAsyncIOTransport",) diff --git a/google/cloud/apigeeconnect_v1/types/connection.py b/google/cloud/apigeeconnect_v1/types/connection.py index da9019b..2b7c009 100644 --- a/google/cloud/apigeeconnect_v1/types/connection.py +++ b/google/cloud/apigeeconnect_v1/types/connection.py @@ -29,6 +29,7 @@ class ListConnectionsRequest(proto.Message): r"""The request for [ListConnections][Management.ListConnections]. + Attributes: parent (str): Required. Parent name of the form: @@ -55,6 +56,7 @@ class ListConnectionsRequest(proto.Message): class ListConnectionsResponse(proto.Message): r"""The response for [ListConnections][Management.ListConnections]. + Attributes: connections (Sequence[google.cloud.apigeeconnect_v1.types.Connection]): A list of clients. @@ -74,6 +76,7 @@ def raw_page(self): class Connection(proto.Message): r""" + Attributes: endpoint (str): The endpoint that the connection is made against. Format: @@ -91,6 +94,7 @@ class Connection(proto.Message): class Cluster(proto.Message): r""" + Attributes: name (str): The name of the cluster. diff --git a/google/cloud/apigeeconnect_v1/types/tether.py b/google/cloud/apigeeconnect_v1/types/tether.py index 26eb01a..1c4d7bc 100644 --- a/google/cloud/apigeeconnect_v1/types/tether.py +++ b/google/cloud/apigeeconnect_v1/types/tether.py @@ -59,6 +59,7 @@ class Scheme(proto.Enum): class EgressRequest(proto.Message): r"""gRPC request payload for tether. + Attributes: id (str): Unique identifier for the request. @@ -85,6 +86,7 @@ class EgressRequest(proto.Message): class Payload(proto.Message): r"""Payload for EgressRequest. + Attributes: http_request (google.cloud.apigeeconnect_v1.types.HttpRequest): The HttpRequest proto. @@ -105,6 +107,7 @@ class Payload(proto.Message): class StreamInfo(proto.Message): r"""The Information of bi-directional stream. + Attributes: id (str): Unique identifier for the stream. @@ -115,6 +118,7 @@ class StreamInfo(proto.Message): class EgressResponse(proto.Message): r"""gRPC response payload for tether. + Attributes: id (str): Unique identifier for the response. Matches @@ -148,6 +152,7 @@ class EgressResponse(proto.Message): class HttpRequest(proto.Message): r"""The proto definition of http request. + Attributes: id (str): A unique identifier for the request. @@ -192,6 +197,7 @@ class Url(proto.Message): class Header(proto.Message): r"""The http headers. + Attributes: key (str): @@ -205,6 +211,7 @@ class Header(proto.Message): class HttpResponse(proto.Message): r"""The proto definition of http response. + Attributes: id (str): A unique identifier that matches the request diff --git a/tests/unit/gapic/apigeeconnect_v1/test_connection_service.py b/tests/unit/gapic/apigeeconnect_v1/test_connection_service.py index 35d3ad0..55ba3fd 100644 --- a/tests/unit/gapic/apigeeconnect_v1/test_connection_service.py +++ b/tests/unit/gapic/apigeeconnect_v1/test_connection_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.apigeeconnect_v1.services.connection_service import ( @@ -956,6 +957,9 @@ def test_connection_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_connection_service_base_transport_with_credentials_file(): @@ -1436,3 +1440,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 = ConnectionServiceAsyncClient( + 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 = ConnectionServiceClient( + 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 = ConnectionServiceClient( + 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/apigeeconnect_v1/test_tether.py b/tests/unit/gapic/apigeeconnect_v1/test_tether.py index 936d227..7cd2271 100644 --- a/tests/unit/gapic/apigeeconnect_v1/test_tether.py +++ b/tests/unit/gapic/apigeeconnect_v1/test_tether.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.apigeeconnect_v1.services.tether import TetherAsyncClient @@ -623,6 +624,9 @@ def test_tether_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_tether_base_transport_with_credentials_file(): @@ -1064,3 +1068,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 = TetherAsyncClient( + 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 = TetherClient( + 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 = TetherClient( + 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()