8000 feat: add context manager support in client (#268) · googleapis/google-cloud-python@243d8db · GitHub
[go: up one dir, main page]

Skip to content

Commit 243d8db

Browse files
feat: add context manager support in client (#268)
- [ ] 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 d70cf41 commit 243d8db

File tree

22 files changed

+278
-12
lines changed

22 files changed

+278
-12
lines changed

packages/google-cloud-speech/google/cloud/speech_v1/services/speech/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,12 @@ def streaming_recognize(
460460
# Done; return the response.
461461
return response
462462

463+
async def __aenter__(self):
464+
return self
465+
466+
async def __aexit__(self, exc_type, exc, tb):
467+
await self.transport.close()
468+
463469

464470
try:
465471
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-speech/google/cloud/speech_v1/services/speech/client.py

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

338335
def recognize(
@@ -610,6 +607,19 @@ def streaming_recognize(
610607
# Done; return the response.
611608
return response
612609

610+
def __enter__(self):
611+
return self
612+
613+
def __exit__(self, type, value, traceback):
614+
"""Releases underlying transport's resources.
615+
616+
.. warning::
617+
ONLY use as a context manager if the transport is NOT shared
618+
with other clients! Exiting the with block will CLOSE the transport
619+
and may cause errors in other clients!
620+
"""
621+
self.transport.close()
622+
613623

614624
try:
615625
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-speech/google/cloud/speech_v1/services/speech/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,15 @@ def _prep_wrapped_messages(self, client_info):
191191
),
192192
}
193193

194+
def close(self):
195+
"""Closes resources associated with the transport.
196+
197+
.. warning::
198+
Only call this method if the transport is NOT shared
199+
with other clients - this may cause errors in other clients!
200+
"""
201+
raise NotImplementedError()
202+
194203
@property
195204
def operations_client(self) -> operations_v1.OperationsClient:
196205
"""Return the client designed to process long-running operations."""

packages/google-cloud-speech/google/cloud/speech_v1/services/speech/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,5 +331,8 @@ def streaming_recognize(
331331
)
332332
return self._stubs["streaming_recognize"]
333333

334+
def close(self):
335+
self.grpc_channel.close()
336+
334337

335338
__all__ = ("SpeechGrpcTransport",)

packages/google-cloud-speech/google/cloud/speech_v1/services/speech/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,5 +340,8 @@ def streaming_recognize(
340340
)
341341
return self._stubs["streaming_recognize"]
342342

343+
def close(self):
344+
return self.grpc_channel.close()
345+
343346

344347
__all__ = ("SpeechGrpcAsyncIOTransport",)

packages/google-cloud-speech/google/cloud/speech_v1/types/cloud_speech.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ class AudioEncoding(proto.Enum):
381381

382382
class SpeakerDiarizationConfig(proto.Message):
383383
r"""Config to enable speaker diarization.
384+
384385
Attributes:
385386
enable_speaker_diarization (bool):
386387
If 'true', enables speaker detection for each recognized
@@ -410,6 +411,7 @@ class SpeakerDiarizationConfig(proto.Message):
410411

411412
class RecognitionMetadata(proto.Message):
412413
r"""Description of audio data to be recognized.
414+
413415
Attributes:
414416
interaction_type (google.cloud.speech_v1.types.RecognitionMetadata.InteractionType):
415417
The use case most closely describing the
@@ -793,6 +795,7 @@ class SpeechRecognitionResult(proto.Message):
793795

794796
class SpeechRecognitionAlternative(proto.Message):
795797
r"""Alternative hypotheses (a.k.a. n-best list).
798+
796799
Attributes:
797800
transcript (str):
798801
Transcript text representing the words that
@@ -819,6 +822,7 @@ class SpeechRecognitionAlternative(proto.Message):
819822

820823
class WordInfo(proto.Message):
821824
r"""Word-specific information for recognized words.
825+
822826
Attributes:
823827
start_time (google.protobuf.duration_pb2.Duration):
824828
Time offset relative to the beginning of the audio, and

packages/google-cloud-speech/google/cloud/speech_v1p1beta1/services/adaptation/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,12 @@ async def delete_custom_class(
971971
request, retry=retry, timeout=timeout, metadata=metadata,
972972
)
973973

974+
async def __aenter__(self):
975+
return self
976+
977+
async def __aexit__(self, exc_type, exc, tb):
978+
await self.transport.close()
979+
974980

975981
try:
976982
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-speech/google/cloud/speech_v1p1beta1/services/adaptation/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,7 @@ def __init__(
360360
client_cert_source_for_mtls=client_cert_source_func,
361361
quota_project_id=client_options.quota_project_id,
362362
client_info=client_info,
363-
always_use_jwt_access=(
364-
Transport == type(self).get_transport_class("grpc")
365-
or Transport == type(self).get_transport_class("grpc_asyncio")
366-
),
363+
always_use_jwt_access=True,
367364
)
368365

369366
def create_phrase_set(
@@ -1178,6 +1175,19 @@ def delete_custom_class(
11781175
request, retry=retry, timeout=timeout, metadata=metadata,
11791176
)
11801177

1178+
def __enter__(self):
1179+
return self
1180+
1181+
def __exit__(self, type, value, traceback):
1182+
"""Releases underlying transport's resources.
1183+
1184+
.. warning::
1185+
ONLY use as a context manager if the transport is NOT shared
1186+
with other clients! Exiting the with block will CLOSE the transport
1187+
and may cause errors in other clients!
1188+
"""
1189+
self.transport.close()
1190+
11811191

11821192
try:
11831193
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-speech/google/cloud/speech_v1p1beta1/services/adaptation/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 create_phrase_set(
191200
self,

packages/google-cloud-speech/google/cloud/speech_v1p1beta1/services/adaptation/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,5 +502,8 @@ def delete_custom_class(
502502
)
503503
return self._stubs["delete_custom_class"]
504504

505+
def close(self):
506+
self.grpc_channel.close()
507+
505508

506509
__all__ = ("AdaptationGrpcTransport",)

0 commit comments

Comments
 (0)
0