|
16 | 16 | from collections import OrderedDict
|
17 | 17 | import functools
|
18 | 18 | import re
|
19 |
| -from typing import Dict, Sequence, Tuple, Type, Union |
| 19 | +from typing import Dict, Optional, Sequence, Tuple, Type, Union |
20 | 20 | import pkg_resources
|
21 | 21 |
|
22 | 22 | from google.api_core.client_options import ClientOptions
|
@@ -125,6 +125,42 @@ def from_service_account_file(cls, filename: str, *args, **kwargs):
|
125 | 125 |
|
126 | 126 | from_service_account_json = from_service_account_file
|
127 | 127 |
|
| 128 | + @classmethod |
| 129 | + def get_mtls_endpoint_and_cert_source( |
| 130 | + cls, client_options: Optional[ClientOptions] = None |
| 131 | + ): |
| 132 | + """Return the API endpoint and client cert source for mutual TLS. |
| 133 | +
|
| 134 | + The client cert source is determined in the following order: |
| 135 | + (1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the |
| 136 | + client cert source is None. |
| 137 | + (2) if `client_options.client_cert_source` is provided, use the provided one; if the |
| 138 | + default client cert source exists, use the default one; otherwise the client cert |
| 139 | + source is None. |
| 140 | +
|
| 141 | + The API endpoint is determined in the following order: |
| 142 | + (1) if `client_options.api_endpoint` if provided, use the provided one. |
| 143 | + (2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the |
| 144 | + default mTLS endpoint; if the environment variabel is "never", use the default API |
| 145 | + endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise |
| 146 | + use the default API endpoint. |
| 147 | +
|
| 148 | + More details can be found at https://google.aip.dev/auth/4114. |
| 149 | +
|
| 150 | + Args: |
| 151 | + client_options (google.api_core.client_options.ClientOptions): Custom options for the |
| 152 | + client. Only the `api_endpoint` and `client_cert_source` properties may be used |
| 153 | + in this method. |
| 154 | +
|
| 155 | + Returns: |
| 156 | + Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the |
| 157 | + client cert source to use. |
| 158 | +
|
| 159 | + Raises: |
| 160 | + google.auth.exceptions.MutualTLSChannelError: If any errors happen. |
| 161 | + """ |
| 162 | + return DocumentProcessorServiceClient.get_mtls_endpoint_and_cert_source(client_options) # type: ignore |
| 163 | + |
128 | 164 | @property
|
129 | 165 | def transport(self) -> DocumentProcessorServiceTransport:
|
130 | 166 | """Returns the transport used by the client instance.
|
@@ -197,6 +233,34 @@ async def process_document(
|
197 | 233 | ) -> document_processor_service.ProcessResponse:
|
198 | 234 | r"""Processes a single document.
|
199 | 235 |
|
| 236 | + .. code-block:: |
| 237 | +
|
| 238 | + from google.cloud import documentai_v1 |
| 239 | +
|
| 240 | + def sample_process_document(): |
| 241 | + # Create a client |
| 242 | + client = documentai_v1.DocumentProcessorServiceClient() |
| 243 | +
|
| 244 | + # Initialize request argument(s) |
| 245 | + inline_document = documentai_v1.Document() |
| 246 | + inline_document.uri = "uri_value" |
| 247 | +
|
| 248 | + project = "my-project-id" |
| 249 | + location = "us-central1" |
| 250 | + processor = "processor_value" |
| 251 | + name = f"projects/{project}/locations/{location}/processors/{processor}" |
| 252 | +
|
| 253 | + request = documentai_v1.ProcessRequest( |
| 254 | + inline_document=inline_document, |
| 255 | + name=name, |
| 256 | + ) |
| 257 | +
|
| 258 | + # Make the request |
| 259 | + response = client.process_document(request=request) |
| 260 | +
|
| 261 | + # Handle response |
| 262 | + print(response) |
| 263 | +
|
200 | 264 | Args:
|
201 | 265 | request (Union[google.cloud.documentai_v1.types.ProcessRequest, dict]):
|
202 | 266 | The request object. Request message for the process
|
@@ -279,6 +343,33 @@ async def batch_process_documents(
|
279 | 343 | r"""LRO endpoint to batch process many documents. The output is
|
280 | 344 | written to Cloud Storage as JSON in the [Document] format.
|
281 | 345 |
|
| 346 | +
|
| 347 | + .. code-block:: |
| 348 | +
|
| 349 | + from google.cloud import documentai_v1 |
| 350 | +
|
| 351 | + def sample_batch_process_documents(): |
| 352 | + # Create a client |
| 353 | + client = documentai_v1.DocumentProcessorServiceClient() |
| 354 | +
|
| 355 | + # Initialize request argument(s) |
| 356 | + project = "my-project-id" |
| 357 | + location = "us-central1" |
| 358 | + processor = "processor_value" |
| 359 | + name = f"projects/{project}/locations/{location}/processors/{processor}" |
| 360 | +
|
| 361 | + request = documentai_v1.BatchProcessRequest( |
| 362 | + name=name, |
| 363 | + ) |
| 364 | +
|
| 365 | + # Make the request |
| 366 | + operation = client.batch_process_documents(request=request) |
| 367 | +
|
| 368 | + print("Waiting for operation to complete...") |
| 369 | +
|
| 370 | + response = operation.result() |
| 371 | + print(response) |
| 372 | +
|
282 | 373 | Args:
|
283 | 374 | request (Union[google.cloud.documentai_v1.types.BatchProcessRequest, dict]):
|
284 | 375 | The request object. Request message for batch process
|
@@ -372,6 +463,37 @@ async def review_document(
|
372 | 463 | r"""Send a document for Human Review. The input document
|
373 | 464 | should be processed by the specified processor.
|
374 | 465 |
|
| 466 | +
|
| 467 | + .. code-block:: |
| 468 | +
|
| 469 | + from google.cloud import documentai_v1 |
| 470 | +
|
| 471 | + def sample_review_document(): |
| 472 | + # Create a client |
| 473 | + client = documentai_v1.DocumentProcessorServiceClient() |
| 474 | +
|
| 475 | + # Initialize request argument(s) |
| 476 | + inline_document = documentai_v1.Document() |
| 477 | + inline_document.uri = "uri_value" |
| 478 | +
|
| 479 | + project = "my-project-id" |
| 480 | + location = "us-central1" |
| 481 | + processor = "processor_value" |
| 482 | + human_review_config = f"projects/{project}/locations/{location}/processors/{processor}/humanReviewConfig" |
| 483 | +
|
| 484 | + request = documentai_v1.ReviewDocumentRequest( |
| 485 | + inline_document=inline_document, |
| 486 | + human_review_config=human_review_config, |
| 487 | + ) |
| 488 | +
|
| 489 | + # Make the request |
| 490 | + operation = client.review_document(request=request) |
| 491 | +
|
| 492 | + print("Waiting for operation to complete...") |
| 493 | +
|
| 494 | + response = operation.result() |
| 495 | + print(response) |
| 496 | +
|
375 | 497 | Args:
|
376 | 498 | request (Union[google.cloud.documentai_v1.types.ReviewDocumentRequest, dict]):
|
377 | 499 | The request object. Request message for review document
|
|
0 commit comments