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

Commit

Permalink
feat: add context manager support in client (#203)
Browse files Browse the repository at this point in the history
- [ ] 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
  • Loading branch information
gcf-owl-bot[bot] authored Oct 7, 2021
1 parent 185309d commit 91d48a8
Show file tree
Hide file tree
Showing 14 changed files with 202 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,12 @@ async def annotate_text(
# 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/language_v1/services/language_service/client.py
Original file line number Diff line number Diff line change
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 analyze_sentiment(
Expand Down Expand Up @@ -776,6 +773,19 @@ def annotate_text(
# 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
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,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 analyze_sentiment(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,5 +407,8 @@ def annotate_text(
)
return self._stubs["annotate_text"]

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


__all__ = ("LanguageServiceGrpcTransport",)
Original file line number Diff line number Diff line change
Expand Up @@ -413,5 +413,8 @@ def annotate_text(
)
return self._stubs["annotate_text"]

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


__all__ = ("LanguageServiceGrpcAsyncIOTransport",)
15 changes: 15 additions & 0 deletions google/cloud/language_v1/types/language_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class Type(proto.Enum):

class Sentence(proto.Message):
r"""Represents a sentence in the input document.
Attributes:
text (google.cloud.language_v1.types.TextSpan):
The sentence text.
Expand Down Expand Up @@ -182,6 +183,7 @@ class Type(proto.Enum):

class Token(proto.Message):
r"""Represents the smallest syntactic building block of the text.
Attributes:
text (google.cloud.language_v1.types.TextSpan):
The token text.
Expand Down Expand Up @@ -538,6 +540,7 @@ class Type(proto.Enum):

class TextSpan(proto.Message):
r"""Represents an output piece of text.
Attributes:
content (str):
The content of the output text.
Expand All @@ -554,6 +557,7 @@ class TextSpan(proto.Message):

class ClassificationCategory(proto.Message):
r"""Represents a category returned from the text classifier.
Attributes:
name (str):
The name of the category representing the document, from the
Expand All @@ -571,6 +575,7 @@ class ClassificationCategory(proto.Message):

class AnalyzeSentimentRequest(proto.Message):
r"""The sentiment analysis request message.
Attributes:
document (google.cloud.language_v1.types.Document):
Input document.
Expand All @@ -585,6 +590,7 @@ class AnalyzeSentimentRequest(proto.Message):

class AnalyzeSentimentResponse(proto.Message):
r"""The sentiment analysis response message.
Attributes:
document_sentiment (google.cloud.language_v1.types.Sentiment):
The overall sentiment of the input document.
Expand All @@ -606,6 +612,7 @@ class AnalyzeSentimentResponse(proto.Message):

class AnalyzeEntitySentimentRequest(proto.Message):
r"""The entity-level sentiment analysis request message.
Attributes:
document (google.cloud.language_v1.types.Document):
Input document.
Expand All @@ -620,6 +627,7 @@ class AnalyzeEntitySentimentRequest(proto.Message):

class AnalyzeEntitySentimentResponse(proto.Message):
r"""The entity-level sentiment analysis response message.
Attributes:
entities (Sequence[google.cloud.language_v1.types.Entity]):
The recognized entities in the input document
Expand All @@ -638,6 +646,7 @@ class AnalyzeEntitySentimentResponse(proto.Message):

class AnalyzeEntitiesRequest(proto.Message):
r"""The entity analysis request message.
Attributes:
document (google.cloud.language_v1.types.Document):
Input document.
Expand All @@ -652,6 +661,7 @@ class AnalyzeEntitiesRequest(proto.Message):

class AnalyzeEntitiesResponse(proto.Message):
r"""The entity analysis response message.
Attributes:
entities (Sequence[google.cloud.language_v1.types.Entity]):
The recognized entities in the input
Expand All @@ -670,6 +680,7 @@ class AnalyzeEntitiesResponse(proto.Message):

class AnalyzeSyntaxRequest(proto.Message):
r"""The syntax analysis request message.
Attributes:
document (google.cloud.language_v1.types.Document):
Input document.
Expand All @@ -684,6 +695,7 @@ class AnalyzeSyntaxRequest(proto.Message):

class AnalyzeSyntaxResponse(proto.Message):
r"""The syntax analysis response message.
Attributes:
sentences (Sequence[google.cloud.language_v1.types.Sentence]):
Sentences in the input document.
Expand All @@ -705,6 +717,7 @@ class AnalyzeSyntaxResponse(proto.Message):

class ClassifyTextRequest(proto.Message):
r"""The document classification request message.
Attributes:
document (google.cloud.language_v1.types.Document):
Input document.
Expand All @@ -715,6 +728,7 @@ class ClassifyTextRequest(proto.Message):

class ClassifyTextResponse(proto.Message):
r"""The document classification response message.
Attributes:
categories (Sequence[google.cloud.language_v1.types.ClassificationCategory]):
Categories representing the input document.
Expand Down Expand Up @@ -772,6 +786,7 @@ class Features(proto.Message):

class AnnotateTextResponse(proto.Message):
r"""The text annotations response message.
Attributes:
sentences (Sequence[google.cloud.language_v1.types.Sentence]):
Sentences in the input document. Populated if the user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,12 @@ async def annotate_text(
# 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/language_v1beta2/services/language_service/client.py
Original file line number Diff line number Diff line change
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 analyze_sentiment(
Expand Down Expand Up @@ -777,6 +774,19 @@ def annotate_text(
# 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
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,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 analyze_sentiment(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,5 +407,8 @@ def annotate_text(
)
return self._stubs["annotate_text"]

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


__all__ = ("LanguageServiceGrpcTransport",)
Original file line number Diff line number Diff line change
Expand Up @@ -413,5 +413,8 @@ def annotate_text(
)
return self._stubs["annotate_text"]

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


__all__ = ("LanguageServiceGrpcAsyncIOTransport",)
Loading

0 comments on commit 91d48a8

Please sign in to comment.