Skip to content

Commit 7c527bf

Browse files
feat: add context manager support in client (#196)
- [ ] 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: googleapis/googleapis-gen@81decff Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9
1 parent 6cd8b67 commit 7c527bf

File tree

14 files changed

+178
-8
lines changed

14 files changed

+178
-8
lines changed

packages/google-cloud-texttospeech/google/cloud/texttospeech_v1/services/text_to_speech/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,12 @@ async def synthesize_speech(
333333
# Done; return the response.
334334
return response
335335

336+
async def __aenter__(self):
337+
return self
338+
339+
async def __aexit__(self, exc_type, exc, tb):
340+
await self.transport.close()
341+
336342

337343
try:
338344
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-texttospeech/google/cloud/texttospeech_v1/services/text_to_speech/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,7 @@ def __init__(
325325
client_cert_source_for_mtls=client_cert_source_func,
326326
quota_project_id=client_options.quota_project_id,
327327
client_info=client_info,
328-
always_use_jwt_access=(
329-
Transport == type(self).get_transport_class("grpc")
330-
or Transport == type(self).get_transport_class("grpc_asyncio")
331-
),
328+
always_use_jwt_access=True,
332329
)
333330

334331
def list_voices(
@@ -490,6 +487,19 @@ def synthesize_speech(
490487
# Done; return the response.
491488
return response
492489

490+
def __enter__(self):
491+
return self
492+
493+
def __exit__(self, type, value, traceback):
494+
"""Releases underlying transport's resources.
495+
496+
.. warning::
497+
ONLY use as a context manager if the transport is NOT shared
498+
with other clients! Exiting the with block will CLOSE the transport
499+
and may cause errors in other clients!
500+
"""
501+
self.transport.close()
502+
493503

494504
try:
495505
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-texttospeech/google/cloud/texttospeech_v1/services/text_to_speech/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ def _prep_wrapped_messages(self, client_info):
186186
),
187187
}
188188

189+
def close(self):
190+
"""Closes resources associated with the transport.
191+
192+
.. warning::
193+
Only call this method if the transport is NOT shared
194+
with other clients - this may cause errors in other clients!
195+
"""
196+
raise NotImplementedError()
197+
189198
@property
190199
def list_voices(
191200
self,

packages/google-cloud-texttospeech/google/cloud/texttospeech_v1/services/text_to_speech/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,5 +280,8 @@ def synthesize_speech(
280280
)
281281
return self._stubs["synthesize_speech"]
282282

283+
def close(self):
284+
self.grpc_channel.close()
285+
283286

284287
__all__ = ("TextToSpeechGrpcTransport",)

packages/google-cloud-texttospeech/google/cloud/texttospeech_v1/services/text_to_speech/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,5 +286,8 @@ def synthesize_speech(
286286
)
287287
return self._stubs["synthesize_speech"]
288288

289+
def close(self):
290+
return self.grpc_channel.close()
291+
289292

290293
__all__ = ("TextToSpeechGrpcAsyncIOTransport",)

packages/google-cloud-texttospeech/google/cloud/texttospeech_v1/types/cloud_tts.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class ListVoicesRequest(proto.Message):
7676

7777
class ListVoicesResponse(proto.Message):
7878
r"""The message returned to the client by the ``ListVoices`` method.
79+
7980
Attributes:
8081
voices (Sequence[google.cloud.texttospeech_v1.types.Voice]):
8182
The list of voices.
@@ -86,6 +87,7 @@ class ListVoicesResponse(proto.Message):
8687

8788
class Voice(proto.Message):
8889
r"""Description of a voice supported by the TTS service.
90+
8991
Attributes:
9092
language_codes (Sequence[str]):
9193
The languages that this voice supports, expressed as
@@ -151,6 +153,7 @@ class SynthesisInput(proto.Message):
151153

152154
class VoiceSelectionParams(proto.Message):
153155
r"""Description of which voice to use for a synthesis request.
156+
154157
Attributes:
155158
language_code (str):
156159
Required. The language (and potentially also the region) of
@@ -187,6 +190,7 @@ class VoiceSelectionParams(proto.Message):
187190

188191
class AudioConfig(proto.Message):
189192
r"""Description of audio data to be synthesized.
193+
190194
Attributes:
191195
audio_encoding (google.cloud.texttospeech_v1.types.AudioEncoding):
192196
Required. The format of the audio byte

packages/google-cloud-texttospeech/google/cloud/texttospeech_v1beta1/services/text_to_speech/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,12 @@ async def synthesize_speech(
314314
# Done; return the response.
315315
return response
316316

317+
async def __aenter__(self):
318+
return self
319+
320+
async def __aexit__(self, exc_type, exc, tb):
321+
await self.transport.close()
322+
317323

318324
try:
319325
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-texttospeech/google/cloud/texttospeech_v1beta1/services/text_to_speech/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,7 @@ def __init__(
325325
client_cert_source_for_mtls=client_cert_source_func,
326326
quota_project_id=client_options.quota_project_id,
327327
client_info=client_info,
328-
always_use_jwt_access=(
329-
Transport == type(self).get_transport_class("grpc")
330-
or Transport == type(self).get_transport_class("grpc_asyncio")
331-
),
328+
always_use_jwt_access=True,
332329
)
333330

334331
def list_voices(
@@ -491,6 +488,19 @@ def synthesize_speech(
491488
# Done; return the response.
492489
return response
493490

491+
def __enter__(self):
492+
return self
493+
494+
def __exit__(self, type, value, traceback):
495+
"""Releases underlying transport's resources.
496+
497+
.. warning::
498+
ONLY use as a context manager if the transport is NOT shared
499+
with other clients! Exiting the with block will CLOSE the transport
500+
and may cause errors in other clients!
501+
"""
502+
self.transport.close()
503+
494504

495505
try:
496506
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-texttospeech/google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/base.py

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

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

packages/google-cloud-texttospeech/google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,5 +280,8 @@ def synthesize_speech(
280280
)
281281
return self._stubs["synthesize_speech"]
282282

283+
def close(self):
284+
self.grpc_channel.close()
285+
283286

284287
__all__ = ("TextToSpeechGrpcTransport",)

0 commit comments

Comments
 (0)