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

Commit 8b65652

Browse files
feat: add context manager support in client (#224)
- [ ] 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 63a8d7c commit 8b65652

31 files changed

Lines changed: 348 additions & 16 deletions

File tree

google/cloud/documentai_v1/services/document_processor_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,12 @@ async def review_document(
451451
# Done; return the response.
452452
return response
453453

454+
async def __aenter__(self):
455+
return self
456+
457+
async def __aexit__(self, exc_type, exc, tb):
458+
await self.transport.close()
459+
454460

455461
try:
456462
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/documentai_v1/services/document_processor_service/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,7 @@ def __init__(
369369
client_cert_source_for_mtls=client_cert_source_func,
370370
quota_project_id=client_options.quota_project_id,
371371
client_info=client_info,
372-
always_use_jwt_access=(
373-
Transport == type(self).get_transport_class("grpc")
374-
or Transport == type(self).get_transport_class("grpc_asyncio")
375-
),
372+
always_use_jwt_access=True,
376373
)
377374

378375
def process_document(
@@ -615,6 +612,19 @@ def review_document(
615612
# Done; return the response.
616613
return response
617614

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

619629
try:
620630
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/documentai_v1/services/document_processor_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,15 @@ def _prep_wrapped_messages(self, client_info):
203203
),
204204
}
205205

206+
def close(self):
207+
"""Closes resources associated with the transport.
208+
209+
.. warning::
210+
Only call this method if the transport is NOT shared
211+
with other clients - this may cause errors in other clients!
212+
"""
213+
raise NotImplementedError()
214+
206215
@property
207216
def operations_client(self) -> operations_v1.OperationsClient:
208217
"""Return the client designed to process long-running operations."""

google/cloud/documentai_v1/services/document_processor_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,5 +333,8 @@ def review_document(
333333
)
334334
return self._stubs["review_document"]
335335

336+
def close(self):
337+
self.grpc_channel.close()
338+
336339

337340
__all__ = ("DocumentProcessorServiceGrpcTransport",)

google/cloud/documentai_v1/services/document_processor_service/transports/grpc_asyncio.py

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

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

344347
__all__ = ("DocumentProcessorServiceGrpcAsyncIOTransport",)

google/cloud/documentai_v1/types/document.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ class Style(proto.Message):
135135

136136
class FontSize(proto.Message):
137137
r"""Font size with unit.
138+
138139
Attributes:
139140
size (float):
140141
Font size for the text.
@@ -162,6 +163,7 @@ class FontSize(proto.Message):
162163

163164
class Page(proto.Message):
164165
r"""A page in a [Document][google.cloud.documentai.v1.Document].
166+
165167
Attributes:
166168
page_number (int):
167169
1-based index for current
@@ -218,6 +220,7 @@ class Page(proto.Message):
218220

219221
class Dimension(proto.Message):
220222
r"""Dimension for the page.
223+
221224
Attributes:
222225
width (float):
223226
Page width.
@@ -233,6 +236,7 @@ class Dimension(proto.Message):
233236

234237
class Image(proto.Message):
235238
r"""Rendered image contents for this page.
239+
236240
Attributes:
237241
content (bytes):
238242
Raw byte content of the image.
@@ -275,6 +279,7 @@ class Matrix(proto.Message):
275279

276280
class Layout(proto.Message):
277281
r"""Visual element describing a layout unit on a page.
282+
278283
Attributes:
279284
text_anchor (google.cloud.documentai_v1.types.Document.TextAnchor):
280285
Text anchor indexing into the
@@ -391,6 +396,7 @@ class Line(proto.Message):
391396

392397
class Token(proto.Message):
393398
r"""A detected token.
399+
394400
Attributes:
395401
layout (google.cloud.documentai_v1.types.Document.Page.Layout):
396402
[Layout][google.cloud.documentai.v1.Document.Page.Layout]
@@ -465,6 +471,7 @@ class VisualElement(proto.Message):
465471

466472
class Table(proto.Message):
467473
r"""A table representation similar to HTML table structure.
474+
468475
Attributes:
469476
layout (google.cloud.documentai_v1.types.Document.Page.Layout):
470477
[Layout][google.cloud.documentai.v1.Document.Page.Layout]
@@ -480,6 +487,7 @@ class Table(proto.Message):
480487

481488
class TableRow(proto.Message):
482489
r"""A row of table cells.
490+
483491
Attributes:
484492
cells (Sequence[google.cloud.documentai_v1.types.Document.Page.Table.TableCell]):
485493
Cells that make up this row.
@@ -491,6 +499,7 @@ class TableRow(proto.Message):
491499

492500
class TableCell(proto.Message):
493501
r"""A cell representation inside the table.
502+
494503
Attributes:
495504
layout (google.cloud.documentai_v1.types.Document.Page.Layout):
496505
[Layout][google.cloud.documentai.v1.Document.Page.Layout]
@@ -529,6 +538,7 @@ class TableCell(proto.Message):
529538

530539
class FormField(proto.Message):
531540
r"""A form field detected on the page.
541+
532542
Attributes:
533543
field_name (google.cloud.documentai_v1.types.Document.Page.Layout):
534544
[Layout][google.cloud.documentai.v1.Document.Page.Layout]
@@ -577,6 +587,7 @@ class FormField(proto.Message):
577587

578588
class DetectedLanguage(proto.Message):
579589
r"""Detected language for a structural component.
590+
580591
Attributes:
581592
language_code (str):
582593
The BCP-47 language code, such as "en-US" or "sr-Latn". For
@@ -671,6 +682,7 @@ class Entity(proto.Message):
671682

672683
class NormalizedValue(proto.Message):
673684
r"""Parsed and normalized entity value.
685+
674686
Attributes:
675687
money_value (google.type.money_pb2.Money):
676688
Money value. See also:
@@ -934,6 +946,7 @@ class Parent(proto.Message):
934946

935947
class Revision(proto.Message):
936948
r"""Contains past or forward revisions of this document.
949+
937950
Attributes:
938951
agent (str):
939952
If the change was made by a person specify
@@ -956,6 +969,7 @@ class Revision(proto.Message):
956969

957970
class HumanReview(proto.Message):
958971
r"""Human Review information of the document.
972+
959973
Attributes:
960974
state (str):
961975
Human review state. e.g. ``requested``, ``succeeded``,
@@ -982,6 +996,7 @@ class HumanReview(proto.Message):
982996

983997
class TextChange(proto.Message):
984998
r"""This message is used for text changes aka. OCR corrections.
999+
9851000
Attributes:
9861001
text_anchor (google.cloud.documentai_v1.types.Document.TextAnchor):
9871002
Provenance of the correction. Text anchor indexing into the

google/cloud/documentai_v1/types/document_io.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
class RawDocument(proto.Message):
3333
r"""Payload message of raw document content (bytes).
34+
3435
Attributes:
3536
content (bytes):
3637
Inline document content.
@@ -45,6 +46,7 @@ class RawDocument(proto.Message):
4546

4647
class GcsDocument(proto.Message):
4748
r"""Specifies a document stored on Cloud Storage.
49+
4850
Attributes:
4951
gcs_uri (str):
5052
The Cloud Storage object uri.
@@ -58,6 +60,7 @@ class GcsDocument(proto.Message):
5860

5961
class GcsDocuments(proto.Message):
6062
r"""Specifies a set of documents on Cloud Storage.
63+
6164
Attributes:
6265
documents (Sequence[google.cloud.documentai_v1.types.GcsDocument]):
6366
The list of documents.
@@ -111,6 +114,7 @@ class DocumentOutputConfig(proto.Message):
111114

112115
class GcsOutputConfig(proto.Message):
113116
r"""The configuration used when outputting documents.
117+
114118
Attributes:
115119
gcs_uri (str):
116120
The Cloud Storage uri (a directory) of the

google/cloud/documentai_v1/types/document_processor_service.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
class ProcessRequest(proto.Message):
4242
r"""Request message for the process document method.
43+
4344
Attributes:
4445
inline_document (google.cloud.documentai_v1.types.Document):
4546
An inline document proto.
@@ -64,6 +65,7 @@ class ProcessRequest(proto.Message):
6465

6566
class HumanReviewStatus(proto.Message):
6667
r"""The status of human review on a processed document.
68+
6769
Attributes:
6870
state (google.cloud.documentai_v1.types.HumanReviewStatus.State):
6971
The state of human review on the processing
@@ -94,6 +96,7 @@ class State(proto.Enum):
9496

9597
class ProcessResponse(proto.Message):
9698
r"""Response message for the process document method.
99+
97100
Attributes:
98101
document (google.cloud.documentai_v1.types.Document):
99102
The document payload, will populate fields
@@ -111,6 +114,7 @@ class ProcessResponse(proto.Message):
111114

112115
class BatchProcessRequest(proto.Message):
113116
r"""Request message for batch process document method.
117+
114118
Attributes:
115119
name (str):
116120
Required. The processor resource name.
@@ -134,11 +138,13 @@ class BatchProcessRequest(proto.Message):
134138

135139

136140
class BatchProcessResponse(proto.Message):
137-
r"""Response message for batch process document method. """
141+
r"""Response message for batch process document method.
142+
"""
138143

139144

140145
class BatchProcessMetadata(proto.Message):
141146
r"""The long running operation metadata for batch process method.
147+
142148
Attributes:
143149
state (google.cloud.documentai_v1.types.BatchProcessMetadata.State):
144150
The state of the current batch processing.
@@ -205,6 +211,7 @@ class IndividualProcessStatus(proto.Message):
205211

206212
class ReviewDocumentRequest(proto.Message):
207213
r"""Request message for review document method.
214+
208215
Attributes:
209216
inline_document (google.cloud.documentai_v1.types.Document):
210217
An inline document proto.
@@ -234,6 +241,7 @@ class Priority(proto.Enum):
234241

235242
class ReviewDocumentResponse(proto.Message):
236243
r"""Response message for review document method.
244+
237245
Attributes:
238246
gcs_destination (str):
239247
The Cloud Storage uri for the human reviewed

google/cloud/documentai_v1/types/geometry.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class NormalizedVertex(proto.Message):
5858

5959
class BoundingPoly(proto.Message):
6060
r"""A bounding polygon for the detected image annotation.
61+
6162
Attributes:
6263
vertices (Sequence[google.cloud.documentai_v1.types.Vertex]):
6364
The bounding polygon vertices.

google/cloud/documentai_v1/types/operation_metadata.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
class CommonOperationMetadata(proto.Message):
2727
r"""The common metadata for long running operations.
28+
2829
Attributes:
2930
state (google.cloud.documentai_v1.types.CommonOperationMetadata.State):
3031
The state of the operation.

0 commit comments

Comments
 (0)