Skip to content
This repository was archived by the owner on Sep 5, 2023. It is now read-only.

Commit 50de95e

Browse files
feat: add context manager support in client (#116)
- [ ] Regenerate this pull request now. chore: fix docstring for first attribute of protos committer: @busunkim96 PiperOrigin-RevId: 401271153 Source-Link: googleapis/googleapis@787f8c9 Source-Link: https://github.com/googleapis/googleapis-gen/commit/81decffe9fc72396a8153e756d1d67a6eecfd620 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9
1 parent 2841347 commit 50de95e

File tree

7 files changed

+87
-4
lines changed

7 files changed

+87
-4
lines changed

google/cloud/mediatranslation_v1beta1/services/speech_translation_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ def streaming_translate_speech(
225225
# Done; return the response.
226226
return response
227227

228+
async def __aenter__(self):
229+
return self
230+
231+
async def __aexit__(self, exc_type, exc, tb):
232+
await self.transport.close()
233+
228234

229235
try:
230236
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/mediatranslation_v1beta1/services/speech_translation_service/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,7 @@ def __init__(
330330
client_cert_source_for_mtls=client_cert_source_func,
331331
quota_project_id=client_options.quota_project_id,
332332
client_info=client_info,
333-
always_use_jwt_access=(
334-
Transport == type(self).get_transport_class("grpc")
335-
or Transport == type(self).get_transport_class("grpc_asyncio")
336-
),
333+
always_use_jwt_access=True,
337334
)
338335

339336
def streaming_translate_speech(
@@ -384,6 +381,19 @@ def streaming_translate_speech(
384381
# Done; return the response.
385382
return response
386383

384+
def __enter__(self):
385+
return self
386+
387+
def __exit__(self, type, value, traceback):
388+
"""Releases underlying transport's resources.
389+
390+
.. warning::
391+
ONLY use as a context manager if the transport is NOT shared
392+
with other clients! Exiting the with block will CLOSE the transport
393+
and may cause errors in other clients!
394+
"""
395+
self.transport.close()
396+
387397

388398
try:
389399
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/mediatranslation_v1beta1/services/speech_translation_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ def _prep_wrapped_messages(self, client_info):
161161
),
162162
}
163163

164+
def close(self):
165+
"""Closes resources associated with the transport.
166+
167+
.. warning::
168+
Only call this method if the transport is NOT shared
169+
with other clients - this may cause errors in other clients!
170+
"""
171+
raise NotImplementedError()
172+
164173
@property
165174
def streaming_translate_speech(
166175
self,

google/cloud/mediatranslation_v1beta1/services/speech_translation_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,5 +256,8 @@ def streaming_translate_speech(
256256
)
257257
return self._stubs["streaming_translate_speech"]
258258

259+
def close(self):
260+
self.grpc_channel.close()
261+
259262

260263
__all__ = ("SpeechTranslationServiceGrpcTransport",)

google/cloud/mediatranslation_v1beta1/services/speech_translation_service/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,5 +259,8 @@ def streaming_translate_speech(
259259
)
260260
return self._stubs["streaming_translate_speech"]
261261

262+
def close(self):
263+
return self.grpc_channel.close()
264+
262265

263266
__all__ = ("SpeechTranslationServiceGrpcAsyncIOTransport",)

google/cloud/mediatranslation_v1beta1/types/media_translation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class TranslateSpeechConfig(proto.Message):
107107

108108
class StreamingTranslateSpeechConfig(proto.Message):
109109
r"""Config used for streaming translation.
110+
110111
Attributes:
111112
audio_config (google.cloud.mediatranslation_v1beta1.types.TranslateSpeechConfig):
112113
Required. The common config for all the
@@ -187,6 +188,7 @@ class StreamingTranslateSpeechResult(proto.Message):
187188

188189
class TextTranslationResult(proto.Message):
189190
r"""Text translation result.
191+
190192
Attributes:
191193
translation (str):
192194
Output only. The translated sentence.

tests/unit/gapic/mediatranslation_v1beta1/test_speech_translation_service.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from google.api_core import gapic_v1
3030
from google.api_core import grpc_helpers
3131
from google.api_core import grpc_helpers_async
32+
from google.api_core import path_template
3233
from google.auth import credentials as ga_credentials
3334
from google.auth.exceptions import MutualTLSChannelError
3435
from google.cloud.mediatranslation_v1beta1.services.speech_translation_service import (
@@ -709,6 +710,9 @@ def test_speech_translation_service_base_transport():
709710
with pytest.raises(NotImplementedError):
710711
getattr(transport, method)(request=object())
711712

713+
with pytest.raises(NotImplementedError):
714+
transport.close()
715+
712716

713717
@requires_google_auth_gte_1_25_0
714718
def test_speech_translation_service_base_transport_with_credentials_file():
@@ -1171,3 +1175,49 @@ def test_client_withDEFAULT_CLIENT_INFO():
11711175
credentials=ga_credentials.AnonymousCredentials(), client_info=client_info,
11721176
)
11731177
prep.assert_called_once_with(client_info)
1178+
1179+
1180+
@pytest.mark.asyncio
1181+
async def test_transport_close_async():
1182+
client = SpeechTranslationServiceAsyncClient(
1183+
credentials=ga_credentials.AnonymousCredentials(), transport="grpc_asyncio",
1184+
)
1185+
with mock.patch.object(
1186+
type(getattr(client.transport, "grpc_channel")), "close"
1187+
) as close:
1188+
async with client:
1189+
close.assert_not_called()
1190+
close.assert_called_once()
1191+
1192+
1193+
def test_transport_close():
1194+
transports = {
1195+
"grpc": "_grpc_channel",
1196+
}
1197+
1198+
for transport, close_name in transports.items():
1199+
client = SpeechTranslationServiceClient(
1200+
credentials=ga_credentials.AnonymousCredentials(), transport=transport
1201+
)
1202+
with mock.patch.object(
1203+
type(getattr(client.transport, close_name)), "close"
1204+
) as close:
1205+
with client:
1206+
close.assert_not_called()
1207+
close.assert_called_once()
1208+
1209+
1210+
def test_client_ctx():
1211+
transports = [
1212+
"grpc",
1213+
]
1214+
for transport in transports:
1215+
client = SpeechTranslationServiceClient(
1216+
credentials=ga_credentials.AnonymousCredentials(), transport=transport
1217+
)
1218+
# Test client calls underlying transport.
1219+
with mock.patch.object(type(client.transport), "close") as close:
1220+
close.assert_not_called()
1221+
with client:
1222+
pass
1223+
close.assert_called()

0 commit comments

Comments
 (0)