Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

feat: add context manager support in client #268

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions google/cloud/speech_v1/services/speech/async_client.py
Expand Up @@ -460,6 +460,12 @@ def streaming_recognize(
# 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(
Expand Down
18 changes: 14 additions & 4 deletions google/cloud/speech_v1/services/speech/client.py
Expand Up @@ -329,10 +329,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 recognize(
Expand Down Expand Up @@ -610,6 +607,19 @@ def streaming_recognize(
# 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(
Expand Down
9 changes: 9 additions & 0 deletions google/cloud/speech_v1/services/speech/transports/base.py
Expand Up @@ -191,6 +191,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 operations_client(self) -> operations_v1.OperationsClient:
"""Return the client designed to process long-running operations."""
Expand Down
3 changes: 3 additions & 0 deletions google/cloud/speech_v1/services/speech/transports/grpc.py
Expand Up @@ -331,5 +331,8 @@ def streaming_recognize(
)
return self._stubs["streaming_recognize"]

def close(self):
self.grpc_channel.close()


__all__ = ("SpeechGrpcTransport",)
Expand Up @@ -340,5 +340,8 @@ def streaming_recognize(
)
return self._stubs["streaming_recognize"]

def close(self):
return self.grpc_channel.close()


__all__ = ("SpeechGrpcAsyncIOTransport",)
4 changes: 4 additions & 0 deletions google/cloud/speech_v1/types/cloud_speech.py
Expand Up @@ -381,6 +381,7 @@ class AudioEncoding(proto.Enum):

class SpeakerDiarizationConfig(proto.Message):
r"""Config to enable speaker diarization.

Attributes:
enable_speaker_diarization (bool):
If 'true', enables speaker detection for each recognized
Expand Down Expand Up @@ -410,6 +411,7 @@ class SpeakerDiarizationConfig(proto.Message):

class RecognitionMetadata(proto.Message):
r"""Description of audio data to be recognized.

Attributes:
interaction_type (google.cloud.speech_v1.types.RecognitionMetadata.InteractionType):
The use case most closely describing the
Expand Down Expand Up @@ -793,6 +795,7 @@ class SpeechRecognitionResult(proto.Message):

class SpeechRecognitionAlternative(proto.Message):
r"""Alternative hypotheses (a.k.a. n-best list).

Attributes:
transcript (str):
Transcript text representing the words that
Expand All @@ -819,6 +822,7 @@ class SpeechRecognitionAlternative(proto.Message):

class WordInfo(proto.Message):
r"""Word-specific information for recognized words.

Attributes:
start_time (google.protobuf.duration_pb2.Duration):
Time offset relative to the beginning of the audio, and
Expand Down
Expand Up @@ -971,6 +971,12 @@ async def delete_custom_class(
request, retry=retry, timeout=timeout, metadata=metadata,
)

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(
Expand Down
18 changes: 14 additions & 4 deletions google/cloud/speech_v1p1beta1/services/adaptation/client.py
Expand Up @@ -360,10 +360,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 create_phrase_set(
Expand Down Expand Up @@ -1178,6 +1175,19 @@ def delete_custom_class(
request, retry=retry, timeout=timeout, metadata=metadata,
)

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(
Expand Down
Expand Up @@ -186,6 +186,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 create_phrase_set(
self,
Expand Down
Expand Up @@ -502,5 +502,8 @@ def delete_custom_class(
)
return self._stubs["delete_custom_class"]

def close(self):
self.grpc_channel.close()


__all__ = ("AdaptationGrpcTransport",)
Expand Up @@ -517,5 +517,8 @@ def delete_custom_class(
)
return self._stubs["delete_custom_class"]

def close(self):
return self.grpc_channel.close()


__all__ = ("AdaptationGrpcAsyncIOTransport",)
6 changes: 6 additions & 0 deletions google/cloud/speech_v1p1beta1/services/speech/async_client.py
Expand Up @@ -464,6 +464,12 @@ def streaming_recognize(
# 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(
Expand Down
18 changes: 14 additions & 4 deletions google/cloud/speech_v1p1beta1/services/speech/client.py
Expand Up @@ -361,10 +361,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 recognize(
Expand Down Expand Up @@ -642,6 +639,19 @@ def streaming_recognize(
# 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(
Expand Down
Expand Up @@ -191,6 +191,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 operations_client(self) -> operations_v1.OperationsClient:
"""Return the client designed to process long-running operations."""
Expand Down
Expand Up @@ -331,5 +331,8 @@ def streaming_recognize(
)
return self._stubs["streaming_recognize"]

def close(self):
self.grpc_channel.close()


__all__ = ("SpeechGrpcTransport",)
Expand Up @@ -340,5 +340,8 @@ def streaming_recognize(
)
return self._stubs["streaming_recognize"]

def close(self):
return self.grpc_channel.close()


__all__ = ("SpeechGrpcAsyncIOTransport",)
4 changes: 4 additions & 0 deletions google/cloud/speech_v1p1beta1/types/cloud_speech.py
Expand Up @@ -461,6 +461,7 @@ class AudioEncoding(proto.Enum):

class SpeakerDiarizationConfig(proto.Message):
r"""Config to enable speaker diarization.

Attributes:
enable_speaker_diarization (bool):
If 'true', enables speaker detection for each recognized
Expand Down Expand Up @@ -490,6 +491,7 @@ class SpeakerDiarizationConfig(proto.Message):

class RecognitionMetadata(proto.Message):
r"""Description of audio data to be recognized.

Attributes:
interaction_type (google.cloud.speech_v1p1beta1.types.RecognitionMetadata.InteractionType):
The use case most closely describing the
Expand Down Expand Up @@ -914,6 +916,7 @@ class SpeechRecognitionResult(proto.Message):

class SpeechRecognitionAlternative(proto.Message):
r"""Alternative hypotheses (a.k.a. n-best list).

Attributes:
transcript (str):
Transcript text representing the words that
Expand All @@ -940,6 +943,7 @@ class SpeechRecognitionAlternative(proto.Message):

class WordInfo(proto.Message):
r"""Word-specific information for recognized words.

Attributes:
start_time (google.protobuf.duration_pb2.Duration):
Time offset relative to the beginning of the audio, and
Expand Down
12 changes: 12 additions & 0 deletions google/cloud/speech_v1p1beta1/types/cloud_speech_adaptation.py
Expand Up @@ -40,6 +40,7 @@

class CreatePhraseSetRequest(proto.Message):
r"""Message sent by the client for the ``CreatePhraseSet`` method.

Attributes:
parent (str):
Required. The parent resource where this phrase set will be
Expand All @@ -63,6 +64,7 @@ class CreatePhraseSetRequest(proto.Message):

class UpdatePhraseSetRequest(proto.Message):
r"""Message sent by the client for the ``UpdatePhraseSet`` method.

Attributes:
phrase_set (google.cloud.speech_v1p1beta1.types.PhraseSet):
Required. The phrase set to update.
Expand All @@ -82,6 +84,7 @@ class UpdatePhraseSetRequest(proto.Message):

class GetPhraseSetRequest(proto.Message):
r"""Message sent by the client for the ``GetPhraseSet`` method.

Attributes:
name (str):
Required. The name of the phrase set to retrieve. Format:
Expand All @@ -93,6 +96,7 @@ class GetPhraseSetRequest(proto.Message):

class ListPhraseSetRequest(proto.Message):
r"""Message sent by the client for the ``ListPhraseSet`` method.

Attributes:
parent (str):
Required. The parent, which owns this
Expand Down Expand Up @@ -120,6 +124,7 @@ class ListPhraseSetRequest(proto.Message):

class ListPhraseSetResponse(proto.Message):
r"""Message returned to the client by the ``ListPhraseSet`` method.

Attributes:
phrase_sets (Sequence[google.cloud.speech_v1p1beta1.types.PhraseSet]):
The phrase set.
Expand All @@ -141,6 +146,7 @@ def raw_page(self):

class DeletePhraseSetRequest(proto.Message):
r"""Message sent by the client for the ``DeletePhraseSet`` method.

Attributes:
name (str):
Required. The name of the phrase set to delete. Format:
Expand All @@ -152,6 +158,7 @@ class DeletePhraseSetRequest(proto.Message):

class CreateCustomClassRequest(proto.Message):
r"""Message sent by the client for the ``CreateCustomClass`` method.

Attributes:
parent (str):
Required. The parent resource where this custom class will
Expand All @@ -175,6 +182,7 @@ class CreateCustomClassRequest(proto.Message):

class UpdateCustomClassRequest(proto.Message):
r"""Message sent by the client for the ``UpdateCustomClass`` method.

Attributes:
custom_class (google.cloud.speech_v1p1beta1.types.CustomClass):
Required. The custom class to update.
Expand All @@ -194,6 +202,7 @@ class UpdateCustomClassRequest(proto.Message):

class GetCustomClassRequest(proto.Message):
r"""Message sent by the client for the ``GetCustomClass`` method.

Attributes:
name (str):
Required. The name of the custom class to retrieve. Format:
Expand All @@ -205,6 +214,7 @@ class GetCustomClassRequest(proto.Message):

class ListCustomClassesRequest(proto.Message):
r"""Message sent by the client for the ``ListCustomClasses`` method.

Attributes:
parent (str):
Required. The parent, which owns this collection of custom
Expand Down Expand Up @@ -232,6 +242,7 @@ class ListCustomClassesRequest(proto.Message):

class ListCustomClassesResponse(proto.Message):
r"""Message returned to the client by the ``ListCustomClasses`` method.

Attributes:
custom_classes (Sequence[google.cloud.speech_v1p1beta1.types.CustomClass]):
The custom classes.
Expand All @@ -253,6 +264,7 @@ def raw_page(self):

class DeleteCustomClassRequest(proto.Message):
r"""Message sent by the client for the ``DeleteCustomClass`` method.

Attributes:
name (str):
Required. The name of the custom class to delete. Format:
Expand Down