diff --git a/google/cloud/mediatranslation_v1beta1/services/speech_translation_service/async_client.py b/google/cloud/mediatranslation_v1beta1/services/speech_translation_service/async_client.py index 52ed49d..e8b324b 100644 --- a/google/cloud/mediatranslation_v1beta1/services/speech_translation_service/async_client.py +++ b/google/cloud/mediatranslation_v1beta1/services/speech_translation_service/async_client.py @@ -225,6 +225,12 @@ def streaming_translate_speech( # 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/mediatranslation_v1beta1/services/speech_translation_service/client.py b/google/cloud/mediatranslation_v1beta1/services/speech_translation_service/client.py index 1e81fc9..1991f37 100644 --- a/google/cloud/mediatranslation_v1beta1/services/speech_translation_service/client.py +++ b/google/cloud/mediatranslation_v1beta1/services/speech_translation_service/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 streaming_translate_speech( @@ -384,6 +381,19 @@ def streaming_translate_speech( # 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/mediatranslation_v1beta1/services/speech_translation_service/transports/base.py b/google/cloud/mediatranslation_v1beta1/services/speech_translation_service/transports/base.py index ed7c9f2..e29b876 100644 --- a/google/cloud/mediatranslation_v1beta1/services/speech_translation_service/transports/base.py +++ b/google/cloud/mediatranslation_v1beta1/services/speech_translation_service/transports/base.py @@ -161,6 +161,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 streaming_translate_speech( self, diff --git a/google/cloud/mediatranslation_v1beta1/services/speech_translation_service/transports/grpc.py b/google/cloud/mediatranslation_v1beta1/services/speech_translation_service/transports/grpc.py index 7a3142c..fd3aafc 100644 --- a/google/cloud/mediatranslation_v1beta1/services/speech_translation_service/transports/grpc.py +++ b/google/cloud/mediatranslation_v1beta1/services/speech_translation_service/transports/grpc.py @@ -256,5 +256,8 @@ def streaming_translate_speech( ) return self._stubs["streaming_translate_speech"] + def close(self): + self.grpc_channel.close() + __all__ = ("SpeechTranslationServiceGrpcTransport",) diff --git a/google/cloud/mediatranslation_v1beta1/services/speech_translation_service/transports/grpc_asyncio.py b/google/cloud/mediatranslation_v1beta1/services/speech_translation_service/transports/grpc_asyncio.py index bef8cdb..4e3e5dd 100644 --- a/google/cloud/mediatranslation_v1beta1/services/speech_translation_service/transports/grpc_asyncio.py +++ b/google/cloud/mediatranslation_v1beta1/services/speech_translation_service/transports/grpc_asyncio.py @@ -259,5 +259,8 @@ def streaming_translate_speech( ) return self._stubs["streaming_translate_speech"] + def close(self): + return self.grpc_channel.close() + __all__ = ("SpeechTranslationServiceGrpcAsyncIOTransport",) diff --git a/google/cloud/mediatranslation_v1beta1/types/media_translation.py b/google/cloud/mediatranslation_v1beta1/types/media_translation.py index afeb0ef..419c5a3 100644 --- a/google/cloud/mediatranslation_v1beta1/types/media_translation.py +++ b/google/cloud/mediatranslation_v1beta1/types/media_translation.py @@ -107,6 +107,7 @@ class TranslateSpeechConfig(proto.Message): class StreamingTranslateSpeechConfig(proto.Message): r"""Config used for streaming translation. + Attributes: audio_config (google.cloud.mediatranslation_v1beta1.types.TranslateSpeechConfig): Required. The common config for all the @@ -187,6 +188,7 @@ class StreamingTranslateSpeechResult(proto.Message): class TextTranslationResult(proto.Message): r"""Text translation result. + Attributes: translation (str): Output only. The translated sentence. diff --git a/tests/unit/gapic/mediatranslation_v1beta1/test_speech_translation_service.py b/tests/unit/gapic/mediatranslation_v1beta1/test_speech_translation_service.py index e04253d..c086157 100644 --- a/tests/unit/gapic/mediatranslation_v1beta1/test_speech_translation_service.py +++ b/tests/unit/gapic/mediatranslation_v1beta1/test_speech_translation_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.mediatranslation_v1beta1.services.speech_translation_service import ( @@ -709,6 +710,9 @@ def test_speech_translation_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_speech_translation_service_base_transport_with_credentials_file(): @@ -1171,3 +1175,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 = SpeechTranslationServiceAsyncClient( + 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 = SpeechTranslationServiceClient( + 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 = SpeechTranslationServiceClient( + 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()