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

Commit 91d48a8

Browse files
feat: add context manager support in client (#203)
- [ ] 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: https://github.com/googleapis/googleapis-gen/commit/81decffe9fc72396a8153e756d1d67a6eecfd620 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9
1 parent 185309d commit 91d48a8

File tree

14 files changed

+202
-8
lines changed

14 files changed

+202
-8
lines changed

google/cloud/language_v1/services/language_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,12 @@ async def annotate_text(
661661
# Done; return the response.
662662
return response
663663

664+
async def __aenter__(self):
665+
return self
666+
667+
async def __aexit__(self, exc_type, exc, tb):
668+
await self.transport.close()
669+
664670

665671
try:
666672
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/language_v1/services/language_service/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 analyze_sentiment(
@@ -776,6 +773,19 @@ def annotate_text(
776773
# Done; return the response.
777774
return response
778775

776+
def __enter__(self):
777+
return self
778+
779+
def __exit__(self, type, value, traceback):
780+
"""Releases underlying transport's resources.
781+
782+
.. warning::
783+
ONLY use as a context manager if the transport is NOT shared
784+
with other clients! Exiting the with block will CLOSE the transport
785+
and may cause errors in other clients!
786+
"""
787+
self.transport.close()
788+
779789

780790
try:
781791
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/language_v1/services/language_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,15 @@ def _prep_wrapped_messages(self, client_info):
247247
),
248248
}
249249

250+
def close(self):
251+
"""Closes resources associated with the transport.
252+
253+
.. warning::
254+
Only call this method if the transport is NOT shared
255+
with other clients - this may cause errors in other clients!
256+
"""
257+
raise NotImplementedError()
258+
250259
@property
251260
def analyze_sentiment(
252261
self,

google/cloud/language_v1/services/language_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,5 +407,8 @@ def annotate_text(
407407
)
408408
return self._stubs["annotate_text"]
409409

410+
def close(self):
411+
self.grpc_channel.close()
412+
410413

411414
__all__ = ("LanguageServiceGrpcTransport",)

google/cloud/language_v1/services/language_service/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,5 +413,8 @@ def annotate_text(
413413
)
414414
return self._stubs["annotate_text"]
415415

416+
def close(self):
417+
return self.grpc_channel.close()
418+
416419

417420
__all__ = ("LanguageServiceGrpcAsyncIOTransport",)

google/cloud/language_v1/types/language_service.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class Type(proto.Enum):
101101

102102
class Sentence(proto.Message):
103103
r"""Represents a sentence in the input document.
104+
104105
Attributes:
105106
text (google.cloud.language_v1.types.TextSpan):
106107
The sentence text.
@@ -182,6 +183,7 @@ class Type(proto.Enum):
182183

183184
class Token(proto.Message):
184185
r"""Represents the smallest syntactic building block of the text.
186+
185187
Attributes:
186188
text (google.cloud.language_v1.types.TextSpan):
187189
The token text.
@@ -538,6 +540,7 @@ class Type(proto.Enum):
538540

539541
class TextSpan(proto.Message):
540542
r"""Represents an output piece of text.
543+
541544
Attributes:
542545
content (str):
543546
The content of the output text.
@@ -554,6 +557,7 @@ class TextSpan(proto.Message):
554557

555558
class ClassificationCategory(proto.Message):
556559
r"""Represents a category returned from the text classifier.
560+
557561
Attributes:
558562
name (str):
559563
The name of the category representing the document, from the
@@ -571,6 +575,7 @@ class ClassificationCategory(proto.Message):
571575

572576
class AnalyzeSentimentRequest(proto.Message):
573577
r"""The sentiment analysis request message.
578+
574579
Attributes:
575580
document (google.cloud.language_v1.types.Document):
576581
Input document.
@@ -585,6 +590,7 @@ class AnalyzeSentimentRequest(proto.Message):
585590

586591
class AnalyzeSentimentResponse(proto.Message):
587592
r"""The sentiment analysis response message.
593+
588594
Attributes:
589595
document_sentiment (google.cloud.language_v1.types.Sentiment):
590596
The overall sentiment of the input document.
@@ -606,6 +612,7 @@ class AnalyzeSentimentResponse(proto.Message):
606612

607613
class AnalyzeEntitySentimentRequest(proto.Message):
608614
r"""The entity-level sentiment analysis request message.
615+
609616
Attributes:
610617
document (google.cloud.language_v1.types.Document):
611618
Input document.
@@ -620,6 +627,7 @@ class AnalyzeEntitySentimentRequest(proto.Message):
620627

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

639647
class AnalyzeEntitiesRequest(proto.Message):
640648
r"""The entity analysis request message.
649+
641650
Attributes:
642651
document (google.cloud.language_v1.types.Document):
643652
Input document.
@@ -652,6 +661,7 @@ class AnalyzeEntitiesRequest(proto.Message):
652661

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

671681
class AnalyzeSyntaxRequest(proto.Message):
672682
r"""The syntax analysis request message.
683+
673684
Attributes:
674685
document (google.cloud.language_v1.types.Document):
675686
Input document.
@@ -684,6 +695,7 @@ class AnalyzeSyntaxRequest(proto.Message):
684695

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

706718
class ClassifyTextRequest(proto.Message):
707719
r"""The document classification request message.
720+
708721
Attributes:
709722
document (google.cloud.language_v1.types.Document):
710723
Input document.
@@ -715,6 +728,7 @@ class ClassifyTextRequest(proto.Message):
715728

716729
class ClassifyTextResponse(proto.Message):
717730
r"""The document classification response message.
731+
718732
Attributes:
719733
categories (Sequence[google.cloud.language_v1.types.ClassificationCategory]):
720734
Categories representing the input document.
@@ -772,6 +786,7 @@ class Features(proto.Message):
772786

773787
class AnnotateTextResponse(proto.Message):
774788
r"""The text annotations response message.
789+
775790
Attributes:
776791
sentences (Sequence[google.cloud.language_v1.types.Sentence]):
777792
Sentences in the input document. Populated if the user

google/cloud/language_v1beta2/services/language_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,12 @@ async def annotate_text(
662662
# Done; return the response.
663663
return response
664664

665+
async def __aenter__(self):
666+
return self
667+
668+
async def __aexit__(self, exc_type, exc, tb):
669+
await self.transport.close()
670+
665671

666672
try:
667673
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/language_v1beta2/services/language_service/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 analyze_sentiment(
@@ -777,6 +774,19 @@ def annotate_text(
777774
# Done; return the response.
778775
return response
779776

777+
def __enter__(self):
778+
return self
779+
780+
def __exit__(self, type, value, traceback):
781+
"""Releases underlying transport's resources.
782+
783+
.. warning::
784+
ONLY use as a context manager if the transport is NOT shared
785+
with other clients! Exiting the with block will CLOSE the transport
786+
and may cause errors in other clients!
787+
"""
788+
self.transport.close()
789+
780790

781791
try:
782792
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/language_v1beta2/services/language_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,15 @@ def _prep_wrapped_messages(self, client_info):
247247
),
248248
}
249249

250+
def close(self):
251+
"""Closes resources associated with the transport.
252+
253+
.. warning::
254+
Only call this method if the transport is NOT shared
255+
with other clients - this may cause errors in other clients!
256+
"""
257+
raise NotImplementedError()
258+
250259
@property
251260
def analyze_sentiment(
252261
self,

google/cloud/language_v1beta2/services/language_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,5 +407,8 @@ def annotate_text(
407407
)
408408
return self._stubs["annotate_text"]
409409

410+
def close(self):
411+
self.grpc_channel.close()
412+
410413

411414
__all__ = ("LanguageServiceGrpcTransport",)

0 commit comments

Comments
 (0)