|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright 2020 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +from collections import OrderedDict |
| 19 | +import functools |
| 20 | +import re |
| 21 | +from typing import Dict, Sequence, Tuple, Type, Union |
| 22 | +import pkg_resources |
| 23 | + |
| 24 | +import google.api_core.client_options as ClientOptions # type: ignore |
| 25 | +from google.api_core import exceptions # type: ignore |
| 26 | +from google.api_core import gapic_v1 # type: ignore |
| 27 | +from google.api_core import retry as retries # type: ignore |
| 28 | +from google.auth import credentials # type: ignore |
| 29 | +from google.oauth2 import service_account # type: ignore |
| 30 | + |
| 31 | +from google.api_core import operation |
| 32 | +from google.api_core import operation_async |
| 33 | +from google.cloud.documentai_v1beta2.types import document |
| 34 | +from google.cloud.documentai_v1beta2.types import document_understanding |
| 35 | +from google.rpc import status_pb2 as status # type: ignore |
| 36 | + |
| 37 | +from .transports.base import DocumentUnderstandingServiceTransport |
| 38 | +from .transports.grpc_asyncio import DocumentUnderstandingServiceGrpcAsyncIOTransport |
| 39 | +from .client import DocumentUnderstandingServiceClient |
| 40 | + |
| 41 | + |
| 42 | +class DocumentUnderstandingServiceAsyncClient: |
| 43 | + """Service to parse structured information from unstructured or |
| 44 | + semi-structured documents using state-of-the-art Google AI such |
| 45 | + as natural language, computer vision, and translation. |
| 46 | + """ |
| 47 | + |
| 48 | + _client: DocumentUnderstandingServiceClient |
| 49 | + |
| 50 | + DEFAULT_ENDPOINT = DocumentUnderstandingServiceClient.DEFAULT_ENDPOINT |
| 51 | + DEFAULT_MTLS_ENDPOINT = DocumentUnderstandingServiceClient.DEFAULT_MTLS_ENDPOINT |
| 52 | + |
| 53 | + from_service_account_file = ( |
| 54 | + DocumentUnderstandingServiceClient.from_service_account_file |
| 55 | + ) |
| 56 | + from_service_account_json = from_service_account_file |
| 57 | + |
| 58 | + get_transport_class = functools.partial( |
| 59 | + type(DocumentUnderstandingServiceClient).get_transport_class, |
| 60 | + type(DocumentUnderstandingServiceClient), |
| 61 | + ) |
| 62 | + |
| 63 | + def __init__( |
| 64 | + self, |
| 65 | + *, |
| 66 | + credentials: credentials.Credentials = None, |
| 67 | + transport: Union[str, DocumentUnderstandingServiceTransport] = "grpc_asyncio", |
| 68 | + client_options: ClientOptions = None, |
| 69 | + ) -> None: |
| 70 | + """Instantiate the document understanding service client. |
| 71 | +
|
| 72 | + Args: |
| 73 | + credentials (Optional[google.auth.credentials.Credentials]): The |
| 74 | + authorization credentials to attach to requests. These |
| 75 | + credentials identify the application to the service; if none |
| 76 | + are specified, the client will attempt to ascertain the |
| 77 | + credentials from the environment. |
| 78 | + transport (Union[str, ~.DocumentUnderstandingServiceTransport]): The |
| 79 | + transport to use. If set to None, a transport is chosen |
| 80 | + automatically. |
| 81 | + client_options (ClientOptions): Custom options for the client. It |
| 82 | + won't take effect if a ``transport`` instance is provided. |
| 83 | + (1) The ``api_endpoint`` property can be used to override the |
| 84 | + default endpoint provided by the client. GOOGLE_API_USE_MTLS |
| 85 | + environment variable can also be used to override the endpoint: |
| 86 | + "always" (always use the default mTLS endpoint), "never" (always |
| 87 | + use the default regular endpoint, this is the default value for |
| 88 | + the environment variable) and "auto" (auto switch to the default |
| 89 | + mTLS endpoint if client SSL credentials is present). However, |
| 90 | + the ``api_endpoint`` property takes precedence if provided. |
| 91 | + (2) The ``client_cert_source`` property is used to provide client |
| 92 | + SSL credentials for mutual TLS transport. If not provided, the |
| 93 | + default SSL credentials will be used if present. |
| 94 | +
|
| 95 | + Raises: |
| 96 | + google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport |
| 97 | + creation failed for any reason. |
| 98 | + """ |
| 99 | + |
| 100 | + self._client = DocumentUnderstandingServiceClient( |
| 101 | + credentials=credentials, transport=transport, client_options=client_options |
| 102 | + ) |
| 103 | + |
| 104 | + async def batch_process_documents( |
| 105 | + self, |
| 106 | + request: document_understanding.BatchProcessDocumentsRequest = None, |
| 107 | + *, |
| 108 | + requests: Sequence[document_understanding.ProcessDocumentRequest] = None, |
| 109 | + retry: retries.Retry = gapic_v1.method.DEFAULT, |
| 110 | + timeout: float = None, |
| 111 | + metadata: Sequence[Tuple[str, str]] = (), |
| 112 | + ) -> operation_async.AsyncOperation: |
| 113 | + r"""LRO endpoint to batch process many documents. The output is |
| 114 | + written to Cloud Storage as JSON in the [Document] format. |
| 115 | +
|
| 116 | + Args: |
| 117 | + request (:class:`~.document_understanding.BatchProcessDocumentsRequest`): |
| 118 | + The request object. Request to batch process documents |
| 119 | + as an asynchronous operation. The output is written to |
| 120 | + Cloud Storage as JSON in the [Document] format. |
| 121 | + requests (:class:`Sequence[~.document_understanding.ProcessDocumentRequest]`): |
| 122 | + Required. Individual requests for |
| 123 | + each document. |
| 124 | + This corresponds to the ``requests`` field |
| 125 | + on the ``request`` instance; if ``request`` is provided, this |
| 126 | + should not be set. |
| 127 | +
|
| 128 | + retry (google.api_core.retry.Retry): Designation of what errors, if any, |
| 129 | + should be retried. |
| 130 | + timeout (float): The timeout for this request. |
| 131 | + metadata (Sequence[Tuple[str, str]]): Strings which should be |
| 132 | + sent along with the request as metadata. |
| 133 | +
|
| 134 | + Returns: |
| 135 | + ~.operation_async.AsyncOperation: |
| 136 | + An object representing a long-running operation. |
| 137 | +
|
| 138 | + The result type for the operation will be |
| 139 | + :class:``~.document_understanding.BatchProcessDocumentsResponse``: |
| 140 | + Response to an batch document processing request. This |
| 141 | + is returned in the LRO Operation after the operation is |
| 142 | + complete. |
| 143 | +
|
| 144 | + """ |
| 145 | + # Create or coerce a protobuf request object. |
| 146 | + # Sanity check: If we got a request object, we should *not* have |
| 147 | + # gotten any keyword arguments that map to the request. |
| 148 | + if request is not None and any([requests]): |
| 149 | + raise ValueError( |
| 150 | + "If the `request` argument is set, then none of " |
| 151 | + "the individual field arguments should be set." |
| 152 | + ) |
| 153 | + |
| 154 | + request = document_understanding.BatchProcessDocumentsRequest(request) |
| 155 | + |
| 156 | + # If we have keyword arguments corresponding to fields on the |
| 157 | + # request, apply these. |
| 158 | + |
| 159 | + if requests is not None: |
| 160 | + request.requests = requests |
| 161 | + |
| 162 | + # Wrap the RPC method; this adds retry and timeout information, |
| 163 | + # and friendly error handling. |
| 164 | + rpc = gapic_v1.method_async.wrap_method( |
| 165 | + self._client._transport.batch_process_documents, |
| 166 | + default_timeout=None, |
| 167 | + client_info=_client_info, |
| 168 | + ) |
| 169 | + |
| 170 | + # Certain fields should be provided within the metadata header; |
| 171 | + # add these here. |
| 172 | + metadata = tuple(metadata) + ( |
| 173 | + gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), |
| 174 | + ) |
| 175 | + |
| 176 | + # Send the request. |
| 177 | + response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata) |
| 178 | + |
| 179 | + # Wrap the response in an operation future. |
| 180 | + response = operation_async.from_gapic( |
| 181 | + response, |
| 182 | + self._client._transport.operations_client, |
| 183 | + document_understanding.BatchProcessDocumentsResponse, |
| 184 | + metadata_type=document_understanding.OperationMetadata, |
| 185 | + ) |
| 186 | + |
| 187 | + # Done; return the response. |
| 188 | + return response |
| 189 | + |
| 190 | + async def process_document( |
| 191 | + self, |
| 192 | + request: document_understanding.ProcessDocumentRequest = None, |
| 193 | + *, |
| 194 | + retry: retries.Retry = gapic_v1.method.DEFAULT, |
| 195 | + timeout: float = None, |
| 196 | + metadata: Sequence[Tuple[str, str]] = (), |
| 197 | + ) -> document.Document: |
| 198 | + r"""Processes a single document. |
| 199 | +
|
| 200 | + Args: |
| 201 | + request (:class:`~.document_understanding.ProcessDocumentRequest`): |
| 202 | + The request object. Request to process one document. |
| 203 | +
|
| 204 | + retry (google.api_core.retry.Retry): Designation of what errors, if any, |
| 205 | + should be retried. |
| 206 | + timeout (float): The timeout for this request. |
| 207 | + metadata (Sequence[Tuple[str, str]]): Strings which should be |
| 208 | + sent along with the request as metadata. |
| 209 | +
|
| 210 | + Returns: |
| 211 | + ~.document.Document: |
| 212 | + Document represents the canonical |
| 213 | + document resource in Document |
| 214 | + Understanding AI. It is an interchange |
| 215 | + format that provides insights into |
| 216 | + documents and allows for collaboration |
| 217 | + between users and Document Understanding |
| 218 | + AI to iterate and optimize for quality. |
| 219 | +
|
| 220 | + """ |
| 221 | + # Create or coerce a protobuf request object. |
| 222 | + |
| 223 | + request = document_understanding.ProcessDocumentRequest(request) |
| 224 | + |
| 225 | + # Wrap the RPC method; this adds retry and timeout information, |
| 226 | + # and friendly error handling. |
| 227 | + rpc = gapic_v1.method_async.wrap_method( |
| 228 | + self._client._transport.process_document, |
| 229 | + default_timeout=None, |
| 230 | + client_info=_client_info, |
| 231 | + ) |
| 232 | + |
| 233 | + # Certain fields should be provided within the metadata header; |
| 234 | + # add these here. |
| 235 | + metadata = tuple(metadata) + ( |
| 236 | + gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), |
| 237 | + ) |
| 238 | + |
| 239 | + # Send the request. |
| 240 | + response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata) |
| 241 | + |
| 242 | + # Done; return the response. |
| 243 | + return response |
| 244 | + |
| 245 | + |
| 246 | +try: |
| 247 | + _client_info = gapic_v1.client_info.ClientInfo( |
| 248 | + gapic_version=pkg_resources.get_distribution("google-cloud-documentai").version |
| 249 | + ) |
| 250 | +except pkg_resources.DistributionNotFound: |
| 251 | + _client_info = gapic_v1.client_info.ClientInfo() |
| 252 | + |
| 253 | + |
| 254 | +__all__ = ("DocumentUnderstandingServiceAsyncClient",) |
0 commit comments