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

feat: add context manager support in client #267

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
Expand Up @@ -1123,6 +1123,12 @@ async def delete_glossary(
# 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/translate_v3/services/translation_service/client.py
Expand Up @@ -349,10 +349,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 translate_text(
Expand Down Expand Up @@ -1276,6 +1273,19 @@ def delete_glossary(
# 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 @@ -241,6 +241,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 @@ -549,5 +549,8 @@ def delete_glossary(
)
return self._stubs["delete_glossary"]

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


__all__ = ("TranslationServiceGrpcTransport",)
Expand Up @@ -557,5 +557,8 @@ def delete_glossary(
)
return self._stubs["delete_glossary"]

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


__all__ = ("TranslationServiceGrpcAsyncIOTransport",)
32 changes: 32 additions & 0 deletions google/cloud/translate_v3/types/translation_service.py
Expand Up @@ -86,6 +86,7 @@ class TranslateTextGlossaryConfig(proto.Message):

class TranslateTextRequest(proto.Message):
r"""The request message for synchronous translation.

Attributes:
contents (Sequence[str]):
Required. The content of the input in string
Expand Down Expand Up @@ -176,6 +177,7 @@ class TranslateTextRequest(proto.Message):

class TranslateTextResponse(proto.Message):
r"""

Attributes:
translations (Sequence[google.cloud.translate_v3.types.Translation]):
Text translation responses with no glossary applied. This
Expand All @@ -197,6 +199,7 @@ class TranslateTextResponse(proto.Message):

class Translation(proto.Message):
r"""A single translation response.

Attributes:
translated_text (str):
Text translated into the target language.
Expand Down Expand Up @@ -232,6 +235,7 @@ class Translation(proto.Message):

class DetectLanguageRequest(proto.Message):
r"""The request message for language detection.

Attributes:
parent (str):
Required. Project or location to make a call. Must refer to
Expand Down Expand Up @@ -287,6 +291,7 @@ class DetectLanguageRequest(proto.Message):

class DetectedLanguage(proto.Message):
r"""The response message for language detection.

Attributes:
language_code (str):
The BCP-47 language code of source content in
Expand All @@ -302,6 +307,7 @@ class DetectedLanguage(proto.Message):

class DetectLanguageResponse(proto.Message):
r"""The response message for language detection.

Attributes:
languages (Sequence[google.cloud.translate_v3.types.DetectedLanguage]):
The most probable language detected by the
Expand All @@ -317,6 +323,7 @@ class DetectLanguageResponse(proto.Message):

class GetSupportedLanguagesRequest(proto.Message):
r"""The request message for discovering supported languages.

Attributes:
parent (str):
Required. Project or location to make a call. Must refer to
Expand Down Expand Up @@ -362,6 +369,7 @@ class GetSupportedLanguagesRequest(proto.Message):

class SupportedLanguages(proto.Message):
r"""The response message for discovering supported languages.

Attributes:
languages (Sequence[google.cloud.translate_v3.types.SupportedLanguage]):
A list of supported language responses. This
Expand Down Expand Up @@ -403,6 +411,7 @@ class SupportedLanguage(proto.Message):

class GcsSource(proto.Message):
r"""The Google Cloud Storage location for the input content.

Attributes:
input_uri (str):
Required. Source data URI. For example,
Expand All @@ -414,6 +423,7 @@ class GcsSource(proto.Message):

class InputConfig(proto.Message):
r"""Input configuration for BatchTranslateText request.

Attributes:
mime_type (str):
Optional. Can be "text/plain" or "text/html". For ``.tsv``,
Expand Down Expand Up @@ -450,6 +460,7 @@ class InputConfig(proto.Message):

class GcsDestination(proto.Message):
r"""The Google Cloud Storage location for the output content.

Attributes:
output_uri_prefix (str):
Required. The bucket used in 'output_uri_prefix' must exist
Expand All @@ -465,6 +476,7 @@ class GcsDestination(proto.Message):

class OutputConfig(proto.Message):
r"""Output configuration for BatchTranslateText request.

Attributes:
gcs_destination (google.cloud.translate_v3.types.GcsDestination):
Google Cloud Storage destination for output content. For
Expand Down Expand Up @@ -552,6 +564,7 @@ class OutputConfig(proto.Message):

class DocumentInputConfig(proto.Message):
r"""A document translation request input config.

Attributes:
content (bytes):
Document's content represented as a stream of
Expand Down Expand Up @@ -582,6 +595,7 @@ class DocumentInputConfig(proto.Message):

class DocumentOutputConfig(proto.Message):
r"""A document translation request output config.

Attributes:
gcs_destination (google.cloud.translate_v3.types.GcsDestination):
Optional. Google Cloud Storage destination for the
Expand Down Expand Up @@ -653,6 +667,7 @@ class DocumentOutputConfig(proto.Message):

class TranslateDocumentRequest(proto.Message):
r"""A document translation request.

Attributes:
parent (str):
Required. Location to make a regional call.
Expand Down Expand Up @@ -744,6 +759,7 @@ class TranslateDocumentRequest(proto.Message):

class DocumentTranslation(proto.Message):
r"""A translated document message.

Attributes:
byte_stream_outputs (Sequence[bytes]):
The array of translated documents. It is
Expand All @@ -769,6 +785,7 @@ class DocumentTranslation(proto.Message):

class TranslateDocumentResponse(proto.Message):
r"""A translated document response message.

Attributes:
document_translation (google.cloud.translate_v3.types.DocumentTranslation):
Translated document.
Expand Down Expand Up @@ -804,6 +821,7 @@ class TranslateDocumentResponse(proto.Message):

class BatchTranslateTextRequest(proto.Message):
r"""The batch translation request.

Attributes:
parent (str):
Required. Location to make a call. Must refer to a caller's
Expand Down Expand Up @@ -880,6 +898,7 @@ class BatchTranslateTextRequest(proto.Message):

class BatchTranslateMetadata(proto.Message):
r"""State metadata for the batch translation operation.

Attributes:
state (google.cloud.translate_v3.types.BatchTranslateMetadata.State):
The state of the operation.
Expand Down Expand Up @@ -948,6 +967,7 @@ class BatchTranslateResponse(proto.Message):

class GlossaryInputConfig(proto.Message):
r"""Input configuration for glossaries.

Attributes:
gcs_source (google.cloud.translate_v3.types.GcsSource):
Required. Google Cloud Storage location of glossary data.
Expand Down Expand Up @@ -983,6 +1003,7 @@ class GlossaryInputConfig(proto.Message):

class Glossary(proto.Message):
r"""Represents a glossary built from user provided data.

Attributes:
name (str):
Required. The resource name of the glossary. Glossary names
Expand All @@ -1008,6 +1029,7 @@ class Glossary(proto.Message):

class LanguageCodePair(proto.Message):
r"""Used with unidirectional glossaries.

Attributes:
source_language_code (str):
Required. The BCP-47 language code of the input text, for
Expand All @@ -1024,6 +1046,7 @@ class LanguageCodePair(proto.Message):

class LanguageCodesSet(proto.Message):
r"""Used with equivalent term set glossaries.

Attributes:
language_codes (Sequence[str]):
The BCP-47 language code(s) for terms defined in the
Expand All @@ -1049,6 +1072,7 @@ class LanguageCodesSet(proto.Message):

class CreateGlossaryRequest(proto.Message):
r"""Request message for CreateGlossary.

Attributes:
parent (str):
Required. The project name.
Expand All @@ -1062,6 +1086,7 @@ class CreateGlossaryRequest(proto.Message):

class GetGlossaryRequest(proto.Message):
r"""Request message for GetGlossary.

Attributes:
name (str):
Required. The name of the glossary to
Expand All @@ -1073,6 +1098,7 @@ class GetGlossaryRequest(proto.Message):

class DeleteGlossaryRequest(proto.Message):
r"""Request message for DeleteGlossary.

Attributes:
name (str):
Required. The name of the glossary to delete.
Expand All @@ -1083,6 +1109,7 @@ class DeleteGlossaryRequest(proto.Message):

class ListGlossariesRequest(proto.Message):
r"""Request message for ListGlossaries.

Attributes:
parent (str):
Required. The name of the project from which
Expand Down Expand Up @@ -1133,6 +1160,7 @@ class ListGlossariesRequest(proto.Message):

class ListGlossariesResponse(proto.Message):
r"""Response message for ListGlossaries.

Attributes:
glossaries (Sequence[google.cloud.translate_v3.types.Glossary]):
The list of glossaries for a project.
Expand Down Expand Up @@ -1241,6 +1269,7 @@ class DeleteGlossaryResponse(proto.Message):

class BatchTranslateDocumentRequest(proto.Message):
r"""The BatchTranslateDocument request.

Attributes:
parent (str):
Required. Location to make a regional call.
Expand Down Expand Up @@ -1326,6 +1355,7 @@ class BatchTranslateDocumentRequest(proto.Message):

class BatchDocumentInputConfig(proto.Message):
r"""Input configuration for BatchTranslateDocument request.

Attributes:
gcs_source (google.cloud.translate_v3.types.GcsSource):
Google Cloud Storage location for the source input. This can
Expand Down Expand Up @@ -1357,6 +1387,7 @@ class BatchDocumentInputConfig(proto.Message):

class BatchDocumentOutputConfig(proto.Message):
r"""Output configuration for BatchTranslateDocument request.

Attributes:
gcs_destination (google.cloud.translate_v3.types.GcsDestination):
Google Cloud Storage destination for output content. For
Expand Down Expand Up @@ -1475,6 +1506,7 @@ class BatchTranslateDocumentResponse(proto.Message):

class BatchTranslateDocumentMetadata(proto.Message):
r"""State metadata for the batch translation operation.

Attributes:
state (google.cloud.translate_v3.types.BatchTranslateDocumentMetadata.State):
The state of the operation.
Expand Down
Expand Up @@ -1027,6 +1027,12 @@ async def delete_glossary(
# 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
Expand Up @@ -349,10 +349,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 translate_text(
Expand Down Expand Up @@ -1181,6 +1178,19 @@ def delete_glossary(
# 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 @@ -241,6 +241,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