diff --git a/packages/google-cloud-documentai/docs/documentai_v1beta2/document_understanding_service.rst b/packages/google-cloud-documentai/docs/documentai_v1beta2/document_understanding_service.rst deleted file mode 100644 index a0d0da7e3c88..000000000000 --- a/packages/google-cloud-documentai/docs/documentai_v1beta2/document_understanding_service.rst +++ /dev/null @@ -1,6 +0,0 @@ -DocumentUnderstandingService ----------------------------------------------- - -.. automodule:: google.cloud.documentai_v1beta2.services.document_understanding_service - :members: - :inherited-members: diff --git a/packages/google-cloud-documentai/docs/documentai_v1beta2/services_.rst b/packages/google-cloud-documentai/docs/documentai_v1beta2/services_.rst deleted file mode 100644 index 13f4a238f1af..000000000000 --- a/packages/google-cloud-documentai/docs/documentai_v1beta2/services_.rst +++ /dev/null @@ -1,6 +0,0 @@ -Services for Google Cloud Documentai v1beta2 API -================================================ -.. toctree:: - :maxdepth: 2 - - document_understanding_service diff --git a/packages/google-cloud-documentai/docs/documentai_v1beta2/types_.rst b/packages/google-cloud-documentai/docs/documentai_v1beta2/types_.rst deleted file mode 100644 index 35540dd05728..000000000000 --- a/packages/google-cloud-documentai/docs/documentai_v1beta2/types_.rst +++ /dev/null @@ -1,6 +0,0 @@ -Types for Google Cloud Documentai v1beta2 API -============================================= - -.. automodule:: google.cloud.documentai_v1beta2.types - :members: - :show-inheritance: diff --git a/packages/google-cloud-documentai/docs/index.rst b/packages/google-cloud-documentai/docs/index.rst index 248f9703943d..41e3241db285 100644 --- a/packages/google-cloud-documentai/docs/index.rst +++ b/packages/google-cloud-documentai/docs/index.rst @@ -14,14 +14,6 @@ API Reference documentai_v1/services_ documentai_v1/types_ -API Reference -------------- -.. toctree:: - :maxdepth: 2 - - documentai_v1beta2/services_ - documentai_v1beta2/types_ - API Reference ------------- .. toctree:: diff --git a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/__init__.py b/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/__init__.py deleted file mode 100644 index 81aaf64e33b0..000000000000 --- a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/__init__.py +++ /dev/null @@ -1,71 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2024 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -from google.cloud.documentai_v1beta2 import gapic_version as package_version - -__version__ = package_version.__version__ - - -from .services.document_understanding_service import ( - DocumentUnderstandingServiceAsyncClient, - DocumentUnderstandingServiceClient, -) -from .types.barcode import Barcode -from .types.document import Document -from .types.document_understanding import ( - AutoMlParams, - BatchProcessDocumentsRequest, - BatchProcessDocumentsResponse, - EntityExtractionParams, - FormExtractionParams, - GcsDestination, - GcsSource, - InputConfig, - KeyValuePairHint, - OcrParams, - OperationMetadata, - OutputConfig, - ProcessDocumentRequest, - ProcessDocumentResponse, - TableBoundHint, - TableExtractionParams, -) -from .types.geometry import BoundingPoly, NormalizedVertex, Vertex - -__all__ = ( - "DocumentUnderstandingServiceAsyncClient", - "AutoMlParams", - "Barcode", - "BatchProcessDocumentsRequest", - "BatchProcessDocumentsResponse", - "BoundingPoly", - "Document", - "DocumentUnderstandingServiceClient", - "EntityExtractionParams", - "FormExtractionParams", - "GcsDestination", - "GcsSource", - "InputConfig", - "KeyValuePairHint", - "NormalizedVertex", - "OcrParams", - "OperationMetadata", - "OutputConfig", - "ProcessDocumentRequest", - "ProcessDocumentResponse", - "TableBoundHint", - "TableExtractionParams", - "Vertex", -) diff --git a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/gapic_metadata.json b/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/gapic_metadata.json deleted file mode 100644 index 633dcc19c472..000000000000 --- a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/gapic_metadata.json +++ /dev/null @@ -1,43 +0,0 @@ - { - "comment": "This file maps proto services/RPCs to the corresponding library clients/methods", - "language": "python", - "libraryPackage": "google.cloud.documentai_v1beta2", - "protoPackage": "google.cloud.documentai.v1beta2", - "schema": "1.0", - "services": { - "DocumentUnderstandingService": { - "clients": { - "grpc": { - "libraryClient": "DocumentUnderstandingServiceClient", - "rpcs": { - "BatchProcessDocuments": { - "methods": [ - "batch_process_documents" - ] - }, - "ProcessDocument": { - "methods": [ - "process_document" - ] - } - } - }, - "grpc-async": { - "libraryClient": "DocumentUnderstandingServiceAsyncClient", - "rpcs": { - "BatchProcessDocuments": { - "methods": [ - "batch_process_documents" - ] - }, - "ProcessDocument": { - "methods": [ - "process_document" - ] - } - } - } - } - } - } -} diff --git a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/gapic_version.py b/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/gapic_version.py deleted file mode 100644 index 1fc3ab9a2878..000000000000 --- a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/gapic_version.py +++ /dev/null @@ -1,16 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2024 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -__version__ = "2.35.0" # {x-release-please-version} diff --git a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/py.typed b/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/py.typed deleted file mode 100644 index 81b450017987..000000000000 --- a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/py.typed +++ /dev/null @@ -1,2 +0,0 @@ -# Marker file for PEP 561. -# The google-cloud-documentai package uses inline types. diff --git a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/__init__.py b/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/__init__.py deleted file mode 100644 index 8f6cf068242c..000000000000 --- a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/__init__.py +++ /dev/null @@ -1,15 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2024 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# diff --git a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/document_understanding_service/__init__.py b/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/document_understanding_service/__init__.py deleted file mode 100644 index 2ea2ca13b79d..000000000000 --- a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/document_understanding_service/__init__.py +++ /dev/null @@ -1,22 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2024 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -from .async_client import DocumentUnderstandingServiceAsyncClient -from .client import DocumentUnderstandingServiceClient - -__all__ = ( - "DocumentUnderstandingServiceClient", - "DocumentUnderstandingServiceAsyncClient", -) diff --git a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/document_understanding_service/async_client.py b/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/document_understanding_service/async_client.py deleted file mode 100644 index 4edfe2c34aa4..000000000000 --- a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/document_understanding_service/async_client.py +++ /dev/null @@ -1,795 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2024 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -from collections import OrderedDict -import re -from typing import ( - Callable, - Dict, - Mapping, - MutableMapping, - MutableSequence, - Optional, - Sequence, - Tuple, - Type, - Union, -) - -from google.api_core import exceptions as core_exceptions -from google.api_core import gapic_v1 -from google.api_core import retry_async as retries -from google.api_core.client_options import ClientOptions -from google.auth import credentials as ga_credentials # type: ignore -from google.oauth2 import service_account # type: ignore - -from google.cloud.documentai_v1beta2 import gapic_version as package_version - -try: - OptionalRetry = Union[retries.AsyncRetry, gapic_v1.method._MethodDefault, None] -except AttributeError: # pragma: NO COVER - OptionalRetry = Union[retries.AsyncRetry, object, None] # type: ignore - -from google.api_core import operation # type: ignore -from google.api_core import operation_async # type: ignore -from google.cloud.location import locations_pb2 # type: ignore -from google.longrunning import operations_pb2 # type: ignore -from google.rpc import status_pb2 # type: ignore - -from google.cloud.documentai_v1beta2.types import document, document_understanding - -from .client import DocumentUnderstandingServiceClient -from .transports.base import DEFAULT_CLIENT_INFO, DocumentUnderstandingServiceTransport -from .transports.grpc_asyncio import DocumentUnderstandingServiceGrpcAsyncIOTransport - - -class DocumentUnderstandingServiceAsyncClient: - """Service to parse structured information from unstructured or - semi-structured documents using state-of-the-art Google AI such - as natural language, computer vision, and translation. - """ - - _client: DocumentUnderstandingServiceClient - - # Copy defaults from the synchronous client for use here. - # Note: DEFAULT_ENDPOINT is deprecated. Use _DEFAULT_ENDPOINT_TEMPLATE instead. - DEFAULT_ENDPOINT = DocumentUnderstandingServiceClient.DEFAULT_ENDPOINT - DEFAULT_MTLS_ENDPOINT = DocumentUnderstandingServiceClient.DEFAULT_MTLS_ENDPOINT - _DEFAULT_ENDPOINT_TEMPLATE = ( - DocumentUnderstandingServiceClient._DEFAULT_ENDPOINT_TEMPLATE - ) - _DEFAULT_UNIVERSE = DocumentUnderstandingServiceClient._DEFAULT_UNIVERSE - - common_billing_account_path = staticmethod( - DocumentUnderstandingServiceClient.common_billing_account_path - ) - parse_common_billing_account_path = staticmethod( - DocumentUnderstandingServiceClient.parse_common_billing_account_path - ) - common_folder_path = staticmethod( - DocumentUnderstandingServiceClient.common_folder_path - ) - parse_common_folder_path = staticmethod( - DocumentUnderstandingServiceClient.parse_common_folder_path - ) - common_organization_path = staticmethod( - DocumentUnderstandingServiceClient.common_organization_path - ) - parse_common_organization_path = staticmethod( - DocumentUnderstandingServiceClient.parse_common_organization_path - ) - common_project_path = staticmethod( - DocumentUnderstandingServiceClient.common_project_path - ) - parse_common_project_path = staticmethod( - DocumentUnderstandingServiceClient.parse_common_project_path - ) - common_location_path = staticmethod( - DocumentUnderstandingServiceClient.common_location_path - ) - parse_common_location_path = staticmethod( - DocumentUnderstandingServiceClient.parse_common_location_path - ) - - @classmethod - def from_service_account_info(cls, info: dict, *args, **kwargs): - """Creates an instance of this client using the provided credentials - info. - - Args: - info (dict): The service account private key info. - args: Additional arguments to pass to the constructor. - kwargs: Additional arguments to pass to the constructor. - - Returns: - DocumentUnderstandingServiceAsyncClient: The constructed client. - """ - return DocumentUnderstandingServiceClient.from_service_account_info.__func__(DocumentUnderstandingServiceAsyncClient, info, *args, **kwargs) # type: ignore - - @classmethod - def from_service_account_file(cls, filename: str, *args, **kwargs): - """Creates an instance of this client using the provided credentials - file. - - Args: - filename (str): The path to the service account private key json - file. - args: Additional arguments to pass to the constructor. - kwargs: Additional arguments to pass to the constructor. - - Returns: - DocumentUnderstandingServiceAsyncClient: The constructed client. - """ - return DocumentUnderstandingServiceClient.from_service_account_file.__func__(DocumentUnderstandingServiceAsyncClient, filename, *args, **kwargs) # type: ignore - - from_service_account_json = from_service_account_file - - @classmethod - def get_mtls_endpoint_and_cert_source( - cls, client_options: Optional[ClientOptions] = None - ): - """Return the API endpoint and client cert source for mutual TLS. - - The client cert source is determined in the following order: - (1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the - client cert source is None. - (2) if `client_options.client_cert_source` is provided, use the provided one; if the - default client cert source exists, use the default one; otherwise the client cert - source is None. - - The API endpoint is determined in the following order: - (1) if `client_options.api_endpoint` if provided, use the provided one. - (2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the - default mTLS endpoint; if the environment variable is "never", use the default API - endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise - use the default API endpoint. - - More details can be found at https://google.aip.dev/auth/4114. - - Args: - client_options (google.api_core.client_options.ClientOptions): Custom options for the - client. Only the `api_endpoint` and `client_cert_source` properties may be used - in this method. - - Returns: - Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the - client cert source to use. - - Raises: - google.auth.exceptions.MutualTLSChannelError: If any errors happen. - """ - return DocumentUnderstandingServiceClient.get_mtls_endpoint_and_cert_source(client_options) # type: ignore - - @property - def transport(self) -> DocumentUnderstandingServiceTransport: - """Returns the transport used by the client instance. - - Returns: - DocumentUnderstandingServiceTransport: The transport used by the client instance. - """ - return self._client.transport - - @property - def api_endpoint(self): - """Return the API endpoint used by the client instance. - - Returns: - str: The API endpoint used by the client instance. - """ - return self._client._api_endpoint - - @property - def universe_domain(self) -> str: - """Return the universe domain used by the client instance. - - Returns: - str: The universe domain used - by the client instance. - """ - return self._client._universe_domain - - get_transport_class = DocumentUnderstandingServiceClient.get_transport_class - - def __init__( - self, - *, - credentials: Optional[ga_credentials.Credentials] = None, - transport: Optional[ - Union[ - str, - DocumentUnderstandingServiceTransport, - Callable[..., DocumentUnderstandingServiceTransport], - ] - ] = "grpc_asyncio", - client_options: Optional[ClientOptions] = None, - client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, - ) -> None: - """Instantiates the document understanding service async client. - - Args: - credentials (Optional[google.auth.credentials.Credentials]): The - authorization credentials to attach to requests. These - credentials identify the application to the service; if none - are specified, the client will attempt to ascertain the - credentials from the environment. - transport (Optional[Union[str,DocumentUnderstandingServiceTransport,Callable[..., DocumentUnderstandingServiceTransport]]]): - The transport to use, or a Callable that constructs and returns a new transport to use. - If a Callable is given, it will be called with the same set of initialization - arguments as used in the DocumentUnderstandingServiceTransport constructor. - If set to None, a transport is chosen automatically. - client_options (Optional[Union[google.api_core.client_options.ClientOptions, dict]]): - Custom options for the client. - - 1. The ``api_endpoint`` property can be used to override the - default endpoint provided by the client when ``transport`` is - not explicitly provided. Only if this property is not set and - ``transport`` was not explicitly provided, the endpoint is - determined by the GOOGLE_API_USE_MTLS_ENDPOINT environment - variable, which have one of the following values: - "always" (always use the default mTLS endpoint), "never" (always - use the default regular endpoint) and "auto" (auto-switch to the - default mTLS endpoint if client certificate is present; this is - the default value). - - 2. If the GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable - is "true", then the ``client_cert_source`` property can be used - to provide a client certificate for mTLS transport. If - not provided, the default SSL client certificate will be used if - present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not - set, no client certificate will be used. - - 3. The ``universe_domain`` property can be used to override the - default "googleapis.com" universe. Note that ``api_endpoint`` - property still takes precedence; and ``universe_domain`` is - currently not supported for mTLS. - - client_info (google.api_core.gapic_v1.client_info.ClientInfo): - The client info used to send a user-agent string along with - API requests. If ``None``, then default info will be used. - Generally, you only need to set this if you're developing - your own client library. - - Raises: - google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport - creation failed for any reason. - """ - self._client = DocumentUnderstandingServiceClient( - credentials=credentials, - transport=transport, - client_options=client_options, - client_info=client_info, - ) - - async def batch_process_documents( - self, - request: Optional[ - Union[document_understanding.BatchProcessDocumentsRequest, dict] - ] = None, - *, - requests: Optional[ - MutableSequence[document_understanding.ProcessDocumentRequest] - ] = None, - retry: OptionalRetry = gapic_v1.method.DEFAULT, - timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), - ) -> operation_async.AsyncOperation: - r"""LRO endpoint to batch process many documents. The output is - written to Cloud Storage as JSON in the [Document] format. - - .. code-block:: python - - # This snippet has been automatically generated and should be regarded as a - # code template only. - # It will require modifications to work: - # - It may require correct/in-range values for request initialization. - # - It may require specifying regional endpoints when creating the service - # client as shown in: - # https://googleapis.dev/python/google-api-core/latest/client_options.html - from google.cloud import documentai_v1beta2 - - async def sample_batch_process_documents(): - # Create a client - client = documentai_v1beta2.DocumentUnderstandingServiceAsyncClient() - - # Initialize request argument(s) - requests = documentai_v1beta2.ProcessDocumentRequest() - requests.input_config.gcs_source.uri = "uri_value" - requests.input_config.mime_type = "mime_type_value" - - request = documentai_v1beta2.BatchProcessDocumentsRequest( - requests=requests, - ) - - # Make the request - operation = client.batch_process_documents(request=request) - - print("Waiting for operation to complete...") - - response = (await operation).result() - - # Handle the response - print(response) - - Args: - request (Optional[Union[google.cloud.documentai_v1beta2.types.BatchProcessDocumentsRequest, dict]]): - The request object. Request to batch process documents as an asynchronous - operation. The output is written to Cloud Storage as - JSON in the [Document] format. - requests (:class:`MutableSequence[google.cloud.documentai_v1beta2.types.ProcessDocumentRequest]`): - Required. Individual requests for - each document. - - This corresponds to the ``requests`` field - on the ``request`` instance; if ``request`` is provided, this - should not be set. - retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, - should be retried. - timeout (float): The timeout for this request. - metadata (Sequence[Tuple[str, str]]): Strings which should be - sent along with the request as metadata. - - Returns: - google.api_core.operation_async.AsyncOperation: - An object representing a long-running operation. - - The result type for the operation will be :class:`google.cloud.documentai_v1beta2.types.BatchProcessDocumentsResponse` Response to an batch document processing request. This is returned in - the LRO Operation after the operation is complete. - - """ - # Create or coerce a protobuf request object. - # - Quick check: If we got a request object, we should *not* have - # gotten any keyword arguments that map to the request. - has_flattened_params = any([requests]) - if request is not None and has_flattened_params: - raise ValueError( - "If the `request` argument is set, then none of " - "the individual field arguments should be set." - ) - - # - Use the request object if provided (there's no risk of modifying the input as - # there are no flattened fields), or create one. - if not isinstance(request, document_understanding.BatchProcessDocumentsRequest): - request = document_understanding.BatchProcessDocumentsRequest(request) - - # If we have keyword arguments corresponding to fields on the - # request, apply these. - if requests: - request.requests.extend(requests) - - # Wrap the RPC method; this adds retry and timeout information, - # and friendly error handling. - rpc = self._client._transport._wrapped_methods[ - self._client._transport.batch_process_documents - ] - - # Certain fields should be provided within the metadata header; - # add these here. - metadata = tuple(metadata) + ( - gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), - ) - - # Validate the universe domain. - self._client._validate_universe_domain() - - # Send the request. - response = await rpc( - request, - retry=retry, - timeout=timeout, - metadata=metadata, - ) - - # Wrap the response in an operation future. - response = operation_async.from_gapic( - response, - self._client._transport.operations_client, - document_understanding.BatchProcessDocumentsResponse, - metadata_type=document_understanding.OperationMetadata, - ) - - # Done; return the response. - return response - - async def process_document( - self, - request: Optional[ - Union[document_understanding.ProcessDocumentRequest, dict] - ] = None, - *, - retry: OptionalRetry = gapic_v1.method.DEFAULT, - timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), - ) -> document.Document: - r"""Processes a single document. - - .. code-block:: python - - # This snippet has been automatically generated and should be regarded as a - # code template only. - # It will require modifications to work: - # - It may require correct/in-range values for request initialization. - # - It may require specifying regional endpoints when creating the service - # client as shown in: - # https://googleapis.dev/python/google-api-core/latest/client_options.html - from google.cloud import documentai_v1beta2 - - async def sample_process_document(): - # Create a client - client = documentai_v1beta2.DocumentUnderstandingServiceAsyncClient() - - # Initialize request argument(s) - input_config = documentai_v1beta2.InputConfig() - input_config.gcs_source.uri = "uri_value" - input_config.mime_type = "mime_type_value" - - request = documentai_v1beta2.ProcessDocumentRequest( - input_config=input_config, - ) - - # Make the request - response = await client.process_document(request=request) - - # Handle the response - print(response) - - Args: - request (Optional[Union[google.cloud.documentai_v1beta2.types.ProcessDocumentRequest, dict]]): - The request object. Request to process one document. - retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any, - should be retried. - timeout (float): The timeout for this request. - metadata (Sequence[Tuple[str, str]]): Strings which should be - sent along with the request as metadata. - - Returns: - google.cloud.documentai_v1beta2.types.Document: - Document represents the canonical - document resource in Document AI. It is - an interchange format that provides - insights into documents and allows for - collaboration between users and Document - AI to iterate and optimize for quality. - - """ - # Create or coerce a protobuf request object. - # - Use the request object if provided (there's no risk of modifying the input as - # there are no flattened fields), or create one. - if not isinstance(request, document_understanding.ProcessDocumentRequest): - request = document_understanding.ProcessDocumentRequest(request) - - # Wrap the RPC method; this adds retry and timeout information, - # and friendly error handling. - rpc = self._client._transport._wrapped_methods[ - self._client._transport.process_document - ] - - # Certain fields should be provided within the metadata header; - # add these here. - metadata = tuple(metadata) + ( - gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), - ) - - # Validate the universe domain. - self._client._validate_universe_domain() - - # Send the request. - response = await rpc( - request, - retry=retry, - timeout=timeout, - metadata=metadata, - ) - - # Done; return the response. - return response - - async def list_operations( - self, - request: Optional[operations_pb2.ListOperationsRequest] = None, - *, - retry: OptionalRetry = gapic_v1.method.DEFAULT, - timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), - ) -> operations_pb2.ListOperationsResponse: - r"""Lists operations that match the specified filter in the request. - - Args: - request (:class:`~.operations_pb2.ListOperationsRequest`): - The request object. Request message for - `ListOperations` method. - retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, - if any, should be retried. - timeout (float): The timeout for this request. - metadata (Sequence[Tuple[str, str]]): Strings which should be - sent along with the request as metadata. - Returns: - ~.operations_pb2.ListOperationsResponse: - Response message for ``ListOperations`` method. - """ - # Create or coerce a protobuf request object. - # The request isn't a proto-plus wrapped type, - # so it must be constructed via keyword expansion. - if isinstance(request, dict): - request = operations_pb2.ListOperationsRequest(**request) - - # Wrap the RPC method; this adds retry and timeout information, - # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.list_operations, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) - - # Certain fields should be provided within the metadata header; - # add these here. - metadata = tuple(metadata) + ( - gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), - ) - - # Validate the universe domain. - self._client._validate_universe_domain() - - # Send the request. - response = await rpc( - request, - retry=retry, - timeout=timeout, - metadata=metadata, - ) - - # Done; return the response. - return response - - async def get_operation( - self, - request: Optional[operations_pb2.GetOperationRequest] = None, - *, - retry: OptionalRetry = gapic_v1.method.DEFAULT, - timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), - ) -> operations_pb2.Operation: - r"""Gets the latest state of a long-running operation. - - Args: - request (:class:`~.operations_pb2.GetOperationRequest`): - The request object. Request message for - `GetOperation` method. - retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, - if any, should be retried. - timeout (float): The timeout for this request. - metadata (Sequence[Tuple[str, str]]): Strings which should be - sent along with the request as metadata. - Returns: - ~.operations_pb2.Operation: - An ``Operation`` object. - """ - # Create or coerce a protobuf request object. - # The request isn't a proto-plus wrapped type, - # so it must be constructed via keyword expansion. - if isinstance(request, dict): - request = operations_pb2.GetOperationRequest(**request) - - # Wrap the RPC method; this adds retry and timeout information, - # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.get_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) - - # Certain fields should be provided within the metadata header; - # add these here. - metadata = tuple(metadata) + ( - gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), - ) - - # Validate the universe domain. - self._client._validate_universe_domain() - - # Send the request. - response = await rpc( - request, - retry=retry, - timeout=timeout, - metadata=metadata, - ) - - # Done; return the response. - return response - - async def cancel_operation( - self, - request: Optional[operations_pb2.CancelOperationRequest] = None, - *, - retry: OptionalRetry = gapic_v1.method.DEFAULT, - timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), - ) -> None: - r"""Starts asynchronous cancellation on a long-running operation. - - The server makes a best effort to cancel the operation, but success - is not guaranteed. If the server doesn't support this method, it returns - `google.rpc.Code.UNIMPLEMENTED`. - - Args: - request (:class:`~.operations_pb2.CancelOperationRequest`): - The request object. Request message for - `CancelOperation` method. - retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, - if any, should be retried. - timeout (float): The timeout for this request. - metadata (Sequence[Tuple[str, str]]): Strings which should be - sent along with the request as metadata. - Returns: - None - """ - # Create or coerce a protobuf request object. - # The request isn't a proto-plus wrapped type, - # so it must be constructed via keyword expansion. - if isinstance(request, dict): - request = operations_pb2.CancelOperationRequest(**request) - - # Wrap the RPC method; this adds retry and timeout information, - # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.cancel_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) - - # Certain fields should be provided within the metadata header; - # add these here. - metadata = tuple(metadata) + ( - gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), - ) - - # Validate the universe domain. - self._client._validate_universe_domain() - - # Send the request. - await rpc( - request, - retry=retry, - timeout=timeout, - metadata=metadata, - ) - - async def get_location( - self, - request: Optional[locations_pb2.GetLocationRequest] = None, - *, - retry: OptionalRetry = gapic_v1.method.DEFAULT, - timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), - ) -> locations_pb2.Location: - r"""Gets information about a location. - - Args: - request (:class:`~.location_pb2.GetLocationRequest`): - The request object. Request message for - `GetLocation` method. - retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, - if any, should be retried. - timeout (float): The timeout for this request. - metadata (Sequence[Tuple[str, str]]): Strings which should be - sent along with the request as metadata. - Returns: - ~.location_pb2.Location: - Location object. - """ - # Create or coerce a protobuf request object. - # The request isn't a proto-plus wrapped type, - # so it must be constructed via keyword expansion. - if isinstance(request, dict): - request = locations_pb2.GetLocationRequest(**request) - - # Wrap the RPC method; this adds retry and timeout information, - # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.get_location, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) - - # Certain fields should be provided within the metadata header; - # add these here. - metadata = tuple(metadata) + ( - gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), - ) - - # Validate the universe domain. - self._client._validate_universe_domain() - - # Send the request. - response = await rpc( - request, - retry=retry, - timeout=timeout, - metadata=metadata, - ) - - # Done; return the response. - return response - - async def list_locations( - self, - request: Optional[locations_pb2.ListLocationsRequest] = None, - *, - retry: OptionalRetry = gapic_v1.method.DEFAULT, - timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), - ) -> locations_pb2.ListLocationsResponse: - r"""Lists information about the supported locations for this service. - - Args: - request (:class:`~.location_pb2.ListLocationsRequest`): - The request object. Request message for - `ListLocations` method. - retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, - if any, should be retried. - timeout (float): The timeout for this request. - metadata (Sequence[Tuple[str, str]]): Strings which should be - sent along with the request as metadata. - Returns: - ~.location_pb2.ListLocationsResponse: - Response message for ``ListLocations`` method. - """ - # Create or coerce a protobuf request object. - # The request isn't a proto-plus wrapped type, - # so it must be constructed via keyword expansion. - if isinstance(request, dict): - request = locations_pb2.ListLocationsRequest(**request) - - # Wrap the RPC method; this adds retry and timeout information, - # and friendly error handling. - rpc = gapic_v1.method_async.wrap_method( - self._client._transport.list_locations, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) - - # Certain fields should be provided within the metadata header; - # add these here. - metadata = tuple(metadata) + ( - gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), - ) - - # Validate the universe domain. - self._client._validate_universe_domain() - - # Send the request. - response = await rpc( - request, - retry=retry, - timeout=timeout, - metadata=metadata, - ) - - # Done; return the response. - return response - - async def __aenter__(self) -> "DocumentUnderstandingServiceAsyncClient": - return self - - async def __aexit__(self, exc_type, exc, tb): - await self.transport.close() - - -DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( - gapic_version=package_version.__version__ -) - - -__all__ = ("DocumentUnderstandingServiceAsyncClient",) diff --git a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/document_understanding_service/client.py b/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/document_understanding_service/client.py deleted file mode 100644 index 3a4dac49cbca..000000000000 --- a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/document_understanding_service/client.py +++ /dev/null @@ -1,1203 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2024 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -from collections import OrderedDict -import os -import re -from typing import ( - Callable, - Dict, - Mapping, - MutableMapping, - MutableSequence, - Optional, - Sequence, - Tuple, - Type, - Union, - cast, -) -import warnings - -from google.api_core import client_options as client_options_lib -from google.api_core import exceptions as core_exceptions -from google.api_core import gapic_v1 -from google.api_core import retry as retries -from google.auth import credentials as ga_credentials # type: ignore -from google.auth.exceptions import MutualTLSChannelError # type: ignore -from google.auth.transport import mtls # type: ignore -from google.auth.transport.grpc import SslCredentials # type: ignore -from google.oauth2 import service_account # type: ignore - -from google.cloud.documentai_v1beta2 import gapic_version as package_version - -try: - OptionalRetry = Union[retries.Retry, gapic_v1.method._MethodDefault, None] -except AttributeError: # pragma: NO COVER - OptionalRetry = Union[retries.Retry, object, None] # type: ignore - -from google.api_core import operation # type: ignore -from google.api_core import operation_async # type: ignore -from google.cloud.location import locations_pb2 # type: ignore -from google.longrunning import operations_pb2 # type: ignore -from google.rpc import status_pb2 # type: ignore - -from google.cloud.documentai_v1beta2.types import document, document_understanding - -from .transports.base import DEFAULT_CLIENT_INFO, DocumentUnderstandingServiceTransport -from .transports.grpc import DocumentUnderstandingServiceGrpcTransport -from .transports.grpc_asyncio import DocumentUnderstandingServiceGrpcAsyncIOTransport - - -class DocumentUnderstandingServiceClientMeta(type): - """Metaclass for the DocumentUnderstandingService client. - - This provides class-level methods for building and retrieving - support objects (e.g. transport) without polluting the client instance - objects. - """ - - _transport_registry = ( - OrderedDict() - ) # type: Dict[str, Type[DocumentUnderstandingServiceTransport]] - _transport_registry["grpc"] = DocumentUnderstandingServiceGrpcTransport - _transport_registry[ - "grpc_asyncio" - ] = DocumentUnderstandingServiceGrpcAsyncIOTransport - - def get_transport_class( - cls, - label: Optional[str] = None, - ) -> Type[DocumentUnderstandingServiceTransport]: - """Returns an appropriate transport class. - - Args: - label: The name of the desired transport. If none is - provided, then the first transport in the registry is used. - - Returns: - The transport class to use. - """ - # If a specific transport is requested, return that one. - if label: - return cls._transport_registry[label] - - # No transport is requested; return the default (that is, the first one - # in the dictionary). - return next(iter(cls._transport_registry.values())) - - -class DocumentUnderstandingServiceClient( - metaclass=DocumentUnderstandingServiceClientMeta -): - """Service to parse structured information from unstructured or - semi-structured documents using state-of-the-art Google AI such - as natural language, computer vision, and translation. - """ - - @staticmethod - def _get_default_mtls_endpoint(api_endpoint): - """Converts api endpoint to mTLS endpoint. - - Convert "*.sandbox.googleapis.com" and "*.googleapis.com" to - "*.mtls.sandbox.googleapis.com" and "*.mtls.googleapis.com" respectively. - Args: - api_endpoint (Optional[str]): the api endpoint to convert. - Returns: - str: converted mTLS api endpoint. - """ - if not api_endpoint: - return api_endpoint - - mtls_endpoint_re = re.compile( - r"(?P[^.]+)(?P\.mtls)?(?P\.sandbox)?(?P\.googleapis\.com)?" - ) - - m = mtls_endpoint_re.match(api_endpoint) - name, mtls, sandbox, googledomain = m.groups() - if mtls or not googledomain: - return api_endpoint - - if sandbox: - return api_endpoint.replace( - "sandbox.googleapis.com", "mtls.sandbox.googleapis.com" - ) - - return api_endpoint.replace(".googleapis.com", ".mtls.googleapis.com") - - # Note: DEFAULT_ENDPOINT is deprecated. Use _DEFAULT_ENDPOINT_TEMPLATE instead. - DEFAULT_ENDPOINT = "documentai.googleapis.com" - DEFAULT_MTLS_ENDPOINT = _get_default_mtls_endpoint.__func__( # type: ignore - DEFAULT_ENDPOINT - ) - - _DEFAULT_ENDPOINT_TEMPLATE = "documentai.{UNIVERSE_DOMAIN}" - _DEFAULT_UNIVERSE = "googleapis.com" - - @classmethod - def from_service_account_info(cls, info: dict, *args, **kwargs): - """Creates an instance of this client using the provided credentials - info. - - Args: - info (dict): The service account private key info. - args: Additional arguments to pass to the constructor. - kwargs: Additional arguments to pass to the constructor. - - Returns: - DocumentUnderstandingServiceClient: The constructed client. - """ - credentials = service_account.Credentials.from_service_account_info(info) - kwargs["credentials"] = credentials - return cls(*args, **kwargs) - - @classmethod - def from_service_account_file(cls, filename: str, *args, **kwargs): - """Creates an instance of this client using the provided credentials - file. - - Args: - filename (str): The path to the service account private key json - file. - args: Additional arguments to pass to the constructor. - kwargs: Additional arguments to pass to the constructor. - - Returns: - DocumentUnderstandingServiceClient: The constructed client. - """ - credentials = service_account.Credentials.from_service_account_file(filename) - kwargs["credentials"] = credentials - return cls(*args, **kwargs) - - from_service_account_json = from_service_account_file - - @property - def transport(self) -> DocumentUnderstandingServiceTransport: - """Returns the transport used by the client instance. - - Returns: - DocumentUnderstandingServiceTransport: The transport used by the client - instance. - """ - return self._transport - - @staticmethod - def common_billing_account_path( - billing_account: str, - ) -> str: - """Returns a fully-qualified billing_account string.""" - return "billingAccounts/{billing_account}".format( - billing_account=billing_account, - ) - - @staticmethod - def parse_common_billing_account_path(path: str) -> Dict[str, str]: - """Parse a billing_account path into its component segments.""" - m = re.match(r"^billingAccounts/(?P.+?)$", path) - return m.groupdict() if m else {} - - @staticmethod - def common_folder_path( - folder: str, - ) -> str: - """Returns a fully-qualified folder string.""" - return "folders/{folder}".format( - folder=folder, - ) - - @staticmethod - def parse_common_folder_path(path: str) -> Dict[str, str]: - """Parse a folder path into its component segments.""" - m = re.match(r"^folders/(?P.+?)$", path) - return m.groupdict() if m else {} - - @staticmethod - def common_organization_path( - organization: str, - ) -> str: - """Returns a fully-qualified organization string.""" - return "organizations/{organization}".format( - organization=organization, - ) - - @staticmethod - def parse_common_organization_path(path: str) -> Dict[str, str]: - """Parse a organization path into its component segments.""" - m = re.match(r"^organizations/(?P.+?)$", path) - return m.groupdict() if m else {} - - @staticmethod - def common_project_path( - project: str, - ) -> str: - """Returns a fully-qualified project string.""" - return "projects/{project}".format( - project=project, - ) - - @staticmethod - def parse_common_project_path(path: str) -> Dict[str, str]: - """Parse a project path into its component segments.""" - m = re.match(r"^projects/(?P.+?)$", path) - return m.groupdict() if m else {} - - @staticmethod - def common_location_path( - project: str, - location: str, - ) -> str: - """Returns a fully-qualified location string.""" - return "projects/{project}/locations/{location}".format( - project=project, - location=location, - ) - - @staticmethod - def parse_common_location_path(path: str) -> Dict[str, str]: - """Parse a location path into its component segments.""" - m = re.match(r"^projects/(?P.+?)/locations/(?P.+?)$", path) - return m.groupdict() if m else {} - - @classmethod - def get_mtls_endpoint_and_cert_source( - cls, client_options: Optional[client_options_lib.ClientOptions] = None - ): - """Deprecated. Return the API endpoint and client cert source for mutual TLS. - - The client cert source is determined in the following order: - (1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the - client cert source is None. - (2) if `client_options.client_cert_source` is provided, use the provided one; if the - default client cert source exists, use the default one; otherwise the client cert - source is None. - - The API endpoint is determined in the following order: - (1) if `client_options.api_endpoint` if provided, use the provided one. - (2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the - default mTLS endpoint; if the environment variable is "never", use the default API - endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise - use the default API endpoint. - - More details can be found at https://google.aip.dev/auth/4114. - - Args: - client_options (google.api_core.client_options.ClientOptions): Custom options for the - client. Only the `api_endpoint` and `client_cert_source` properties may be used - in this method. - - Returns: - Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the - client cert source to use. - - Raises: - google.auth.exceptions.MutualTLSChannelError: If any errors happen. - """ - - warnings.warn( - "get_mtls_endpoint_and_cert_source is deprecated. Use the api_endpoint property instead.", - DeprecationWarning, - ) - if client_options is None: - client_options = client_options_lib.ClientOptions() - use_client_cert = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") - use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") - if use_client_cert not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`" - ) - if use_mtls_endpoint not in ("auto", "never", "always"): - raise MutualTLSChannelError( - "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`" - ) - - # Figure out the client cert source to use. - client_cert_source = None - if use_client_cert == "true": - if client_options.client_cert_source: - client_cert_source = client_options.client_cert_source - elif mtls.has_default_client_cert_source(): - client_cert_source = mtls.default_client_cert_source() - - # Figure out which api endpoint to use. - if client_options.api_endpoint is not None: - api_endpoint = client_options.api_endpoint - elif use_mtls_endpoint == "always" or ( - use_mtls_endpoint == "auto" and client_cert_source - ): - api_endpoint = cls.DEFAULT_MTLS_ENDPOINT - else: - api_endpoint = cls.DEFAULT_ENDPOINT - - return api_endpoint, client_cert_source - - @staticmethod - def _read_environment_variables(): - """Returns the environment variables used by the client. - - Returns: - Tuple[bool, str, str]: returns the GOOGLE_API_USE_CLIENT_CERTIFICATE, - GOOGLE_API_USE_MTLS_ENDPOINT, and GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variables. - - Raises: - ValueError: If GOOGLE_API_USE_CLIENT_CERTIFICATE is not - any of ["true", "false"]. - google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT - is not any of ["auto", "never", "always"]. - """ - use_client_cert = os.getenv( - "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" - ).lower() - use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower() - universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN") - if use_client_cert not in ("true", "false"): - raise ValueError( - "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`" - ) - if use_mtls_endpoint not in ("auto", "never", "always"): - raise MutualTLSChannelError( - "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`" - ) - return use_client_cert == "true", use_mtls_endpoint, universe_domain_env - - @staticmethod - def _get_client_cert_source(provided_cert_source, use_cert_flag): - """Return the client cert source to be used by the client. - - Args: - provided_cert_source (bytes): The client certificate source provided. - use_cert_flag (bool): A flag indicating whether to use the client certificate. - - Returns: - bytes or None: The client cert source to be used by the client. - """ - client_cert_source = None - if use_cert_flag: - if provided_cert_source: - client_cert_source = provided_cert_source - elif mtls.has_default_client_cert_source(): - client_cert_source = mtls.default_client_cert_source() - return client_cert_source - - @staticmethod - def _get_api_endpoint( - api_override, client_cert_source, universe_domain, use_mtls_endpoint - ): - """Return the API endpoint used by the client. - - Args: - api_override (str): The API endpoint override. If specified, this is always - the return value of this function and the other arguments are not used. - client_cert_source (bytes): The client certificate source used by the client. - universe_domain (str): The universe domain used by the client. - use_mtls_endpoint (str): How to use the mTLS endpoint, which depends also on the other parameters. - Possible values are "always", "auto", or "never". - - Returns: - str: The API endpoint to be used by the client. - """ - if api_override is not None: - api_endpoint = api_override - elif use_mtls_endpoint == "always" or ( - use_mtls_endpoint == "auto" and client_cert_source - ): - _default_universe = DocumentUnderstandingServiceClient._DEFAULT_UNIVERSE - if universe_domain != _default_universe: - raise MutualTLSChannelError( - f"mTLS is not supported in any universe other than {_default_universe}." - ) - api_endpoint = DocumentUnderstandingServiceClient.DEFAULT_MTLS_ENDPOINT - else: - api_endpoint = ( - DocumentUnderstandingServiceClient._DEFAULT_ENDPOINT_TEMPLATE.format( - UNIVERSE_DOMAIN=universe_domain - ) - ) - return api_endpoint - - @staticmethod - def _get_universe_domain( - client_universe_domain: Optional[str], universe_domain_env: Optional[str] - ) -> str: - """Return the universe domain used by the client. - - Args: - client_universe_domain (Optional[str]): The universe domain configured via the client options. - universe_domain_env (Optional[str]): The universe domain configured via the "GOOGLE_CLOUD_UNIVERSE_DOMAIN" environment variable. - - Returns: - str: The universe domain to be used by the client. - - Raises: - ValueError: If the universe domain is an empty string. - """ - universe_domain = DocumentUnderstandingServiceClient._DEFAULT_UNIVERSE - if client_universe_domain is not None: - universe_domain = client_universe_domain - elif universe_domain_env is not None: - universe_domain = universe_domain_env - if len(universe_domain.strip()) == 0: - raise ValueError("Universe Domain cannot be an empty string.") - return universe_domain - - @staticmethod - def _compare_universes( - client_universe: str, credentials: ga_credentials.Credentials - ) -> bool: - """Returns True iff the universe domains used by the client and credentials match. - - Args: - client_universe (str): The universe domain configured via the client options. - credentials (ga_credentials.Credentials): The credentials being used in the client. - - Returns: - bool: True iff client_universe matches the universe in credentials. - - Raises: - ValueError: when client_universe does not match the universe in credentials. - """ - - default_universe = DocumentUnderstandingServiceClient._DEFAULT_UNIVERSE - credentials_universe = getattr(credentials, "universe_domain", default_universe) - - if client_universe != credentials_universe: - raise ValueError( - "The configured universe domain " - f"({client_universe}) does not match the universe domain " - f"found in the credentials ({credentials_universe}). " - "If you haven't configured the universe domain explicitly, " - f"`{default_universe}` is the default." - ) - return True - - def _validate_universe_domain(self): - """Validates client's and credentials' universe domains are consistent. - - Returns: - bool: True iff the configured universe domain is valid. - - Raises: - ValueError: If the configured universe domain is not valid. - """ - self._is_universe_domain_valid = ( - self._is_universe_domain_valid - or DocumentUnderstandingServiceClient._compare_universes( - self.universe_domain, self.transport._credentials - ) - ) - return self._is_universe_domain_valid - - @property - def api_endpoint(self): - """Return the API endpoint used by the client instance. - - Returns: - str: The API endpoint used by the client instance. - """ - return self._api_endpoint - - @property - def universe_domain(self) -> str: - """Return the universe domain used by the client instance. - - Returns: - str: The universe domain used by the client instance. - """ - return self._universe_domain - - def __init__( - self, - *, - credentials: Optional[ga_credentials.Credentials] = None, - transport: Optional[ - Union[ - str, - DocumentUnderstandingServiceTransport, - Callable[..., DocumentUnderstandingServiceTransport], - ] - ] = None, - client_options: Optional[Union[client_options_lib.ClientOptions, dict]] = None, - client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, - ) -> None: - """Instantiates the document understanding service client. - - Args: - credentials (Optional[google.auth.credentials.Credentials]): The - authorization credentials to attach to requests. These - credentials identify the application to the service; if none - are specified, the client will attempt to ascertain the - credentials from the environment. - transport (Optional[Union[str,DocumentUnderstandingServiceTransport,Callable[..., DocumentUnderstandingServiceTransport]]]): - The transport to use, or a Callable that constructs and returns a new transport. - If a Callable is given, it will be called with the same set of initialization - arguments as used in the DocumentUnderstandingServiceTransport constructor. - If set to None, a transport is chosen automatically. - client_options (Optional[Union[google.api_core.client_options.ClientOptions, dict]]): - Custom options for the client. - - 1. The ``api_endpoint`` property can be used to override the - default endpoint provided by the client when ``transport`` is - not explicitly provided. Only if this property is not set and - ``transport`` was not explicitly provided, the endpoint is - determined by the GOOGLE_API_USE_MTLS_ENDPOINT environment - variable, which have one of the following values: - "always" (always use the default mTLS endpoint), "never" (always - use the default regular endpoint) and "auto" (auto-switch to the - default mTLS endpoint if client certificate is present; this is - the default value). - - 2. If the GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable - is "true", then the ``client_cert_source`` property can be used - to provide a client certificate for mTLS transport. If - not provided, the default SSL client certificate will be used if - present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not - set, no client certificate will be used. - - 3. The ``universe_domain`` property can be used to override the - default "googleapis.com" universe. Note that the ``api_endpoint`` - property still takes precedence; and ``universe_domain`` is - currently not supported for mTLS. - - client_info (google.api_core.gapic_v1.client_info.ClientInfo): - The client info used to send a user-agent string along with - API requests. If ``None``, then default info will be used. - Generally, you only need to set this if you're developing - your own client library. - - Raises: - google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport - creation failed for any reason. - """ - self._client_options = client_options - if isinstance(self._client_options, dict): - self._client_options = client_options_lib.from_dict(self._client_options) - if self._client_options is None: - self._client_options = client_options_lib.ClientOptions() - self._client_options = cast( - client_options_lib.ClientOptions, self._client_options - ) - - universe_domain_opt = getattr(self._client_options, "universe_domain", None) - - ( - self._use_client_cert, - self._use_mtls_endpoint, - self._universe_domain_env, - ) = DocumentUnderstandingServiceClient._read_environment_variables() - self._client_cert_source = ( - DocumentUnderstandingServiceClient._get_client_cert_source( - self._client_options.client_cert_source, self._use_client_cert - ) - ) - self._universe_domain = DocumentUnderstandingServiceClient._get_universe_domain( - universe_domain_opt, self._universe_domain_env - ) - self._api_endpoint = None # updated below, depending on `transport` - - # Initialize the universe domain validation. - self._is_universe_domain_valid = False - - api_key_value = getattr(self._client_options, "api_key", None) - if api_key_value and credentials: - raise ValueError( - "client_options.api_key and credentials are mutually exclusive" - ) - - # Save or instantiate the transport. - # Ordinarily, we provide the transport, but allowing a custom transport - # instance provides an extensibility point for unusual situations. - transport_provided = isinstance( - transport, DocumentUnderstandingServiceTransport - ) - if transport_provided: - # transport is a DocumentUnderstandingServiceTransport instance. - if credentials or self._client_options.credentials_file or api_key_value: - raise ValueError( - "When providing a transport instance, " - "provide its credentials directly." - ) - if self._client_options.scopes: - raise ValueError( - "When providing a transport instance, provide its scopes " - "directly." - ) - self._transport = cast(DocumentUnderstandingServiceTransport, transport) - self._api_endpoint = self._transport.host - - self._api_endpoint = ( - self._api_endpoint - or DocumentUnderstandingServiceClient._get_api_endpoint( - self._client_options.api_endpoint, - self._client_cert_source, - self._universe_domain, - self._use_mtls_endpoint, - ) - ) - - if not transport_provided: - import google.auth._default # type: ignore - - if api_key_value and hasattr( - google.auth._default, "get_api_key_credentials" - ): - credentials = google.auth._default.get_api_key_credentials( - api_key_value - ) - - transport_init: Union[ - Type[DocumentUnderstandingServiceTransport], - Callable[..., DocumentUnderstandingServiceTransport], - ] = ( - DocumentUnderstandingServiceClient.get_transport_class(transport) - if isinstance(transport, str) or transport is None - else cast( - Callable[..., DocumentUnderstandingServiceTransport], transport - ) - ) - # initialize with the provided callable or the passed in class - self._transport = transport_init( - credentials=credentials, - credentials_file=self._client_options.credentials_file, - host=self._api_endpoint, - scopes=self._client_options.scopes, - client_cert_source_for_mtls=self._client_cert_source, - quota_project_id=self._client_options.quota_project_id, - client_info=client_info, - always_use_jwt_access=True, - api_audience=self._client_options.api_audience, - ) - - def batch_process_documents( - self, - request: Optional[ - Union[document_understanding.BatchProcessDocumentsRequest, dict] - ] = None, - *, - requests: Optional[ - MutableSequence[document_understanding.ProcessDocumentRequest] - ] = None, - retry: OptionalRetry = gapic_v1.method.DEFAULT, - timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), - ) -> operation.Operation: - r"""LRO endpoint to batch process many documents. The output is - written to Cloud Storage as JSON in the [Document] format. - - .. code-block:: python - - # This snippet has been automatically generated and should be regarded as a - # code template only. - # It will require modifications to work: - # - It may require correct/in-range values for request initialization. - # - It may require specifying regional endpoints when creating the service - # client as shown in: - # https://googleapis.dev/python/google-api-core/latest/client_options.html - from google.cloud import documentai_v1beta2 - - def sample_batch_process_documents(): - # Create a client - client = documentai_v1beta2.DocumentUnderstandingServiceClient() - - # Initialize request argument(s) - requests = documentai_v1beta2.ProcessDocumentRequest() - requests.input_config.gcs_source.uri = "uri_value" - requests.input_config.mime_type = "mime_type_value" - - request = documentai_v1beta2.BatchProcessDocumentsRequest( - requests=requests, - ) - - # Make the request - operation = client.batch_process_documents(request=request) - - print("Waiting for operation to complete...") - - response = operation.result() - - # Handle the response - print(response) - - Args: - request (Union[google.cloud.documentai_v1beta2.types.BatchProcessDocumentsRequest, dict]): - The request object. Request to batch process documents as an asynchronous - operation. The output is written to Cloud Storage as - JSON in the [Document] format. - requests (MutableSequence[google.cloud.documentai_v1beta2.types.ProcessDocumentRequest]): - Required. Individual requests for - each document. - - This corresponds to the ``requests`` field - on the ``request`` instance; if ``request`` is provided, this - should not be set. - retry (google.api_core.retry.Retry): Designation of what errors, if any, - should be retried. - timeout (float): The timeout for this request. - metadata (Sequence[Tuple[str, str]]): Strings which should be - sent along with the request as metadata. - - Returns: - google.api_core.operation.Operation: - An object representing a long-running operation. - - The result type for the operation will be :class:`google.cloud.documentai_v1beta2.types.BatchProcessDocumentsResponse` Response to an batch document processing request. This is returned in - the LRO Operation after the operation is complete. - - """ - # Create or coerce a protobuf request object. - # - Quick check: If we got a request object, we should *not* have - # gotten any keyword arguments that map to the request. - has_flattened_params = any([requests]) - if request is not None and has_flattened_params: - raise ValueError( - "If the `request` argument is set, then none of " - "the individual field arguments should be set." - ) - - # - Use the request object if provided (there's no risk of modifying the input as - # there are no flattened fields), or create one. - if not isinstance(request, document_understanding.BatchProcessDocumentsRequest): - request = document_understanding.BatchProcessDocumentsRequest(request) - # If we have keyword arguments corresponding to fields on the - # request, apply these. - if requests is not None: - request.requests = requests - - # Wrap the RPC method; this adds retry and timeout information, - # and friendly error handling. - rpc = self._transport._wrapped_methods[self._transport.batch_process_documents] - - # Certain fields should be provided within the metadata header; - # add these here. - metadata = tuple(metadata) + ( - gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), - ) - - # Validate the universe domain. - self._validate_universe_domain() - - # Send the request. - response = rpc( - request, - retry=retry, - timeout=timeout, - metadata=metadata, - ) - - # Wrap the response in an operation future. - response = operation.from_gapic( - response, - self._transport.operations_client, - document_understanding.BatchProcessDocumentsResponse, - metadata_type=document_understanding.OperationMetadata, - ) - - # Done; return the response. - return response - - def process_document( - self, - request: Optional[ - Union[document_understanding.ProcessDocumentRequest, dict] - ] = None, - *, - retry: OptionalRetry = gapic_v1.method.DEFAULT, - timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), - ) -> document.Document: - r"""Processes a single document. - - .. code-block:: python - - # This snippet has been automatically generated and should be regarded as a - # code template only. - # It will require modifications to work: - # - It may require correct/in-range values for request initialization. - # - It may require specifying regional endpoints when creating the service - # client as shown in: - # https://googleapis.dev/python/google-api-core/latest/client_options.html - from google.cloud import documentai_v1beta2 - - def sample_process_document(): - # Create a client - client = documentai_v1beta2.DocumentUnderstandingServiceClient() - - # Initialize request argument(s) - input_config = documentai_v1beta2.InputConfig() - input_config.gcs_source.uri = "uri_value" - input_config.mime_type = "mime_type_value" - - request = documentai_v1beta2.ProcessDocumentRequest( - input_config=input_config, - ) - - # Make the request - response = client.process_document(request=request) - - # Handle the response - print(response) - - Args: - request (Union[google.cloud.documentai_v1beta2.types.ProcessDocumentRequest, dict]): - The request object. Request to process one document. - retry (google.api_core.retry.Retry): Designation of what errors, if any, - should be retried. - timeout (float): The timeout for this request. - metadata (Sequence[Tuple[str, str]]): Strings which should be - sent along with the request as metadata. - - Returns: - google.cloud.documentai_v1beta2.types.Document: - Document represents the canonical - document resource in Document AI. It is - an interchange format that provides - insights into documents and allows for - collaboration between users and Document - AI to iterate and optimize for quality. - - """ - # Create or coerce a protobuf request object. - # - Use the request object if provided (there's no risk of modifying the input as - # there are no flattened fields), or create one. - if not isinstance(request, document_understanding.ProcessDocumentRequest): - request = document_understanding.ProcessDocumentRequest(request) - - # Wrap the RPC method; this adds retry and timeout information, - # and friendly error handling. - rpc = self._transport._wrapped_methods[self._transport.process_document] - - # Certain fields should be provided within the metadata header; - # add these here. - metadata = tuple(metadata) + ( - gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), - ) - - # Validate the universe domain. - self._validate_universe_domain() - - # Send the request. - response = rpc( - request, - retry=retry, - timeout=timeout, - metadata=metadata, - ) - - # Done; return the response. - return response - - def __enter__(self) -> "DocumentUnderstandingServiceClient": - 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() - - def list_operations( - self, - request: Optional[operations_pb2.ListOperationsRequest] = None, - *, - retry: OptionalRetry = gapic_v1.method.DEFAULT, - timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), - ) -> operations_pb2.ListOperationsResponse: - r"""Lists operations that match the specified filter in the request. - - Args: - request (:class:`~.operations_pb2.ListOperationsRequest`): - The request object. Request message for - `ListOperations` method. - retry (google.api_core.retry.Retry): Designation of what errors, - if any, should be retried. - timeout (float): The timeout for this request. - metadata (Sequence[Tuple[str, str]]): Strings which should be - sent along with the request as metadata. - Returns: - ~.operations_pb2.ListOperationsResponse: - Response message for ``ListOperations`` method. - """ - # Create or coerce a protobuf request object. - # The request isn't a proto-plus wrapped type, - # so it must be constructed via keyword expansion. - if isinstance(request, dict): - request = operations_pb2.ListOperationsRequest(**request) - - # Wrap the RPC method; this adds retry and timeout information, - # and friendly error handling. - rpc = gapic_v1.method.wrap_method( - self._transport.list_operations, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) - - # Certain fields should be provided within the metadata header; - # add these here. - metadata = tuple(metadata) + ( - gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), - ) - - # Validate the universe domain. - self._validate_universe_domain() - - # Send the request. - response = rpc( - request, - retry=retry, - timeout=timeout, - metadata=metadata, - ) - - # Done; return the response. - return response - - def get_operation( - self, - request: Optional[operations_pb2.GetOperationRequest] = None, - *, - retry: OptionalRetry = gapic_v1.method.DEFAULT, - timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), - ) -> operations_pb2.Operation: - r"""Gets the latest state of a long-running operation. - - Args: - request (:class:`~.operations_pb2.GetOperationRequest`): - The request object. Request message for - `GetOperation` method. - retry (google.api_core.retry.Retry): Designation of what errors, - if any, should be retried. - timeout (float): The timeout for this request. - metadata (Sequence[Tuple[str, str]]): Strings which should be - sent along with the request as metadata. - Returns: - ~.operations_pb2.Operation: - An ``Operation`` object. - """ - # Create or coerce a protobuf request object. - # The request isn't a proto-plus wrapped type, - # so it must be constructed via keyword expansion. - if isinstance(request, dict): - request = operations_pb2.GetOperationRequest(**request) - - # Wrap the RPC method; this adds retry and timeout information, - # and friendly error handling. - rpc = gapic_v1.method.wrap_method( - self._transport.get_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) - - # Certain fields should be provided within the metadata header; - # add these here. - metadata = tuple(metadata) + ( - gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), - ) - - # Validate the universe domain. - self._validate_universe_domain() - - # Send the request. - response = rpc( - request, - retry=retry, - timeout=timeout, - metadata=metadata, - ) - - # Done; return the response. - return response - - def cancel_operation( - self, - request: Optional[operations_pb2.CancelOperationRequest] = None, - *, - retry: OptionalRetry = gapic_v1.method.DEFAULT, - timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), - ) -> None: - r"""Starts asynchronous cancellation on a long-running operation. - - The server makes a best effort to cancel the operation, but success - is not guaranteed. If the server doesn't support this method, it returns - `google.rpc.Code.UNIMPLEMENTED`. - - Args: - request (:class:`~.operations_pb2.CancelOperationRequest`): - The request object. Request message for - `CancelOperation` method. - retry (google.api_core.retry.Retry): Designation of what errors, - if any, should be retried. - timeout (float): The timeout for this request. - metadata (Sequence[Tuple[str, str]]): Strings which should be - sent along with the request as metadata. - Returns: - None - """ - # Create or coerce a protobuf request object. - # The request isn't a proto-plus wrapped type, - # so it must be constructed via keyword expansion. - if isinstance(request, dict): - request = operations_pb2.CancelOperationRequest(**request) - - # Wrap the RPC method; this adds retry and timeout information, - # and friendly error handling. - rpc = gapic_v1.method.wrap_method( - self._transport.cancel_operation, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) - - # Certain fields should be provided within the metadata header; - # add these here. - metadata = tuple(metadata) + ( - gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), - ) - - # Validate the universe domain. - self._validate_universe_domain() - - # Send the request. - rpc( - request, - retry=retry, - timeout=timeout, - metadata=metadata, - ) - - def get_location( - self, - request: Optional[locations_pb2.GetLocationRequest] = None, - *, - retry: OptionalRetry = gapic_v1.method.DEFAULT, - timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), - ) -> locations_pb2.Location: - r"""Gets information about a location. - - Args: - request (:class:`~.location_pb2.GetLocationRequest`): - The request object. Request message for - `GetLocation` method. - retry (google.api_core.retry.Retry): Designation of what errors, - if any, should be retried. - timeout (float): The timeout for this request. - metadata (Sequence[Tuple[str, str]]): Strings which should be - sent along with the request as metadata. - Returns: - ~.location_pb2.Location: - Location object. - """ - # Create or coerce a protobuf request object. - # The request isn't a proto-plus wrapped type, - # so it must be constructed via keyword expansion. - if isinstance(request, dict): - request = locations_pb2.GetLocationRequest(**request) - - # Wrap the RPC method; this adds retry and timeout information, - # and friendly error handling. - rpc = gapic_v1.method.wrap_method( - self._transport.get_location, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) - - # Certain fields should be provided within the metadata header; - # add these here. - metadata = tuple(metadata) + ( - gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), - ) - - # Validate the universe domain. - self._validate_universe_domain() - - # Send the request. - response = rpc( - request, - retry=retry, - timeout=timeout, - metadata=metadata, - ) - - # Done; return the response. - return response - - def list_locations( - self, - request: Optional[locations_pb2.ListLocationsRequest] = None, - *, - retry: OptionalRetry = gapic_v1.method.DEFAULT, - timeout: Union[float, object] = gapic_v1.method.DEFAULT, - metadata: Sequence[Tuple[str, str]] = (), - ) -> locations_pb2.ListLocationsResponse: - r"""Lists information about the supported locations for this service. - - Args: - request (:class:`~.location_pb2.ListLocationsRequest`): - The request object. Request message for - `ListLocations` method. - retry (google.api_core.retry.Retry): Designation of what errors, - if any, should be retried. - timeout (float): The timeout for this request. - metadata (Sequence[Tuple[str, str]]): Strings which should be - sent along with the request as metadata. - Returns: - ~.location_pb2.ListLocationsResponse: - Response message for ``ListLocations`` method. - """ - # Create or coerce a protobuf request object. - # The request isn't a proto-plus wrapped type, - # so it must be constructed via keyword expansion. - if isinstance(request, dict): - request = locations_pb2.ListLocationsRequest(**request) - - # Wrap the RPC method; this adds retry and timeout information, - # and friendly error handling. - rpc = gapic_v1.method.wrap_method( - self._transport.list_locations, - default_timeout=None, - client_info=DEFAULT_CLIENT_INFO, - ) - - # Certain fields should be provided within the metadata header; - # add these here. - metadata = tuple(metadata) + ( - gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), - ) - - # Validate the universe domain. - self._validate_universe_domain() - - # Send the request. - response = rpc( - request, - retry=retry, - timeout=timeout, - metadata=metadata, - ) - - # Done; return the response. - return response - - -DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( - gapic_version=package_version.__version__ -) - - -__all__ = ("DocumentUnderstandingServiceClient",) diff --git a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/document_understanding_service/transports/__init__.py b/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/document_understanding_service/transports/__init__.py deleted file mode 100644 index 28624835a5ae..000000000000 --- a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/document_understanding_service/transports/__init__.py +++ /dev/null @@ -1,34 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2024 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -from collections import OrderedDict -from typing import Dict, Type - -from .base import DocumentUnderstandingServiceTransport -from .grpc import DocumentUnderstandingServiceGrpcTransport -from .grpc_asyncio import DocumentUnderstandingServiceGrpcAsyncIOTransport - -# Compile a registry of transports. -_transport_registry = ( - OrderedDict() -) # type: Dict[str, Type[DocumentUnderstandingServiceTransport]] -_transport_registry["grpc"] = DocumentUnderstandingServiceGrpcTransport -_transport_registry["grpc_asyncio"] = DocumentUnderstandingServiceGrpcAsyncIOTransport - -__all__ = ( - "DocumentUnderstandingServiceTransport", - "DocumentUnderstandingServiceGrpcTransport", - "DocumentUnderstandingServiceGrpcAsyncIOTransport", -) diff --git a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/document_understanding_service/transports/base.py b/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/document_understanding_service/transports/base.py deleted file mode 100644 index 4390831f04d4..000000000000 --- a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/document_understanding_service/transports/base.py +++ /dev/null @@ -1,250 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2024 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -import abc -from typing import Awaitable, Callable, Dict, Optional, Sequence, Union - -import google.api_core -from google.api_core import exceptions as core_exceptions -from google.api_core import gapic_v1, operations_v1 -from google.api_core import retry as retries -import google.auth # type: ignore -from google.auth import credentials as ga_credentials # type: ignore -from google.cloud.location import locations_pb2 # type: ignore -from google.longrunning import operations_pb2 # type: ignore -from google.oauth2 import service_account # type: ignore - -from google.cloud.documentai_v1beta2 import gapic_version as package_version -from google.cloud.documentai_v1beta2.types import document, document_understanding - -DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( - gapic_version=package_version.__version__ -) - - -class DocumentUnderstandingServiceTransport(abc.ABC): - """Abstract transport class for DocumentUnderstandingService.""" - - AUTH_SCOPES = ("https://www.googleapis.com/auth/cloud-platform",) - - DEFAULT_HOST: str = "documentai.googleapis.com" - - def __init__( - self, - *, - host: str = DEFAULT_HOST, - credentials: Optional[ga_credentials.Credentials] = None, - credentials_file: Optional[str] = None, - scopes: Optional[Sequence[str]] = None, - quota_project_id: Optional[str] = None, - client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, - always_use_jwt_access: Optional[bool] = False, - api_audience: Optional[str] = None, - **kwargs, - ) -> None: - """Instantiate the transport. - - Args: - host (Optional[str]): - The hostname to connect to (default: 'documentai.googleapis.com'). - credentials (Optional[google.auth.credentials.Credentials]): The - authorization credentials to attach to requests. These - credentials identify the application to the service; if none - are specified, the client will attempt to ascertain the - credentials from the environment. - credentials_file (Optional[str]): A file with credentials that can - be loaded with :func:`google.auth.load_credentials_from_file`. - This argument is mutually exclusive with credentials. - scopes (Optional[Sequence[str]]): A list of scopes. - quota_project_id (Optional[str]): An optional project to use for billing - and quota. - client_info (google.api_core.gapic_v1.client_info.ClientInfo): - The client info used to send a user-agent string along with - API requests. If ``None``, then default info will be used. - Generally, you only need to set this if you're developing - your own client library. - always_use_jwt_access (Optional[bool]): Whether self signed JWT should - be used for service account credentials. - """ - - scopes_kwargs = {"scopes": scopes, "default_scopes": self.AUTH_SCOPES} - - # Save the scopes. - self._scopes = scopes - if not hasattr(self, "_ignore_credentials"): - self._ignore_credentials: bool = False - - # If no credentials are provided, then determine the appropriate - # defaults. - if credentials and credentials_file: - raise core_exceptions.DuplicateCredentialArgs( - "'credentials_file' and 'credentials' are mutually exclusive" - ) - - if credentials_file is not None: - credentials, _ = google.auth.load_credentials_from_file( - credentials_file, **scopes_kwargs, quota_project_id=quota_project_id - ) - elif credentials is None and not self._ignore_credentials: - credentials, _ = google.auth.default( - **scopes_kwargs, quota_project_id=quota_project_id - ) - # Don't apply audience if the credentials file passed from user. - if hasattr(credentials, "with_gdch_audience"): - credentials = credentials.with_gdch_audience( - api_audience if api_audience else host - ) - - # If the credentials are service account credentials, then always try to use self signed JWT. - if ( - always_use_jwt_access - and isinstance(credentials, service_account.Credentials) - and hasattr(service_account.Credentials, "with_always_use_jwt_access") - ): - credentials = credentials.with_always_use_jwt_access(True) - - # Save the credentials. - self._credentials = credentials - - # Save the hostname. Default to port 443 (HTTPS) if none is specified. - if ":" not in host: - host += ":443" - self._host = host - - @property - def host(self): - return self._host - - def _prep_wrapped_messages(self, client_info): - # Precompute the wrapped methods. - self._wrapped_methods = { - self.batch_process_documents: gapic_v1.method.wrap_method( - self.batch_process_documents, - default_retry=retries.Retry( - initial=0.1, - maximum=60.0, - multiplier=1.3, - predicate=retries.if_exception_type( - core_exceptions.DeadlineExceeded, - core_exceptions.ServiceUnavailable, - ), - deadline=120.0, - ), - default_timeout=120.0, - client_info=client_info, - ), - self.process_document: gapic_v1.method.wrap_method( - self.process_document, - default_retry=retries.Retry( - initial=0.1, - maximum=60.0, - multiplier=1.3, - predicate=retries.if_exception_type( - core_exceptions.DeadlineExceeded, - core_exceptions.ServiceUnavailable, - ), - deadline=120.0, - ), - default_timeout=120.0, - client_info=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 operations_client(self): - """Return the client designed to process long-running operations.""" - raise NotImplementedError() - - @property - def batch_process_documents( - self, - ) -> Callable[ - [document_understanding.BatchProcessDocumentsRequest], - Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]], - ]: - raise NotImplementedError() - - @property - def process_document( - self, - ) -> Callable[ - [document_understanding.ProcessDocumentRequest], - Union[document.Document, Awaitable[document.Document]], - ]: - raise NotImplementedError() - - @property - def list_operations( - self, - ) -> Callable[ - [operations_pb2.ListOperationsRequest], - Union[ - operations_pb2.ListOperationsResponse, - Awaitable[operations_pb2.ListOperationsResponse], - ], - ]: - raise NotImplementedError() - - @property - def get_operation( - self, - ) -> Callable[ - [operations_pb2.GetOperationRequest], - Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]], - ]: - raise NotImplementedError() - - @property - def cancel_operation( - self, - ) -> Callable[[operations_pb2.CancelOperationRequest], None,]: - raise NotImplementedError() - - @property - def get_location( - self, - ) -> Callable[ - [locations_pb2.GetLocationRequest], - Union[locations_pb2.Location, Awaitable[locations_pb2.Location]], - ]: - raise NotImplementedError() - - @property - def list_locations( - self, - ) -> Callable[ - [locations_pb2.ListLocationsRequest], - Union[ - locations_pb2.ListLocationsResponse, - Awaitable[locations_pb2.ListLocationsResponse], - ], - ]: - raise NotImplementedError() - - @property - def kind(self) -> str: - raise NotImplementedError() - - -__all__ = ("DocumentUnderstandingServiceTransport",) diff --git a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/document_understanding_service/transports/grpc.py b/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/document_understanding_service/transports/grpc.py deleted file mode 100644 index 4f3d9967aeec..000000000000 --- a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/document_understanding_service/transports/grpc.py +++ /dev/null @@ -1,408 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2024 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -from typing import Callable, Dict, Optional, Sequence, Tuple, Union -import warnings - -from google.api_core import gapic_v1, grpc_helpers, operations_v1 -import google.auth # type: ignore -from google.auth import credentials as ga_credentials # type: ignore -from google.auth.transport.grpc import SslCredentials # type: ignore -from google.cloud.location import locations_pb2 # type: ignore -from google.longrunning import operations_pb2 # type: ignore -import grpc # type: ignore - -from google.cloud.documentai_v1beta2.types import document, document_understanding - -from .base import DEFAULT_CLIENT_INFO, DocumentUnderstandingServiceTransport - - -class DocumentUnderstandingServiceGrpcTransport(DocumentUnderstandingServiceTransport): - """gRPC backend transport for DocumentUnderstandingService. - - Service to parse structured information from unstructured or - semi-structured documents using state-of-the-art Google AI such - as natural language, computer vision, and translation. - - This class defines the same methods as the primary client, so the - primary client can load the underlying transport implementation - and call it. - - It sends protocol buffers over the wire using gRPC (which is built on - top of HTTP/2); the ``grpcio`` package must be installed. - """ - - _stubs: Dict[str, Callable] - - def __init__( - self, - *, - host: str = "documentai.googleapis.com", - credentials: Optional[ga_credentials.Credentials] = None, - credentials_file: Optional[str] = None, - scopes: Optional[Sequence[str]] = None, - channel: Optional[Union[grpc.Channel, Callable[..., grpc.Channel]]] = None, - api_mtls_endpoint: Optional[str] = None, - client_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]] = None, - ssl_channel_credentials: Optional[grpc.ChannelCredentials] = None, - client_cert_source_for_mtls: Optional[Callable[[], Tuple[bytes, bytes]]] = None, - quota_project_id: Optional[str] = None, - client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, - always_use_jwt_access: Optional[bool] = False, - api_audience: Optional[str] = None, - ) -> None: - """Instantiate the transport. - - Args: - host (Optional[str]): - The hostname to connect to (default: 'documentai.googleapis.com'). - credentials (Optional[google.auth.credentials.Credentials]): The - authorization credentials to attach to requests. These - credentials identify the application to the service; if none - are specified, the client will attempt to ascertain the - credentials from the environment. - This argument is ignored if a ``channel`` instance is provided. - credentials_file (Optional[str]): A file with credentials that can - be loaded with :func:`google.auth.load_credentials_from_file`. - This argument is ignored if a ``channel`` instance is provided. - scopes (Optional(Sequence[str])): A list of scopes. This argument is - ignored if a ``channel`` instance is provided. - channel (Optional[Union[grpc.Channel, Callable[..., grpc.Channel]]]): - A ``Channel`` instance through which to make calls, or a Callable - that constructs and returns one. If set to None, ``self.create_channel`` - is used to create the channel. If a Callable is given, it will be called - with the same arguments as used in ``self.create_channel``. - api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint. - If provided, it overrides the ``host`` argument and tries to create - a mutual TLS channel with client SSL credentials from - ``client_cert_source`` or application default SSL credentials. - client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): - Deprecated. A callback to provide client SSL certificate bytes and - private key bytes, both in PEM format. It is ignored if - ``api_mtls_endpoint`` is None. - ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials - for the grpc channel. It is ignored if a ``channel`` instance is provided. - client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]): - A callback to provide client certificate bytes and private key bytes, - both in PEM format. It is used to configure a mutual TLS channel. It is - ignored if a ``channel`` instance or ``ssl_channel_credentials`` is provided. - quota_project_id (Optional[str]): An optional project to use for billing - and quota. - client_info (google.api_core.gapic_v1.client_info.ClientInfo): - The client info used to send a user-agent string along with - API requests. If ``None``, then default info will be used. - Generally, you only need to set this if you're developing - your own client library. - always_use_jwt_access (Optional[bool]): Whether self signed JWT should - be used for service account credentials. - - Raises: - google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport - creation failed for any reason. - google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` - and ``credentials_file`` are passed. - """ - self._grpc_channel = None - self._ssl_channel_credentials = ssl_channel_credentials - self._stubs: Dict[str, Callable] = {} - self._operations_client: Optional[operations_v1.OperationsClient] = None - - if api_mtls_endpoint: - warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning) - if client_cert_source: - warnings.warn("client_cert_source is deprecated", DeprecationWarning) - - if isinstance(channel, grpc.Channel): - # Ignore credentials if a channel was passed. - credentials = None - self._ignore_credentials = True - # If a channel was explicitly provided, set it. - self._grpc_channel = channel - self._ssl_channel_credentials = None - - else: - if api_mtls_endpoint: - host = api_mtls_endpoint - - # Create SSL credentials with client_cert_source or application - # default SSL credentials. - if client_cert_source: - cert, key = client_cert_source() - self._ssl_channel_credentials = grpc.ssl_channel_credentials( - certificate_chain=cert, private_key=key - ) - else: - self._ssl_channel_credentials = SslCredentials().ssl_credentials - - else: - if client_cert_source_for_mtls and not ssl_channel_credentials: - cert, key = client_cert_source_for_mtls() - self._ssl_channel_credentials = grpc.ssl_channel_credentials( - certificate_chain=cert, private_key=key - ) - - # The base transport sets the host, credentials and scopes - super().__init__( - host=host, - credentials=credentials, - credentials_file=credentials_file, - scopes=scopes, - quota_project_id=quota_project_id, - client_info=client_info, - always_use_jwt_access=always_use_jwt_access, - api_audience=api_audience, - ) - - if not self._grpc_channel: - # initialize with the provided callable or the default channel - channel_init = channel or type(self).create_channel - self._grpc_channel = channel_init( - self._host, - # use the credentials which are saved - credentials=self._credentials, - # Set ``credentials_file`` to ``None`` here as - # the credentials that we saved earlier should be used. - credentials_file=None, - scopes=self._scopes, - ssl_credentials=self._ssl_channel_credentials, - quota_project_id=quota_project_id, - options=[ - ("grpc.max_send_message_length", -1), - ("grpc.max_receive_message_length", -1), - ], - ) - - # Wrap messages. This must be done after self._grpc_channel exists - self._prep_wrapped_messages(client_info) - - @classmethod - def create_channel( - cls, - host: str = "documentai.googleapis.com", - credentials: Optional[ga_credentials.Credentials] = None, - credentials_file: Optional[str] = None, - scopes: Optional[Sequence[str]] = None, - quota_project_id: Optional[str] = None, - **kwargs, - ) -> grpc.Channel: - """Create and return a gRPC channel object. - Args: - host (Optional[str]): The host for the channel to use. - credentials (Optional[~.Credentials]): The - authorization credentials to attach to requests. These - credentials identify this application to the service. If - none are specified, the client will attempt to ascertain - the credentials from the environment. - credentials_file (Optional[str]): A file with credentials that can - be loaded with :func:`google.auth.load_credentials_from_file`. - This argument is mutually exclusive with credentials. - scopes (Optional[Sequence[str]]): A optional list of scopes needed for this - service. These are only used when credentials are not specified and - are passed to :func:`google.auth.default`. - quota_project_id (Optional[str]): An optional project to use for billing - and quota. - kwargs (Optional[dict]): Keyword arguments, which are passed to the - channel creation. - Returns: - grpc.Channel: A gRPC channel object. - - Raises: - google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` - and ``credentials_file`` are passed. - """ - - return grpc_helpers.create_channel( - host, - credentials=credentials, - credentials_file=credentials_file, - quota_project_id=quota_project_id, - default_scopes=cls.AUTH_SCOPES, - scopes=scopes, - default_host=cls.DEFAULT_HOST, - **kwargs, - ) - - @property - def grpc_channel(self) -> grpc.Channel: - """Return the channel designed to connect to this service.""" - return self._grpc_channel - - @property - def operations_client(self) -> operations_v1.OperationsClient: - """Create the client designed to process long-running operations. - - This property caches on the instance; repeated calls return the same - client. - """ - # Quick check: Only create a new client if we do not already have one. - if self._operations_client is None: - self._operations_client = operations_v1.OperationsClient(self.grpc_channel) - - # Return the client from cache. - return self._operations_client - - @property - def batch_process_documents( - self, - ) -> Callable[ - [document_understanding.BatchProcessDocumentsRequest], operations_pb2.Operation - ]: - r"""Return a callable for the batch process documents method over gRPC. - - LRO endpoint to batch process many documents. The output is - written to Cloud Storage as JSON in the [Document] format. - - Returns: - Callable[[~.BatchProcessDocumentsRequest], - ~.Operation]: - A function that, when called, will call the underlying RPC - on the server. - """ - # Generate a "stub function" on-the-fly which will actually make - # the request. - # gRPC handles serialization and deserialization, so we just need - # to pass in the functions for each. - if "batch_process_documents" not in self._stubs: - self._stubs["batch_process_documents"] = self.grpc_channel.unary_unary( - "/google.cloud.documentai.v1beta2.DocumentUnderstandingService/BatchProcessDocuments", - request_serializer=document_understanding.BatchProcessDocumentsRequest.serialize, - response_deserializer=operations_pb2.Operation.FromString, - ) - return self._stubs["batch_process_documents"] - - @property - def process_document( - self, - ) -> Callable[[document_understanding.ProcessDocumentRequest], document.Document]: - r"""Return a callable for the process document method over gRPC. - - Processes a single document. - - Returns: - Callable[[~.ProcessDocumentRequest], - ~.Document]: - A function that, when called, will call the underlying RPC - on the server. - """ - # Generate a "stub function" on-the-fly which will actually make - # the request. - # gRPC handles serialization and deserialization, so we just need - # to pass in the functions for each. - if "process_document" not in self._stubs: - self._stubs["process_document"] = self.grpc_channel.unary_unary( - "/google.cloud.documentai.v1beta2.DocumentUnderstandingService/ProcessDocument", - request_serializer=document_understanding.ProcessDocumentRequest.serialize, - response_deserializer=document.Document.deserialize, - ) - return self._stubs["process_document"] - - def close(self): - self.grpc_channel.close() - - @property - def cancel_operation( - self, - ) -> Callable[[operations_pb2.CancelOperationRequest], None]: - r"""Return a callable for the cancel_operation method over gRPC.""" - # Generate a "stub function" on-the-fly which will actually make - # the request. - # gRPC handles serialization and deserialization, so we just need - # to pass in the functions for each. - if "cancel_operation" not in self._stubs: - self._stubs["cancel_operation"] = self.grpc_channel.unary_unary( - "/google.longrunning.Operations/CancelOperation", - request_serializer=operations_pb2.CancelOperationRequest.SerializeToString, - response_deserializer=None, - ) - return self._stubs["cancel_operation"] - - @property - def get_operation( - self, - ) -> Callable[[operations_pb2.GetOperationRequest], operations_pb2.Operation]: - r"""Return a callable for the get_operation method over gRPC.""" - # Generate a "stub function" on-the-fly which will actually make - # the request. - # gRPC handles serialization and deserialization, so we just need - # to pass in the functions for each. - if "get_operation" not in self._stubs: - self._stubs["get_operation"] = self.grpc_channel.unary_unary( - "/google.longrunning.Operations/GetOperation", - request_serializer=operations_pb2.GetOperationRequest.SerializeToString, - response_deserializer=operations_pb2.Operation.FromString, - ) - return self._stubs["get_operation"] - - @property - def list_operations( - self, - ) -> Callable[ - [operations_pb2.ListOperationsRequest], operations_pb2.ListOperationsResponse - ]: - r"""Return a callable for the list_operations method over gRPC.""" - # Generate a "stub function" on-the-fly which will actually make - # the request. - # gRPC handles serialization and deserialization, so we just need - # to pass in the functions for each. - if "list_operations" not in self._stubs: - self._stubs["list_operations"] = self.grpc_channel.unary_unary( - "/google.longrunning.Operations/ListOperations", - request_serializer=operations_pb2.ListOperationsRequest.SerializeToString, - response_deserializer=operations_pb2.ListOperationsResponse.FromString, - ) - return self._stubs["list_operations"] - - @property - def list_locations( - self, - ) -> Callable[ - [locations_pb2.ListLocationsRequest], locations_pb2.ListLocationsResponse - ]: - r"""Return a callable for the list locations method over gRPC.""" - # Generate a "stub function" on-the-fly which will actually make - # the request. - # gRPC handles serialization and deserialization, so we just need - # to pass in the functions for each. - if "list_locations" not in self._stubs: - self._stubs["list_locations"] = self.grpc_channel.unary_unary( - "/google.cloud.location.Locations/ListLocations", - request_serializer=locations_pb2.ListLocationsRequest.SerializeToString, - response_deserializer=locations_pb2.ListLocationsResponse.FromString, - ) - return self._stubs["list_locations"] - - @property - def get_location( - self, - ) -> Callable[[locations_pb2.GetLocationRequest], locations_pb2.Location]: - r"""Return a callable for the list locations method over gRPC.""" - # Generate a "stub function" on-the-fly which will actually make - # the request. - # gRPC handles serialization and deserialization, so we just need - # to pass in the functions for each. - if "get_location" not in self._stubs: - self._stubs["get_location"] = self.grpc_channel.unary_unary( - "/google.cloud.location.Locations/GetLocation", - request_serializer=locations_pb2.GetLocationRequest.SerializeToString, - response_deserializer=locations_pb2.Location.FromString, - ) - return self._stubs["get_location"] - - @property - def kind(self) -> str: - return "grpc" - - -__all__ = ("DocumentUnderstandingServiceGrpcTransport",) diff --git a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/document_understanding_service/transports/grpc_asyncio.py b/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/document_understanding_service/transports/grpc_asyncio.py deleted file mode 100644 index e7541c534b38..000000000000 --- a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/services/document_understanding_service/transports/grpc_asyncio.py +++ /dev/null @@ -1,450 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2024 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple, Union -import warnings - -from google.api_core import exceptions as core_exceptions -from google.api_core import gapic_v1, grpc_helpers_async, operations_v1 -from google.api_core import retry_async as retries -from google.auth import credentials as ga_credentials # type: ignore -from google.auth.transport.grpc import SslCredentials # type: ignore -from google.cloud.location import locations_pb2 # type: ignore -from google.longrunning import operations_pb2 # type: ignore -import grpc # type: ignore -from grpc.experimental import aio # type: ignore - -from google.cloud.documentai_v1beta2.types import document, document_understanding - -from .base import DEFAULT_CLIENT_INFO, DocumentUnderstandingServiceTransport -from .grpc import DocumentUnderstandingServiceGrpcTransport - - -class DocumentUnderstandingServiceGrpcAsyncIOTransport( - DocumentUnderstandingServiceTransport -): - """gRPC AsyncIO backend transport for DocumentUnderstandingService. - - Service to parse structured information from unstructured or - semi-structured documents using state-of-the-art Google AI such - as natural language, computer vision, and translation. - - This class defines the same methods as the primary client, so the - primary client can load the underlying transport implementation - and call it. - - It sends protocol buffers over the wire using gRPC (which is built on - top of HTTP/2); the ``grpcio`` package must be installed. - """ - - _grpc_channel: aio.Channel - _stubs: Dict[str, Callable] = {} - - @classmethod - def create_channel( - cls, - host: str = "documentai.googleapis.com", - credentials: Optional[ga_credentials.Credentials] = None, - credentials_file: Optional[str] = None, - scopes: Optional[Sequence[str]] = None, - quota_project_id: Optional[str] = None, - **kwargs, - ) -> aio.Channel: - """Create and return a gRPC AsyncIO channel object. - Args: - host (Optional[str]): The host for the channel to use. - credentials (Optional[~.Credentials]): The - authorization credentials to attach to requests. These - credentials identify this application to the service. If - none are specified, the client will attempt to ascertain - the credentials from the environment. - credentials_file (Optional[str]): A file with credentials that can - be loaded with :func:`google.auth.load_credentials_from_file`. - scopes (Optional[Sequence[str]]): A optional list of scopes needed for this - service. These are only used when credentials are not specified and - are passed to :func:`google.auth.default`. - quota_project_id (Optional[str]): An optional project to use for billing - and quota. - kwargs (Optional[dict]): Keyword arguments, which are passed to the - channel creation. - Returns: - aio.Channel: A gRPC AsyncIO channel object. - """ - - return grpc_helpers_async.create_channel( - host, - credentials=credentials, - credentials_file=credentials_file, - quota_project_id=quota_project_id, - default_scopes=cls.AUTH_SCOPES, - scopes=scopes, - default_host=cls.DEFAULT_HOST, - **kwargs, - ) - - def __init__( - self, - *, - host: str = "documentai.googleapis.com", - credentials: Optional[ga_credentials.Credentials] = None, - credentials_file: Optional[str] = None, - scopes: Optional[Sequence[str]] = None, - channel: Optional[Union[aio.Channel, Callable[..., aio.Channel]]] = None, - api_mtls_endpoint: Optional[str] = None, - client_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]] = None, - ssl_channel_credentials: Optional[grpc.ChannelCredentials] = None, - client_cert_source_for_mtls: Optional[Callable[[], Tuple[bytes, bytes]]] = None, - quota_project_id: Optional[str] = None, - client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, - always_use_jwt_access: Optional[bool] = False, - api_audience: Optional[str] = None, - ) -> None: - """Instantiate the transport. - - Args: - host (Optional[str]): - The hostname to connect to (default: 'documentai.googleapis.com'). - credentials (Optional[google.auth.credentials.Credentials]): The - authorization credentials to attach to requests. These - credentials identify the application to the service; if none - are specified, the client will attempt to ascertain the - credentials from the environment. - This argument is ignored if a ``channel`` instance is provided. - credentials_file (Optional[str]): A file with credentials that can - be loaded with :func:`google.auth.load_credentials_from_file`. - This argument is ignored if a ``channel`` instance is provided. - scopes (Optional[Sequence[str]]): A optional list of scopes needed for this - service. These are only used when credentials are not specified and - are passed to :func:`google.auth.default`. - channel (Optional[Union[aio.Channel, Callable[..., aio.Channel]]]): - A ``Channel`` instance through which to make calls, or a Callable - that constructs and returns one. If set to None, ``self.create_channel`` - is used to create the channel. If a Callable is given, it will be called - with the same arguments as used in ``self.create_channel``. - api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint. - If provided, it overrides the ``host`` argument and tries to create - a mutual TLS channel with client SSL credentials from - ``client_cert_source`` or application default SSL credentials. - client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): - Deprecated. A callback to provide client SSL certificate bytes and - private key bytes, both in PEM format. It is ignored if - ``api_mtls_endpoint`` is None. - ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials - for the grpc channel. It is ignored if a ``channel`` instance is provided. - client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]): - A callback to provide client certificate bytes and private key bytes, - both in PEM format. It is used to configure a mutual TLS channel. It is - ignored if a ``channel`` instance or ``ssl_channel_credentials`` is provided. - quota_project_id (Optional[str]): An optional project to use for billing - and quota. - client_info (google.api_core.gapic_v1.client_info.ClientInfo): - The client info used to send a user-agent string along with - API requests. If ``None``, then default info will be used. - Generally, you only need to set this if you're developing - your own client library. - always_use_jwt_access (Optional[bool]): Whether self signed JWT should - be used for service account credentials. - - Raises: - google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport - creation failed for any reason. - google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` - and ``credentials_file`` are passed. - """ - self._grpc_channel = None - self._ssl_channel_credentials = ssl_channel_credentials - self._stubs: Dict[str, Callable] = {} - self._operations_client: Optional[operations_v1.OperationsAsyncClient] = None - - if api_mtls_endpoint: - warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning) - if client_cert_source: - warnings.warn("client_cert_source is deprecated", DeprecationWarning) - - if isinstance(channel, aio.Channel): - # Ignore credentials if a channel was passed. - credentials = None - self._ignore_credentials = True - # If a channel was explicitly provided, set it. - self._grpc_channel = channel - self._ssl_channel_credentials = None - else: - if api_mtls_endpoint: - host = api_mtls_endpoint - - # Create SSL credentials with client_cert_source or application - # default SSL credentials. - if client_cert_source: - cert, key = client_cert_source() - self._ssl_channel_credentials = grpc.ssl_channel_credentials( - certificate_chain=cert, private_key=key - ) - else: - self._ssl_channel_credentials = SslCredentials().ssl_credentials - - else: - if client_cert_source_for_mtls and not ssl_channel_credentials: - cert, key = client_cert_source_for_mtls() - self._ssl_channel_credentials = grpc.ssl_channel_credentials( - certificate_chain=cert, private_key=key - ) - - # The base transport sets the host, credentials and scopes - super().__init__( - host=host, - credentials=credentials, - credentials_file=credentials_file, - scopes=scopes, - quota_project_id=quota_project_id, - client_info=client_info, - always_use_jwt_access=always_use_jwt_access, - api_audience=api_audience, - ) - - if not self._grpc_channel: - # initialize with the provided callable or the default channel - channel_init = channel or type(self).create_channel - self._grpc_channel = channel_init( - self._host, - # use the credentials which are saved - credentials=self._credentials, - # Set ``credentials_file`` to ``None`` here as - # the credentials that we saved earlier should be used. - credentials_file=None, - scopes=self._scopes, - ssl_credentials=self._ssl_channel_credentials, - quota_project_id=quota_project_id, - options=[ - ("grpc.max_send_message_length", -1), - ("grpc.max_receive_message_length", -1), - ], - ) - - # Wrap messages. This must be done after self._grpc_channel exists - self._prep_wrapped_messages(client_info) - - @property - def grpc_channel(self) -> aio.Channel: - """Create the channel designed to connect to this service. - - This property caches on the instance; repeated calls return - the same channel. - """ - # Return the channel from cache. - return self._grpc_channel - - @property - def operations_client(self) -> operations_v1.OperationsAsyncClient: - """Create the client designed to process long-running operations. - - This property caches on the instance; repeated calls return the same - client. - """ - # Quick check: Only create a new client if we do not already have one. - if self._operations_client is None: - self._operations_client = operations_v1.OperationsAsyncClient( - self.grpc_channel - ) - - # Return the client from cache. - return self._operations_client - - @property - def batch_process_documents( - self, - ) -> Callable[ - [document_understanding.BatchProcessDocumentsRequest], - Awaitable[operations_pb2.Operation], - ]: - r"""Return a callable for the batch process documents method over gRPC. - - LRO endpoint to batch process many documents. The output is - written to Cloud Storage as JSON in the [Document] format. - - Returns: - Callable[[~.BatchProcessDocumentsRequest], - Awaitable[~.Operation]]: - A function that, when called, will call the underlying RPC - on the server. - """ - # Generate a "stub function" on-the-fly which will actually make - # the request. - # gRPC handles serialization and deserialization, so we just need - # to pass in the functions for each. - if "batch_process_documents" not in self._stubs: - self._stubs["batch_process_documents"] = self.grpc_channel.unary_unary( - "/google.cloud.documentai.v1beta2.DocumentUnderstandingService/BatchProcessDocuments", - request_serializer=document_understanding.BatchProcessDocumentsRequest.serialize, - response_deserializer=operations_pb2.Operation.FromString, - ) - return self._stubs["batch_process_documents"] - - @property - def process_document( - self, - ) -> Callable[ - [document_understanding.ProcessDocumentRequest], Awaitable[document.Document] - ]: - r"""Return a callable for the process document method over gRPC. - - Processes a single document. - - Returns: - Callable[[~.ProcessDocumentRequest], - Awaitable[~.Document]]: - A function that, when called, will call the underlying RPC - on the server. - """ - # Generate a "stub function" on-the-fly which will actually make - # the request. - # gRPC handles serialization and deserialization, so we just need - # to pass in the functions for each. - if "process_document" not in self._stubs: - self._stubs["process_document"] = self.grpc_channel.unary_unary( - "/google.cloud.documentai.v1beta2.DocumentUnderstandingService/ProcessDocument", - request_serializer=document_understanding.ProcessDocumentRequest.serialize, - response_deserializer=document.Document.deserialize, - ) - return self._stubs["process_document"] - - def _prep_wrapped_messages(self, client_info): - """Precompute the wrapped methods, overriding the base class method to use async wrappers.""" - self._wrapped_methods = { - self.batch_process_documents: gapic_v1.method_async.wrap_method( - self.batch_process_documents, - default_retry=retries.AsyncRetry( - initial=0.1, - maximum=60.0, - multiplier=1.3, - predicate=retries.if_exception_type( - core_exceptions.DeadlineExceeded, - core_exceptions.ServiceUnavailable, - ), - deadline=120.0, - ), - default_timeout=120.0, - client_info=client_info, - ), - self.process_document: gapic_v1.method_async.wrap_method( - self.process_document, - default_retry=retries.AsyncRetry( - initial=0.1, - maximum=60.0, - multiplier=1.3, - predicate=retries.if_exception_type( - core_exceptions.DeadlineExceeded, - core_exceptions.ServiceUnavailable, - ), - deadline=120.0, - ), - default_timeout=120.0, - client_info=client_info, - ), - } - - def close(self): - return self.grpc_channel.close() - - @property - def cancel_operation( - self, - ) -> Callable[[operations_pb2.CancelOperationRequest], None]: - r"""Return a callable for the cancel_operation method over gRPC.""" - # Generate a "stub function" on-the-fly which will actually make - # the request. - # gRPC handles serialization and deserialization, so we just need - # to pass in the functions for each. - if "cancel_operation" not in self._stubs: - self._stubs["cancel_operation"] = self.grpc_channel.unary_unary( - "/google.longrunning.Operations/CancelOperation", - request_serializer=operations_pb2.CancelOperationRequest.SerializeToString, - response_deserializer=None, - ) - return self._stubs["cancel_operation"] - - @property - def get_operation( - self, - ) -> Callable[[operations_pb2.GetOperationRequest], operations_pb2.Operation]: - r"""Return a callable for the get_operation method over gRPC.""" - # Generate a "stub function" on-the-fly which will actually make - # the request. - # gRPC handles serialization and deserialization, so we just need - # to pass in the functions for each. - if "get_operation" not in self._stubs: - self._stubs["get_operation"] = self.grpc_channel.unary_unary( - "/google.longrunning.Operations/GetOperation", - request_serializer=operations_pb2.GetOperationRequest.SerializeToString, - response_deserializer=operations_pb2.Operation.FromString, - ) - return self._stubs["get_operation"] - - @property - def list_operations( - self, - ) -> Callable[ - [operations_pb2.ListOperationsRequest], operations_pb2.ListOperationsResponse - ]: - r"""Return a callable for the list_operations method over gRPC.""" - # Generate a "stub function" on-the-fly which will actually make - # the request. - # gRPC handles serialization and deserialization, so we just need - # to pass in the functions for each. - if "list_operations" not in self._stubs: - self._stubs["list_operations"] = self.grpc_channel.unary_unary( - "/google.longrunning.Operations/ListOperations", - request_serializer=operations_pb2.ListOperationsRequest.SerializeToString, - response_deserializer=operations_pb2.ListOperationsResponse.FromString, - ) - return self._stubs["list_operations"] - - @property - def list_locations( - self, - ) -> Callable[ - [locations_pb2.ListLocationsRequest], locations_pb2.ListLocationsResponse - ]: - r"""Return a callable for the list locations method over gRPC.""" - # Generate a "stub function" on-the-fly which will actually make - # the request. - # gRPC handles serialization and deserialization, so we just need - # to pass in the functions for each. - if "list_locations" not in self._stubs: - self._stubs["list_locations"] = self.grpc_channel.unary_unary( - "/google.cloud.location.Locations/ListLocations", - request_serializer=locations_pb2.ListLocationsRequest.SerializeToString, - response_deserializer=locations_pb2.ListLocationsResponse.FromString, - ) - return self._stubs["list_locations"] - - @property - def get_location( - self, - ) -> Callable[[locations_pb2.GetLocationRequest], locations_pb2.Location]: - r"""Return a callable for the list locations method over gRPC.""" - # Generate a "stub function" on-the-fly which will actually make - # the request. - # gRPC handles serialization and deserialization, so we just need - # to pass in the functions for each. - if "get_location" not in self._stubs: - self._stubs["get_location"] = self.grpc_channel.unary_unary( - "/google.cloud.location.Locations/GetLocation", - request_serializer=locations_pb2.GetLocationRequest.SerializeToString, - response_deserializer=locations_pb2.Location.FromString, - ) - return self._stubs["get_location"] - - -__all__ = ("DocumentUnderstandingServiceGrpcAsyncIOTransport",) diff --git a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/types/__init__.py b/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/types/__init__.py deleted file mode 100644 index d98091dca051..000000000000 --- a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/types/__init__.py +++ /dev/null @@ -1,60 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2024 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -from .barcode import Barcode -from .document import Document -from .document_understanding import ( - AutoMlParams, - BatchProcessDocumentsRequest, - BatchProcessDocumentsResponse, - EntityExtractionParams, - FormExtractionParams, - GcsDestination, - GcsSource, - InputConfig, - KeyValuePairHint, - OcrParams, - OperationMetadata, - OutputConfig, - ProcessDocumentRequest, - ProcessDocumentResponse, - TableBoundHint, - TableExtractionParams, -) -from .geometry import BoundingPoly, NormalizedVertex, Vertex - -__all__ = ( - "Barcode", - "Document", - "AutoMlParams", - "BatchProcessDocumentsRequest", - "BatchProcessDocumentsResponse", - "EntityExtractionParams", - "FormExtractionParams", - "GcsDestination", - "GcsSource", - "InputConfig", - "KeyValuePairHint", - "OcrParams", - "OperationMetadata", - "OutputConfig", - "ProcessDocumentRequest", - "ProcessDocumentResponse", - "TableBoundHint", - "TableExtractionParams", - "BoundingPoly", - "NormalizedVertex", - "Vertex", -) diff --git a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/types/barcode.py b/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/types/barcode.py deleted file mode 100644 index 12f6b3bf6406..000000000000 --- a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/types/barcode.py +++ /dev/null @@ -1,86 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2024 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -from __future__ import annotations - -from typing import MutableMapping, MutableSequence - -import proto # type: ignore - -__protobuf__ = proto.module( - package="google.cloud.documentai.v1beta2", - manifest={ - "Barcode", - }, -) - - -class Barcode(proto.Message): - r"""Encodes the detailed information of a barcode. - - Attributes: - format_ (str): - Format of a barcode. The supported formats are: - - - ``CODE_128``: Code 128 type. - - ``CODE_39``: Code 39 type. - - ``CODE_93``: Code 93 type. - - ``CODABAR``: Codabar type. - - ``DATA_MATRIX``: 2D Data Matrix type. - - ``ITF``: ITF type. - - ``EAN_13``: EAN-13 type. - - ``EAN_8``: EAN-8 type. - - ``QR_CODE``: 2D QR code type. - - ``UPC_A``: UPC-A type. - - ``UPC_E``: UPC-E type. - - ``PDF417``: PDF417 type. - - ``AZTEC``: 2D Aztec code type. - - ``DATABAR``: GS1 DataBar code type. - value_format (str): - Value format describes the format of the value that a - barcode encodes. The supported formats are: - - - ``CONTACT_INFO``: Contact information. - - ``EMAIL``: Email address. - - ``ISBN``: ISBN identifier. - - ``PHONE``: Phone number. - - ``PRODUCT``: Product. - - ``SMS``: SMS message. - - ``TEXT``: Text string. - - ``URL``: URL address. - - ``WIFI``: Wifi information. - - ``GEO``: Geo-localization. - - ``CALENDAR_EVENT``: Calendar event. - - ``DRIVER_LICENSE``: Driver's license. - raw_value (str): - Raw value encoded in the barcode. For example: - ``'MEBKM:TITLE:Google;URL:https://www.google.com;;'``. - """ - - format_: str = proto.Field( - proto.STRING, - number=1, - ) - value_format: str = proto.Field( - proto.STRING, - number=2, - ) - raw_value: str = proto.Field( - proto.STRING, - number=3, - ) - - -__all__ = tuple(sorted(__protobuf__.manifest)) diff --git a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/types/document.py b/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/types/document.py deleted file mode 100644 index 8f227eb67852..000000000000 --- a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/types/document.py +++ /dev/null @@ -1,1926 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2024 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -from __future__ import annotations - -from typing import MutableMapping, MutableSequence - -from google.protobuf import timestamp_pb2 # type: ignore -from google.rpc import status_pb2 # type: ignore -from google.type import color_pb2 # type: ignore -from google.type import date_pb2 # type: ignore -from google.type import datetime_pb2 # type: ignore -from google.type import money_pb2 # type: ignore -from google.type import postal_address_pb2 # type: ignore -import proto # type: ignore - -from google.cloud.documentai_v1beta2.types import barcode as gcd_barcode -from google.cloud.documentai_v1beta2.types import geometry - -__protobuf__ = proto.module( - package="google.cloud.documentai.v1beta2", - manifest={ - "Document", - }, -) - - -class Document(proto.Message): - r"""Document represents the canonical document resource in - Document AI. It is an interchange format that provides insights - into documents and allows for collaboration between users and - Document AI to iterate and optimize for quality. - - This message has `oneof`_ fields (mutually exclusive fields). - For each oneof, at most one member field can be set at the same time. - Setting any member of the oneof automatically clears all other - members. - - .. _oneof: https://proto-plus-python.readthedocs.io/en/stable/fields.html#oneofs-mutually-exclusive-fields - - Attributes: - uri (str): - Optional. Currently supports Google Cloud Storage URI of the - form ``gs://bucket_name/object_name``. Object versioning is - not supported. For more information, refer to `Google Cloud - Storage Request - URIs `__. - - This field is a member of `oneof`_ ``source``. - content (bytes): - Optional. Inline document content, represented as a stream - of bytes. Note: As with all ``bytes`` fields, protobuffers - use a pure binary representation, whereas JSON - representations use base64. - - This field is a member of `oneof`_ ``source``. - mime_type (str): - An IANA published `media type (MIME - type) `__. - text (str): - Optional. UTF-8 encoded text in reading order - from the document. - text_styles (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Style]): - Styles for the - [Document.text][google.cloud.documentai.v1beta2.Document.text]. - pages (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page]): - Visual page layout for the - [Document][google.cloud.documentai.v1beta2.Document]. - entities (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Entity]): - A list of entities detected on - [Document.text][google.cloud.documentai.v1beta2.Document.text]. - For document shards, entities in this list may cross shard - boundaries. - entity_relations (MutableSequence[google.cloud.documentai_v1beta2.types.Document.EntityRelation]): - Placeholder. Relationship among - [Document.entities][google.cloud.documentai.v1beta2.Document.entities]. - text_changes (MutableSequence[google.cloud.documentai_v1beta2.types.Document.TextChange]): - Placeholder. A list of text corrections made to - [Document.text][google.cloud.documentai.v1beta2.Document.text]. - This is usually used for annotating corrections to OCR - mistakes. Text changes for a given revision may not overlap - with each other. - shard_info (google.cloud.documentai_v1beta2.types.Document.ShardInfo): - Information about the sharding if this - document is sharded part of a larger document. - If the document is not sharded, this message is - not specified. - labels (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Label]): - [Label][google.cloud.documentai.v1beta2.Document.Label]s for - this document. - error (google.rpc.status_pb2.Status): - Any error that occurred while processing this - document. - revisions (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Revision]): - Placeholder. Revision history of this - document. - """ - - class ShardInfo(proto.Message): - r"""For a large document, sharding may be performed to produce - several document shards. Each document shard contains this field - to detail which shard it is. - - Attributes: - shard_index (int): - The 0-based index of this shard. - shard_count (int): - Total number of shards. - text_offset (int): - The index of the first character in - [Document.text][google.cloud.documentai.v1beta2.Document.text] - in the overall document global text. - """ - - shard_index: int = proto.Field( - proto.INT64, - number=1, - ) - shard_count: int = proto.Field( - proto.INT64, - number=2, - ) - text_offset: int = proto.Field( - proto.INT64, - number=3, - ) - - class Label(proto.Message): - r"""Label attaches schema information and/or other metadata to segments - within a [Document][google.cloud.documentai.v1beta2.Document]. - Multiple [Label][google.cloud.documentai.v1beta2.Document.Label]s on - a single field can denote either different labels, different - instances of the same label created at different times, or some - combination of both. - - - .. _oneof: https://proto-plus-python.readthedocs.io/en/stable/fields.html#oneofs-mutually-exclusive-fields - - Attributes: - automl_model (str): - Label is generated AutoML model. This field stores the full - resource name of the AutoML model. - - Format: - ``projects/{project-id}/locations/{location-id}/models/{model-id}`` - - This field is a member of `oneof`_ ``source``. - name (str): - Name of the label. - - When the label is generated from AutoML Text - Classification model, this field represents the - name of the category. - confidence (float): - Confidence score between 0 and 1 for label - assignment. - """ - - automl_model: str = proto.Field( - proto.STRING, - number=2, - oneof="source", - ) - name: str = proto.Field( - proto.STRING, - number=1, - ) - confidence: float = proto.Field( - proto.FLOAT, - number=3, - ) - - class Style(proto.Message): - r"""Annotation for common text style attributes. This adheres to - CSS conventions as much as possible. - - Attributes: - text_anchor (google.cloud.documentai_v1beta2.types.Document.TextAnchor): - Text anchor indexing into the - [Document.text][google.cloud.documentai.v1beta2.Document.text]. - color (google.type.color_pb2.Color): - Text color. - background_color (google.type.color_pb2.Color): - Text background color. - font_weight (str): - `Font - weight `__. - Possible values are ``normal``, ``bold``, ``bolder``, and - ``lighter``. - text_style (str): - `Text - style `__. - Possible values are ``normal``, ``italic``, and ``oblique``. - text_decoration (str): - `Text - decoration `__. - Follows CSS standard. - font_size (google.cloud.documentai_v1beta2.types.Document.Style.FontSize): - Font size. - font_family (str): - Font family such as ``Arial``, ``Times New Roman``. - https://www.w3schools.com/cssref/pr_font_font-family.asp - """ - - class FontSize(proto.Message): - r"""Font size with unit. - - Attributes: - size (float): - Font size for the text. - unit (str): - Unit for the font size. Follows CSS naming (such as ``in``, - ``px``, and ``pt``). - """ - - size: float = proto.Field( - proto.FLOAT, - number=1, - ) - unit: str = proto.Field( - proto.STRING, - number=2, - ) - - text_anchor: "Document.TextAnchor" = proto.Field( - proto.MESSAGE, - number=1, - message="Document.TextAnchor", - ) - color: color_pb2.Color = proto.Field( - proto.MESSAGE, - number=2, - message=color_pb2.Color, - ) - background_color: color_pb2.Color = proto.Field( - proto.MESSAGE, - number=3, - message=color_pb2.Color, - ) - font_weight: str = proto.Field( - proto.STRING, - number=4, - ) - text_style: str = proto.Field( - proto.STRING, - number=5, - ) - text_decoration: str = proto.Field( - proto.STRING, - number=6, - ) - font_size: "Document.Style.FontSize" = proto.Field( - proto.MESSAGE, - number=7, - message="Document.Style.FontSize", - ) - font_family: str = proto.Field( - proto.STRING, - number=8, - ) - - class Page(proto.Message): - r"""A page in a [Document][google.cloud.documentai.v1beta2.Document]. - - Attributes: - page_number (int): - 1-based index for current - [Page][google.cloud.documentai.v1beta2.Document.Page] in a - parent [Document][google.cloud.documentai.v1beta2.Document]. - Useful when a page is taken out of a - [Document][google.cloud.documentai.v1beta2.Document] for - individual processing. - image (google.cloud.documentai_v1beta2.types.Document.Page.Image): - Rendered image for this page. This image is - preprocessed to remove any skew, rotation, and - distortions such that the annotation bounding - boxes can be upright and axis-aligned. - transforms (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.Matrix]): - Transformation matrices that were applied to the original - document image to produce - [Page.image][google.cloud.documentai.v1beta2.Document.Page.image]. - dimension (google.cloud.documentai_v1beta2.types.Document.Page.Dimension): - Physical dimension of the page. - layout (google.cloud.documentai_v1beta2.types.Document.Page.Layout): - [Layout][google.cloud.documentai.v1beta2.Document.Page.Layout] - for the page. - detected_languages (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.DetectedLanguage]): - A list of detected languages together with - confidence. - blocks (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.Block]): - A list of visually detected text blocks on - the page. A block has a set of lines (collected - into paragraphs) that have a common line-spacing - and orientation. - paragraphs (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.Paragraph]): - A list of visually detected text paragraphs - on the page. A collection of lines that a human - would perceive as a paragraph. - lines (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.Line]): - A list of visually detected text lines on the - page. A collection of tokens that a human would - perceive as a line. - tokens (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.Token]): - A list of visually detected tokens on the - page. - visual_elements (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.VisualElement]): - A list of detected non-text visual elements - e.g. checkbox, signature etc. on the page. - tables (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.Table]): - A list of visually detected tables on the - page. - form_fields (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.FormField]): - A list of visually detected form fields on - the page. - symbols (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.Symbol]): - A list of visually detected symbols on the - page. - detected_barcodes (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.DetectedBarcode]): - A list of detected barcodes. - image_quality_scores (google.cloud.documentai_v1beta2.types.Document.Page.ImageQualityScores): - Image quality scores. - provenance (google.cloud.documentai_v1beta2.types.Document.Provenance): - The history of this page. - """ - - class Dimension(proto.Message): - r"""Dimension for the page. - - Attributes: - width (float): - Page width. - height (float): - Page height. - unit (str): - Dimension unit. - """ - - width: float = proto.Field( - proto.FLOAT, - number=1, - ) - height: float = proto.Field( - proto.FLOAT, - number=2, - ) - unit: str = proto.Field( - proto.STRING, - number=3, - ) - - class Image(proto.Message): - r"""Rendered image contents for this page. - - Attributes: - content (bytes): - Raw byte content of the image. - mime_type (str): - Encoding `media type (MIME - type) `__ - for the image. - width (int): - Width of the image in pixels. - height (int): - Height of the image in pixels. - """ - - content: bytes = proto.Field( - proto.BYTES, - number=1, - ) - mime_type: str = proto.Field( - proto.STRING, - number=2, - ) - width: int = proto.Field( - proto.INT32, - number=3, - ) - height: int = proto.Field( - proto.INT32, - number=4, - ) - - class Matrix(proto.Message): - r"""Representation for transformation matrix, intended to be - compatible and used with OpenCV format for image manipulation. - - Attributes: - rows (int): - Number of rows in the matrix. - cols (int): - Number of columns in the matrix. - type_ (int): - This encodes information about what data type the matrix - uses. For example, 0 (CV_8U) is an unsigned 8-bit image. For - the full list of OpenCV primitive data types, please refer - to - https://docs.opencv.org/4.3.0/d1/d1b/group__core__hal__interface.html - data (bytes): - The matrix data. - """ - - rows: int = proto.Field( - proto.INT32, - number=1, - ) - cols: int = proto.Field( - proto.INT32, - number=2, - ) - type_: int = proto.Field( - proto.INT32, - number=3, - ) - data: bytes = proto.Field( - proto.BYTES, - number=4, - ) - - class Layout(proto.Message): - r"""Visual element describing a layout unit on a page. - - Attributes: - text_anchor (google.cloud.documentai_v1beta2.types.Document.TextAnchor): - Text anchor indexing into the - [Document.text][google.cloud.documentai.v1beta2.Document.text]. - confidence (float): - Confidence of the current - [Layout][google.cloud.documentai.v1beta2.Document.Page.Layout] - within context of the object this layout is for. e.g. - confidence can be for a single token, a table, a visual - element, etc. depending on context. Range ``[0, 1]``. - bounding_poly (google.cloud.documentai_v1beta2.types.BoundingPoly): - The bounding polygon for the - [Layout][google.cloud.documentai.v1beta2.Document.Page.Layout]. - orientation (google.cloud.documentai_v1beta2.types.Document.Page.Layout.Orientation): - Detected orientation for the - [Layout][google.cloud.documentai.v1beta2.Document.Page.Layout]. - """ - - class Orientation(proto.Enum): - r"""Detected human reading orientation. - - Values: - ORIENTATION_UNSPECIFIED (0): - Unspecified orientation. - PAGE_UP (1): - Orientation is aligned with page up. - PAGE_RIGHT (2): - Orientation is aligned with page right. - Turn the head 90 degrees clockwise from upright - to read. - PAGE_DOWN (3): - Orientation is aligned with page down. - Turn the head 180 degrees from upright to read. - PAGE_LEFT (4): - Orientation is aligned with page left. - Turn the head 90 degrees counterclockwise from - upright to read. - """ - ORIENTATION_UNSPECIFIED = 0 - PAGE_UP = 1 - PAGE_RIGHT = 2 - PAGE_DOWN = 3 - PAGE_LEFT = 4 - - text_anchor: "Document.TextAnchor" = proto.Field( - proto.MESSAGE, - number=1, - message="Document.TextAnchor", - ) - confidence: float = proto.Field( - proto.FLOAT, - number=2, - ) - bounding_poly: geometry.BoundingPoly = proto.Field( - proto.MESSAGE, - number=3, - message=geometry.BoundingPoly, - ) - orientation: "Document.Page.Layout.Orientation" = proto.Field( - proto.ENUM, - number=4, - enum="Document.Page.Layout.Orientation", - ) - - class Block(proto.Message): - r"""A block has a set of lines (collected into paragraphs) that - have a common line-spacing and orientation. - - Attributes: - layout (google.cloud.documentai_v1beta2.types.Document.Page.Layout): - [Layout][google.cloud.documentai.v1beta2.Document.Page.Layout] - for - [Block][google.cloud.documentai.v1beta2.Document.Page.Block]. - detected_languages (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.DetectedLanguage]): - A list of detected languages together with - confidence. - provenance (google.cloud.documentai_v1beta2.types.Document.Provenance): - The history of this annotation. - """ - - layout: "Document.Page.Layout" = proto.Field( - proto.MESSAGE, - number=1, - message="Document.Page.Layout", - ) - detected_languages: MutableSequence[ - "Document.Page.DetectedLanguage" - ] = proto.RepeatedField( - proto.MESSAGE, - number=2, - message="Document.Page.DetectedLanguage", - ) - provenance: "Document.Provenance" = proto.Field( - proto.MESSAGE, - number=3, - message="Document.Provenance", - ) - - class Paragraph(proto.Message): - r"""A collection of lines that a human would perceive as a - paragraph. - - Attributes: - layout (google.cloud.documentai_v1beta2.types.Document.Page.Layout): - [Layout][google.cloud.documentai.v1beta2.Document.Page.Layout] - for - [Paragraph][google.cloud.documentai.v1beta2.Document.Page.Paragraph]. - detected_languages (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.DetectedLanguage]): - A list of detected languages together with - confidence. - provenance (google.cloud.documentai_v1beta2.types.Document.Provenance): - The history of this annotation. - """ - - layout: "Document.Page.Layout" = proto.Field( - proto.MESSAGE, - number=1, - message="Document.Page.Layout", - ) - detected_languages: MutableSequence[ - "Document.Page.DetectedLanguage" - ] = proto.RepeatedField( - proto.MESSAGE, - number=2, - message="Document.Page.DetectedLanguage", - ) - provenance: "Document.Provenance" = proto.Field( - proto.MESSAGE, - number=3, - message="Document.Provenance", - ) - - class Line(proto.Message): - r"""A collection of tokens that a human would perceive as a line. - Does not cross column boundaries, can be horizontal, vertical, - etc. - - Attributes: - layout (google.cloud.documentai_v1beta2.types.Document.Page.Layout): - [Layout][google.cloud.documentai.v1beta2.Document.Page.Layout] - for - [Line][google.cloud.documentai.v1beta2.Document.Page.Line]. - detected_languages (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.DetectedLanguage]): - A list of detected languages together with - confidence. - provenance (google.cloud.documentai_v1beta2.types.Document.Provenance): - The history of this annotation. - """ - - layout: "Document.Page.Layout" = proto.Field( - proto.MESSAGE, - number=1, - message="Document.Page.Layout", - ) - detected_languages: MutableSequence[ - "Document.Page.DetectedLanguage" - ] = proto.RepeatedField( - proto.MESSAGE, - number=2, - message="Document.Page.DetectedLanguage", - ) - provenance: "Document.Provenance" = proto.Field( - proto.MESSAGE, - number=3, - message="Document.Provenance", - ) - - class Token(proto.Message): - r"""A detected token. - - Attributes: - layout (google.cloud.documentai_v1beta2.types.Document.Page.Layout): - [Layout][google.cloud.documentai.v1beta2.Document.Page.Layout] - for - [Token][google.cloud.documentai.v1beta2.Document.Page.Token]. - detected_break (google.cloud.documentai_v1beta2.types.Document.Page.Token.DetectedBreak): - Detected break at the end of a - [Token][google.cloud.documentai.v1beta2.Document.Page.Token]. - detected_languages (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.DetectedLanguage]): - A list of detected languages together with - confidence. - provenance (google.cloud.documentai_v1beta2.types.Document.Provenance): - The history of this annotation. - style_info (google.cloud.documentai_v1beta2.types.Document.Page.Token.StyleInfo): - Text style attributes. - """ - - class DetectedBreak(proto.Message): - r"""Detected break at the end of a - [Token][google.cloud.documentai.v1beta2.Document.Page.Token]. - - Attributes: - type_ (google.cloud.documentai_v1beta2.types.Document.Page.Token.DetectedBreak.Type): - Detected break type. - """ - - class Type(proto.Enum): - r"""Enum to denote the type of break found. - - Values: - TYPE_UNSPECIFIED (0): - Unspecified break type. - SPACE (1): - A single whitespace. - WIDE_SPACE (2): - A wider whitespace. - HYPHEN (3): - A hyphen that indicates that a token has been - split across lines. - """ - TYPE_UNSPECIFIED = 0 - SPACE = 1 - WIDE_SPACE = 2 - HYPHEN = 3 - - type_: "Document.Page.Token.DetectedBreak.Type" = proto.Field( - proto.ENUM, - number=1, - enum="Document.Page.Token.DetectedBreak.Type", - ) - - class StyleInfo(proto.Message): - r"""Font and other text style attributes. - - Attributes: - font_size (int): - Font size in points (``1`` point is ``¹⁄₇₂`` inches). - pixel_font_size (float): - Font size in pixels, equal to *unrounded - [font_size][google.cloud.documentai.v1beta2.Document.Page.Token.StyleInfo.font_size]* - - - *resolution* ÷ ``72.0``. - letter_spacing (float): - Letter spacing in points. - font_type (str): - Name or style of the font. - bold (bool): - Whether the text is bold (equivalent to - [font_weight][google.cloud.documentai.v1beta2.Document.Page.Token.StyleInfo.font_weight] - is at least ``700``). - italic (bool): - Whether the text is italic. - underlined (bool): - Whether the text is underlined. - strikeout (bool): - Whether the text is strikethrough. - subscript (bool): - Whether the text is a subscript. - superscript (bool): - Whether the text is a superscript. - smallcaps (bool): - Whether the text is in small caps. - font_weight (int): - TrueType weight on a scale ``100`` (thin) to ``1000`` - (ultra-heavy). Normal is ``400``, bold is ``700``. - handwritten (bool): - Whether the text is handwritten. - text_color (google.type.color_pb2.Color): - Color of the text. - background_color (google.type.color_pb2.Color): - Color of the background. - """ - - font_size: int = proto.Field( - proto.INT32, - number=1, - ) - pixel_font_size: float = proto.Field( - proto.DOUBLE, - number=2, - ) - letter_spacing: float = proto.Field( - proto.DOUBLE, - number=3, - ) - font_type: str = proto.Field( - proto.STRING, - number=4, - ) - bold: bool = proto.Field( - proto.BOOL, - number=5, - ) - italic: bool = proto.Field( - proto.BOOL, - number=6, - ) - underlined: bool = proto.Field( - proto.BOOL, - number=7, - ) - strikeout: bool = proto.Field( - proto.BOOL, - number=8, - ) - subscript: bool = proto.Field( - proto.BOOL, - number=9, - ) - superscript: bool = proto.Field( - proto.BOOL, - number=10, - ) - smallcaps: bool = proto.Field( - proto.BOOL, - number=11, - ) - font_weight: int = proto.Field( - proto.INT32, - number=12, - ) - handwritten: bool = proto.Field( - proto.BOOL, - number=13, - ) - text_color: color_pb2.Color = proto.Field( - proto.MESSAGE, - number=14, - message=color_pb2.Color, - ) - background_color: color_pb2.Color = proto.Field( - proto.MESSAGE, - number=15, - message=color_pb2.Color, - ) - - layout: "Document.Page.Layout" = proto.Field( - proto.MESSAGE, - number=1, - message="Document.Page.Layout", - ) - detected_break: "Document.Page.Token.DetectedBreak" = proto.Field( - proto.MESSAGE, - number=2, - message="Document.Page.Token.DetectedBreak", - ) - detected_languages: MutableSequence[ - "Document.Page.DetectedLanguage" - ] = proto.RepeatedField( - proto.MESSAGE, - number=3, - message="Document.Page.DetectedLanguage", - ) - provenance: "Document.Provenance" = proto.Field( - proto.MESSAGE, - number=4, - message="Document.Provenance", - ) - style_info: "Document.Page.Token.StyleInfo" = proto.Field( - proto.MESSAGE, - number=5, - message="Document.Page.Token.StyleInfo", - ) - - class Symbol(proto.Message): - r"""A detected symbol. - - Attributes: - layout (google.cloud.documentai_v1beta2.types.Document.Page.Layout): - [Layout][google.cloud.documentai.v1beta2.Document.Page.Layout] - for - [Symbol][google.cloud.documentai.v1beta2.Document.Page.Symbol]. - detected_languages (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.DetectedLanguage]): - A list of detected languages together with - confidence. - """ - - layout: "Document.Page.Layout" = proto.Field( - proto.MESSAGE, - number=1, - message="Document.Page.Layout", - ) - detected_languages: MutableSequence[ - "Document.Page.DetectedLanguage" - ] = proto.RepeatedField( - proto.MESSAGE, - number=2, - message="Document.Page.DetectedLanguage", - ) - - class VisualElement(proto.Message): - r"""Detected non-text visual elements e.g. checkbox, signature - etc. on the page. - - Attributes: - layout (google.cloud.documentai_v1beta2.types.Document.Page.Layout): - [Layout][google.cloud.documentai.v1beta2.Document.Page.Layout] - for - [VisualElement][google.cloud.documentai.v1beta2.Document.Page.VisualElement]. - type_ (str): - Type of the - [VisualElement][google.cloud.documentai.v1beta2.Document.Page.VisualElement]. - detected_languages (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.DetectedLanguage]): - A list of detected languages together with - confidence. - """ - - layout: "Document.Page.Layout" = proto.Field( - proto.MESSAGE, - number=1, - message="Document.Page.Layout", - ) - type_: str = proto.Field( - proto.STRING, - number=2, - ) - detected_languages: MutableSequence[ - "Document.Page.DetectedLanguage" - ] = proto.RepeatedField( - proto.MESSAGE, - number=3, - message="Document.Page.DetectedLanguage", - ) - - class Table(proto.Message): - r"""A table representation similar to HTML table structure. - - Attributes: - layout (google.cloud.documentai_v1beta2.types.Document.Page.Layout): - [Layout][google.cloud.documentai.v1beta2.Document.Page.Layout] - for - [Table][google.cloud.documentai.v1beta2.Document.Page.Table]. - header_rows (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.Table.TableRow]): - Header rows of the table. - body_rows (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.Table.TableRow]): - Body rows of the table. - detected_languages (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.DetectedLanguage]): - A list of detected languages together with - confidence. - provenance (google.cloud.documentai_v1beta2.types.Document.Provenance): - The history of this table. - """ - - class TableRow(proto.Message): - r"""A row of table cells. - - Attributes: - cells (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.Table.TableCell]): - Cells that make up this row. - """ - - cells: MutableSequence[ - "Document.Page.Table.TableCell" - ] = proto.RepeatedField( - proto.MESSAGE, - number=1, - message="Document.Page.Table.TableCell", - ) - - class TableCell(proto.Message): - r"""A cell representation inside the table. - - Attributes: - layout (google.cloud.documentai_v1beta2.types.Document.Page.Layout): - [Layout][google.cloud.documentai.v1beta2.Document.Page.Layout] - for - [TableCell][google.cloud.documentai.v1beta2.Document.Page.Table.TableCell]. - row_span (int): - How many rows this cell spans. - col_span (int): - How many columns this cell spans. - detected_languages (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.DetectedLanguage]): - A list of detected languages together with - confidence. - """ - - layout: "Document.Page.Layout" = proto.Field( - proto.MESSAGE, - number=1, - message="Document.Page.Layout", - ) - row_span: int = proto.Field( - proto.INT32, - number=2, - ) - col_span: int = proto.Field( - proto.INT32, - number=3, - ) - detected_languages: MutableSequence[ - "Document.Page.DetectedLanguage" - ] = proto.RepeatedField( - proto.MESSAGE, - number=4, - message="Document.Page.DetectedLanguage", - ) - - layout: "Document.Page.Layout" = proto.Field( - proto.MESSAGE, - number=1, - message="Document.Page.Layout", - ) - header_rows: MutableSequence[ - "Document.Page.Table.TableRow" - ] = proto.RepeatedField( - proto.MESSAGE, - number=2, - message="Document.Page.Table.TableRow", - ) - body_rows: MutableSequence[ - "Document.Page.Table.TableRow" - ] = proto.RepeatedField( - proto.MESSAGE, - number=3, - message="Document.Page.Table.TableRow", - ) - detected_languages: MutableSequence[ - "Document.Page.DetectedLanguage" - ] = proto.RepeatedField( - proto.MESSAGE, - number=4, - message="Document.Page.DetectedLanguage", - ) - provenance: "Document.Provenance" = proto.Field( - proto.MESSAGE, - number=5, - message="Document.Provenance", - ) - - class FormField(proto.Message): - r"""A form field detected on the page. - - Attributes: - field_name (google.cloud.documentai_v1beta2.types.Document.Page.Layout): - [Layout][google.cloud.documentai.v1beta2.Document.Page.Layout] - for the - [FormField][google.cloud.documentai.v1beta2.Document.Page.FormField] - name. e.g. ``Address``, ``Email``, ``Grand total``, - ``Phone number``, etc. - field_value (google.cloud.documentai_v1beta2.types.Document.Page.Layout): - [Layout][google.cloud.documentai.v1beta2.Document.Page.Layout] - for the - [FormField][google.cloud.documentai.v1beta2.Document.Page.FormField] - value. - name_detected_languages (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.DetectedLanguage]): - A list of detected languages for name - together with confidence. - value_detected_languages (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.DetectedLanguage]): - A list of detected languages for value - together with confidence. - value_type (str): - If the value is non-textual, this field represents the type. - Current valid values are: - - - blank (this indicates the ``field_value`` is normal text) - - ``unfilled_checkbox`` - - ``filled_checkbox`` - corrected_key_text (str): - Created for Labeling UI to export key text. If corrections - were made to the text identified by the - ``field_name.text_anchor``, this field will contain the - correction. - corrected_value_text (str): - Created for Labeling UI to export value text. If corrections - were made to the text identified by the - ``field_value.text_anchor``, this field will contain the - correction. - provenance (google.cloud.documentai_v1beta2.types.Document.Provenance): - The history of this annotation. - """ - - field_name: "Document.Page.Layout" = proto.Field( - proto.MESSAGE, - number=1, - message="Document.Page.Layout", - ) - field_value: "Document.Page.Layout" = proto.Field( - proto.MESSAGE, - number=2, - message="Document.Page.Layout", - ) - name_detected_languages: MutableSequence[ - "Document.Page.DetectedLanguage" - ] = proto.RepeatedField( - proto.MESSAGE, - number=3, - message="Document.Page.DetectedLanguage", - ) - value_detected_languages: MutableSequence[ - "Document.Page.DetectedLanguage" - ] = proto.RepeatedField( - proto.MESSAGE, - number=4, - message="Document.Page.DetectedLanguage", - ) - value_type: str = proto.Field( - proto.STRING, - number=5, - ) - corrected_key_text: str = proto.Field( - proto.STRING, - number=6, - ) - corrected_value_text: str = proto.Field( - proto.STRING, - number=7, - ) - provenance: "Document.Provenance" = proto.Field( - proto.MESSAGE, - number=8, - message="Document.Provenance", - ) - - class DetectedBarcode(proto.Message): - r"""A detected barcode. - - Attributes: - layout (google.cloud.documentai_v1beta2.types.Document.Page.Layout): - [Layout][google.cloud.documentai.v1beta2.Document.Page.Layout] - for - [DetectedBarcode][google.cloud.documentai.v1beta2.Document.Page.DetectedBarcode]. - barcode (google.cloud.documentai_v1beta2.types.Barcode): - Detailed barcode information of the - [DetectedBarcode][google.cloud.documentai.v1beta2.Document.Page.DetectedBarcode]. - """ - - layout: "Document.Page.Layout" = proto.Field( - proto.MESSAGE, - number=1, - message="Document.Page.Layout", - ) - barcode: gcd_barcode.Barcode = proto.Field( - proto.MESSAGE, - number=2, - message=gcd_barcode.Barcode, - ) - - class DetectedLanguage(proto.Message): - r"""Detected language for a structural component. - - Attributes: - language_code (str): - The `BCP-47 language - code `__, - such as ``en-US`` or ``sr-Latn``. - confidence (float): - Confidence of detected language. Range ``[0, 1]``. - """ - - language_code: str = proto.Field( - proto.STRING, - number=1, - ) - confidence: float = proto.Field( - proto.FLOAT, - number=2, - ) - - class ImageQualityScores(proto.Message): - r"""Image quality scores for the page image. - - Attributes: - quality_score (float): - The overall quality score. Range ``[0, 1]`` where ``1`` is - perfect quality. - detected_defects (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Page.ImageQualityScores.DetectedDefect]): - A list of detected defects. - """ - - class DetectedDefect(proto.Message): - r"""Image Quality Defects - - Attributes: - type_ (str): - Name of the defect type. Supported values are: - - - ``quality/defect_blurry`` - - ``quality/defect_noisy`` - - ``quality/defect_dark`` - - ``quality/defect_faint`` - - ``quality/defect_text_too_small`` - - ``quality/defect_document_cutoff`` - - ``quality/defect_text_cutoff`` - - ``quality/defect_glare`` - confidence (float): - Confidence of detected defect. Range ``[0, 1]`` where ``1`` - indicates strong confidence that the defect exists. - """ - - type_: str = proto.Field( - proto.STRING, - number=1, - ) - confidence: float = proto.Field( - proto.FLOAT, - number=2, - ) - - quality_score: float = proto.Field( - proto.FLOAT, - number=1, - ) - detected_defects: MutableSequence[ - "Document.Page.ImageQualityScores.DetectedDefect" - ] = proto.RepeatedField( - proto.MESSAGE, - number=2, - message="Document.Page.ImageQualityScores.DetectedDefect", - ) - - page_number: int = proto.Field( - proto.INT32, - number=1, - ) - image: "Document.Page.Image" = proto.Field( - proto.MESSAGE, - number=13, - message="Document.Page.Image", - ) - transforms: MutableSequence["Document.Page.Matrix"] = proto.RepeatedField( - proto.MESSAGE, - number=14, - message="Document.Page.Matrix", - ) - dimension: "Document.Page.Dimension" = proto.Field( - proto.MESSAGE, - number=2, - message="Document.Page.Dimension", - ) - layout: "Document.Page.Layout" = proto.Field( - proto.MESSAGE, - number=3, - message="Document.Page.Layout", - ) - detected_languages: MutableSequence[ - "Document.Page.DetectedLanguage" - ] = proto.RepeatedField( - proto.MESSAGE, - number=4, - message="Document.Page.DetectedLanguage", - ) - blocks: MutableSequence["Document.Page.Block"] = proto.RepeatedField( - proto.MESSAGE, - number=5, - message="Document.Page.Block", - ) - paragraphs: MutableSequence["Document.Page.Paragraph"] = proto.RepeatedField( - proto.MESSAGE, - number=6, - message="Document.Page.Paragraph", - ) - lines: MutableSequence["Document.Page.Line"] = proto.RepeatedField( - proto.MESSAGE, - number=7, - message="Document.Page.Line", - ) - tokens: MutableSequence["Document.Page.Token"] = proto.RepeatedField( - proto.MESSAGE, - number=8, - message="Document.Page.Token", - ) - visual_elements: MutableSequence[ - "Document.Page.VisualElement" - ] = proto.RepeatedField( - proto.MESSAGE, - number=9, - message="Document.Page.VisualElement", - ) - tables: MutableSequence["Document.Page.Table"] = proto.RepeatedField( - proto.MESSAGE, - number=10, - message="Document.Page.Table", - ) - form_fields: MutableSequence["Document.Page.FormField"] = proto.RepeatedField( - proto.MESSAGE, - number=11, - message="Document.Page.FormField", - ) - symbols: MutableSequence["Document.Page.Symbol"] = proto.RepeatedField( - proto.MESSAGE, - number=12, - message="Document.Page.Symbol", - ) - detected_barcodes: MutableSequence[ - "Document.Page.DetectedBarcode" - ] = proto.RepeatedField( - proto.MESSAGE, - number=15, - message="Document.Page.DetectedBarcode", - ) - image_quality_scores: "Document.Page.ImageQualityScores" = proto.Field( - proto.MESSAGE, - number=17, - message="Document.Page.ImageQualityScores", - ) - provenance: "Document.Provenance" = proto.Field( - proto.MESSAGE, - number=16, - message="Document.Provenance", - ) - - class Entity(proto.Message): - r"""An entity that could be a phrase in the text or a property - that belongs to the document. It is a known entity type, such as - a person, an organization, or location. - - Attributes: - text_anchor (google.cloud.documentai_v1beta2.types.Document.TextAnchor): - Optional. Provenance of the entity. Text anchor indexing - into the - [Document.text][google.cloud.documentai.v1beta2.Document.text]. - type_ (str): - Required. Entity type from a schema e.g. ``Address``. - mention_text (str): - Optional. Text value of the entity e.g. - ``1600 Amphitheatre Pkwy``. - mention_id (str): - Optional. Deprecated. Use ``id`` field instead. - confidence (float): - Optional. Confidence of detected Schema entity. Range - ``[0, 1]``. - page_anchor (google.cloud.documentai_v1beta2.types.Document.PageAnchor): - Optional. Represents the provenance of this - entity wrt. the location on the page where it - was found. - id (str): - Optional. Canonical id. This will be a unique - value in the entity list for this document. - normalized_value (google.cloud.documentai_v1beta2.types.Document.Entity.NormalizedValue): - Optional. Normalized entity value. Absent if - the extracted value could not be converted or - the type (e.g. address) is not supported for - certain parsers. This field is also only - populated for certain supported document types. - properties (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Entity]): - Optional. Entities can be nested to form a - hierarchical data structure representing the - content in the document. - provenance (google.cloud.documentai_v1beta2.types.Document.Provenance): - Optional. The history of this annotation. - redacted (bool): - Optional. Whether the entity will be redacted - for de-identification purposes. - """ - - class NormalizedValue(proto.Message): - r"""Parsed and normalized entity value. - - This message has `oneof`_ fields (mutually exclusive fields). - For each oneof, at most one member field can be set at the same time. - Setting any member of the oneof automatically clears all other - members. - - .. _oneof: https://proto-plus-python.readthedocs.io/en/stable/fields.html#oneofs-mutually-exclusive-fields - - Attributes: - money_value (google.type.money_pb2.Money): - Money value. See also: - - https://github.com/googleapis/googleapis/blob/master/google/type/money.proto - - This field is a member of `oneof`_ ``structured_value``. - date_value (google.type.date_pb2.Date): - Date value. Includes year, month, day. See - also: - https://github.com/googleapis/googleapis/blob/master/google/type/date.proto - - This field is a member of `oneof`_ ``structured_value``. - datetime_value (google.type.datetime_pb2.DateTime): - DateTime value. Includes date, time, and - timezone. See also: - https://github.com/googleapis/googleapis/blob/master/google/type/datetime.proto - - This field is a member of `oneof`_ ``structured_value``. - address_value (google.type.postal_address_pb2.PostalAddress): - Postal address. See also: - https://github.com/googleapis/googleapis/blob/master/google/type/postal_address.proto - - This field is a member of `oneof`_ ``structured_value``. - boolean_value (bool): - Boolean value. Can be used for entities with - binary values, or for checkboxes. - - This field is a member of `oneof`_ ``structured_value``. - integer_value (int): - Integer value. - - This field is a member of `oneof`_ ``structured_value``. - float_value (float): - Float value. - - This field is a member of `oneof`_ ``structured_value``. - text (str): - Optional. An optional field to store a normalized string. - For some entity types, one of respective - ``structured_value`` fields may also be populated. Also not - all the types of ``structured_value`` will be normalized. - For example, some processors may not generate ``float`` or - ``integer`` normalized text by default. - - Below are sample formats mapped to structured values. - - - Money/Currency type (``money_value``) is in the ISO 4217 - text format. - - Date type (``date_value``) is in the ISO 8601 text - format. - - Datetime type (``datetime_value``) is in the ISO 8601 - text format. - """ - - money_value: money_pb2.Money = proto.Field( - proto.MESSAGE, - number=2, - oneof="structured_value", - message=money_pb2.Money, - ) - date_value: date_pb2.Date = proto.Field( - proto.MESSAGE, - number=3, - oneof="structured_value", - message=date_pb2.Date, - ) - datetime_value: datetime_pb2.DateTime = proto.Field( - proto.MESSAGE, - number=4, - oneof="structured_value", - message=datetime_pb2.DateTime, - ) - address_value: postal_address_pb2.PostalAddress = proto.Field( - proto.MESSAGE, - number=5, - oneof="structured_value", - message=postal_address_pb2.PostalAddress, - ) - boolean_value: bool = proto.Field( - proto.BOOL, - number=6, - oneof="structured_value", - ) - integer_value: int = proto.Field( - proto.INT32, - number=7, - oneof="structured_value", - ) - float_value: float = proto.Field( - proto.FLOAT, - number=8, - oneof="structured_value", - ) - text: str = proto.Field( - proto.STRING, - number=1, - ) - - text_anchor: "Document.TextAnchor" = proto.Field( - proto.MESSAGE, - number=1, - message="Document.TextAnchor", - ) - type_: str = proto.Field( - proto.STRING, - number=2, - ) - mention_text: str = proto.Field( - proto.STRING, - number=3, - ) - mention_id: str = proto.Field( - proto.STRING, - number=4, - ) - confidence: float = proto.Field( - proto.FLOAT, - number=5, - ) - page_anchor: "Document.PageAnchor" = proto.Field( - proto.MESSAGE, - number=6, - message="Document.PageAnchor", - ) - id: str = proto.Field( - proto.STRING, - number=7, - ) - normalized_value: "Document.Entity.NormalizedValue" = proto.Field( - proto.MESSAGE, - number=9, - message="Document.Entity.NormalizedValue", - ) - properties: MutableSequence["Document.Entity"] = proto.RepeatedField( - proto.MESSAGE, - number=10, - message="Document.Entity", - ) - provenance: "Document.Provenance" = proto.Field( - proto.MESSAGE, - number=11, - message="Document.Provenance", - ) - redacted: bool = proto.Field( - proto.BOOL, - number=12, - ) - - class EntityRelation(proto.Message): - r"""Relationship between - [Entities][google.cloud.documentai.v1beta2.Document.Entity]. - - Attributes: - subject_id (str): - Subject entity id. - object_id (str): - Object entity id. - relation (str): - Relationship description. - """ - - subject_id: str = proto.Field( - proto.STRING, - number=1, - ) - object_id: str = proto.Field( - proto.STRING, - number=2, - ) - relation: str = proto.Field( - proto.STRING, - number=3, - ) - - class TextAnchor(proto.Message): - r"""Text reference indexing into the - [Document.text][google.cloud.documentai.v1beta2.Document.text]. - - Attributes: - text_segments (MutableSequence[google.cloud.documentai_v1beta2.types.Document.TextAnchor.TextSegment]): - The text segments from the - [Document.text][google.cloud.documentai.v1beta2.Document.text]. - content (str): - Contains the content of the text span so that users do not - have to look it up in the text_segments. It is always - populated for formFields. - """ - - class TextSegment(proto.Message): - r"""A text segment in the - [Document.text][google.cloud.documentai.v1beta2.Document.text]. The - indices may be out of bounds which indicate that the text extends - into another document shard for large sharded documents. See - [ShardInfo.text_offset][google.cloud.documentai.v1beta2.Document.ShardInfo.text_offset] - - Attributes: - start_index (int): - [TextSegment][google.cloud.documentai.v1beta2.Document.TextAnchor.TextSegment] - start UTF-8 char index in the - [Document.text][google.cloud.documentai.v1beta2.Document.text]. - end_index (int): - [TextSegment][google.cloud.documentai.v1beta2.Document.TextAnchor.TextSegment] - half open end UTF-8 char index in the - [Document.text][google.cloud.documentai.v1beta2.Document.text]. - """ - - start_index: int = proto.Field( - proto.INT64, - number=1, - ) - end_index: int = proto.Field( - proto.INT64, - number=2, - ) - - text_segments: MutableSequence[ - "Document.TextAnchor.TextSegment" - ] = proto.RepeatedField( - proto.MESSAGE, - number=1, - message="Document.TextAnchor.TextSegment", - ) - content: str = proto.Field( - proto.STRING, - number=2, - ) - - class PageAnchor(proto.Message): - r"""Referencing the visual context of the entity in the - [Document.pages][google.cloud.documentai.v1beta2.Document.pages]. - Page anchors can be cross-page, consist of multiple bounding - polygons and optionally reference specific layout element types. - - Attributes: - page_refs (MutableSequence[google.cloud.documentai_v1beta2.types.Document.PageAnchor.PageRef]): - One or more references to visual page - elements - """ - - class PageRef(proto.Message): - r"""Represents a weak reference to a page element within a - document. - - Attributes: - page (int): - Required. Index into the - [Document.pages][google.cloud.documentai.v1beta2.Document.pages] - element, for example using - ``[Document.pages][page_refs.page]`` to locate the related - page element. This field is skipped when its value is the - default ``0``. See - https://developers.google.com/protocol-buffers/docs/proto3#json. - layout_type (google.cloud.documentai_v1beta2.types.Document.PageAnchor.PageRef.LayoutType): - Optional. The type of the layout element that - is being referenced if any. - layout_id (str): - Optional. Deprecated. Use - [PageRef.bounding_poly][google.cloud.documentai.v1beta2.Document.PageAnchor.PageRef.bounding_poly] - instead. - bounding_poly (google.cloud.documentai_v1beta2.types.BoundingPoly): - Optional. Identifies the bounding polygon of a layout - element on the page. If ``layout_type`` is set, the bounding - polygon must be exactly the same to the layout element it's - referring to. - confidence (float): - Optional. Confidence of detected page element, if - applicable. Range ``[0, 1]``. - """ - - class LayoutType(proto.Enum): - r"""The type of layout that is being referenced. - - Values: - LAYOUT_TYPE_UNSPECIFIED (0): - Layout Unspecified. - BLOCK (1): - References a - [Page.blocks][google.cloud.documentai.v1beta2.Document.Page.blocks] - element. - PARAGRAPH (2): - References a - [Page.paragraphs][google.cloud.documentai.v1beta2.Document.Page.paragraphs] - element. - LINE (3): - References a - [Page.lines][google.cloud.documentai.v1beta2.Document.Page.lines] - element. - TOKEN (4): - References a - [Page.tokens][google.cloud.documentai.v1beta2.Document.Page.tokens] - element. - VISUAL_ELEMENT (5): - References a - [Page.visual_elements][google.cloud.documentai.v1beta2.Document.Page.visual_elements] - element. - TABLE (6): - Refrrences a - [Page.tables][google.cloud.documentai.v1beta2.Document.Page.tables] - element. - FORM_FIELD (7): - References a - [Page.form_fields][google.cloud.documentai.v1beta2.Document.Page.form_fields] - element. - """ - LAYOUT_TYPE_UNSPECIFIED = 0 - BLOCK = 1 - PARAGRAPH = 2 - LINE = 3 - TOKEN = 4 - VISUAL_ELEMENT = 5 - TABLE = 6 - FORM_FIELD = 7 - - page: int = proto.Field( - proto.INT64, - number=1, - ) - layout_type: "Document.PageAnchor.PageRef.LayoutType" = proto.Field( - proto.ENUM, - number=2, - enum="Document.PageAnchor.PageRef.LayoutType", - ) - layout_id: str = proto.Field( - proto.STRING, - number=3, - ) - bounding_poly: geometry.BoundingPoly = proto.Field( - proto.MESSAGE, - number=4, - message=geometry.BoundingPoly, - ) - confidence: float = proto.Field( - proto.FLOAT, - number=5, - ) - - page_refs: MutableSequence["Document.PageAnchor.PageRef"] = proto.RepeatedField( - proto.MESSAGE, - number=1, - message="Document.PageAnchor.PageRef", - ) - - class Provenance(proto.Message): - r"""Structure to identify provenance relationships between - annotations in different revisions. - - Attributes: - revision (int): - The index of the revision that produced this - element. - id (int): - The Id of this operation. Needs to be unique - within the scope of the revision. - parents (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Provenance.Parent]): - References to the original elements that are - replaced. - type_ (google.cloud.documentai_v1beta2.types.Document.Provenance.OperationType): - The type of provenance operation. - """ - - class OperationType(proto.Enum): - r"""If a processor or agent does an explicit operation on - existing elements. - - Values: - OPERATION_TYPE_UNSPECIFIED (0): - Operation type unspecified. If no operation is specified a - provenance entry is simply used to match against a - ``parent``. - ADD (1): - Add an element. - REMOVE (2): - Remove an element identified by ``parent``. - UPDATE (7): - Updates any fields within the given - provenance scope of the message. It overwrites - the fields rather than replacing them. Use this - when you want to update a field value of an - entity without also updating all the child - properties. - REPLACE (3): - Currently unused. Replace an element identified by - ``parent``. - EVAL_REQUESTED (4): - Deprecated. Request human review for the element identified - by ``parent``. - EVAL_APPROVED (5): - Deprecated. Element is reviewed and approved - at human review, confidence will be set to 1.0. - EVAL_SKIPPED (6): - Deprecated. Element is skipped in the - validation process. - """ - OPERATION_TYPE_UNSPECIFIED = 0 - ADD = 1 - REMOVE = 2 - UPDATE = 7 - REPLACE = 3 - EVAL_REQUESTED = 4 - EVAL_APPROVED = 5 - EVAL_SKIPPED = 6 - - class Parent(proto.Message): - r"""The parent element the current element is based on. Used for - referencing/aligning, removal and replacement operations. - - Attributes: - revision (int): - The index of the index into current revision's parent_ids - list. - index (int): - The index of the parent item in the - corresponding item list (eg. list of entities, - properties within entities, etc.) in the parent - revision. - id (int): - The id of the parent provenance. - """ - - revision: int = proto.Field( - proto.INT32, - number=1, - ) - index: int = proto.Field( - proto.INT32, - number=3, - ) - id: int = proto.Field( - proto.INT32, - number=2, - ) - - revision: int = proto.Field( - proto.INT32, - number=1, - ) - id: int = proto.Field( - proto.INT32, - number=2, - ) - parents: MutableSequence["Document.Provenance.Parent"] = proto.RepeatedField( - proto.MESSAGE, - number=3, - message="Document.Provenance.Parent", - ) - type_: "Document.Provenance.OperationType" = proto.Field( - proto.ENUM, - number=4, - enum="Document.Provenance.OperationType", - ) - - class Revision(proto.Message): - r"""Contains past or forward revisions of this document. - - This message has `oneof`_ fields (mutually exclusive fields). - For each oneof, at most one member field can be set at the same time. - Setting any member of the oneof automatically clears all other - members. - - .. _oneof: https://proto-plus-python.readthedocs.io/en/stable/fields.html#oneofs-mutually-exclusive-fields - - Attributes: - agent (str): - If the change was made by a person specify - the name or id of that person. - - This field is a member of `oneof`_ ``source``. - processor (str): - If the annotation was made by processor - identify the processor by its resource name. - - This field is a member of `oneof`_ ``source``. - id (str): - Id of the revision, internally generated by - doc proto storage. Unique within the context of - the document. - parent (MutableSequence[int]): - The revisions that this revision is based on. This can - include one or more parent (when documents are merged.) This - field represents the index into the ``revisions`` field. - parent_ids (MutableSequence[str]): - The revisions that this revision is based on. Must include - all the ids that have anything to do with this revision - - eg. there are ``provenance.parent.revision`` fields that - index into this field. - create_time (google.protobuf.timestamp_pb2.Timestamp): - The time that the revision was created, - internally generated by doc proto storage at the - time of create. - human_review (google.cloud.documentai_v1beta2.types.Document.Revision.HumanReview): - Human Review information of this revision. - """ - - class HumanReview(proto.Message): - r"""Human Review information of the document. - - Attributes: - state (str): - Human review state. e.g. ``requested``, ``succeeded``, - ``rejected``. - state_message (str): - A message providing more details about the current state of - processing. For example, the rejection reason when the state - is ``rejected``. - """ - - state: str = proto.Field( - proto.STRING, - number=1, - ) - state_message: str = proto.Field( - proto.STRING, - number=2, - ) - - agent: str = proto.Field( - proto.STRING, - number=4, - oneof="source", - ) - processor: str = proto.Field( - proto.STRING, - number=5, - oneof="source", - ) - id: str = proto.Field( - proto.STRING, - number=1, - ) - parent: MutableSequence[int] = proto.RepeatedField( - proto.INT32, - number=2, - ) - parent_ids: MutableSequence[str] = proto.RepeatedField( - proto.STRING, - number=7, - ) - create_time: timestamp_pb2.Timestamp = proto.Field( - proto.MESSAGE, - number=3, - message=timestamp_pb2.Timestamp, - ) - human_review: "Document.Revision.HumanReview" = proto.Field( - proto.MESSAGE, - number=6, - message="Document.Revision.HumanReview", - ) - - class TextChange(proto.Message): - r"""This message is used for text changes aka. OCR corrections. - - Attributes: - text_anchor (google.cloud.documentai_v1beta2.types.Document.TextAnchor): - Provenance of the correction. Text anchor indexing into the - [Document.text][google.cloud.documentai.v1beta2.Document.text]. - There can only be a single ``TextAnchor.text_segments`` - element. If the start and end index of the text segment are - the same, the text change is inserted before that index. - changed_text (str): - The text that replaces the text identified in the - ``text_anchor``. - provenance (MutableSequence[google.cloud.documentai_v1beta2.types.Document.Provenance]): - The history of this annotation. - """ - - text_anchor: "Document.TextAnchor" = proto.Field( - proto.MESSAGE, - number=1, - message="Document.TextAnchor", - ) - changed_text: str = proto.Field( - proto.STRING, - number=2, - ) - provenance: MutableSequence["Document.Provenance"] = proto.RepeatedField( - proto.MESSAGE, - number=3, - message="Document.Provenance", - ) - - uri: str = proto.Field( - proto.STRING, - number=1, - oneof="source", - ) - content: bytes = proto.Field( - proto.BYTES, - number=2, - oneof="source", - ) - mime_type: str = proto.Field( - proto.STRING, - number=3, - ) - text: str = proto.Field( - proto.STRING, - number=4, - ) - text_styles: MutableSequence[Style] = proto.RepeatedField( - proto.MESSAGE, - number=5, - message=Style, - ) - pages: MutableSequence[Page] = proto.RepeatedField( - proto.MESSAGE, - number=6, - message=Page, - ) - entities: MutableSequence[Entity] = proto.RepeatedField( - proto.MESSAGE, - number=7, - message=Entity, - ) - entity_relations: MutableSequence[EntityRelation] = proto.RepeatedField( - proto.MESSAGE, - number=8, - message=EntityRelation, - ) - text_changes: MutableSequence[TextChange] = proto.RepeatedField( - proto.MESSAGE, - number=14, - message=TextChange, - ) - shard_info: ShardInfo = proto.Field( - proto.MESSAGE, - number=9, - message=ShardInfo, - ) - labels: MutableSequence[Label] = proto.RepeatedField( - proto.MESSAGE, - number=11, - message=Label, - ) - error: status_pb2.Status = proto.Field( - proto.MESSAGE, - number=10, - message=status_pb2.Status, - ) - revisions: MutableSequence[Revision] = proto.RepeatedField( - proto.MESSAGE, - number=13, - message=Revision, - ) - - -__all__ = tuple(sorted(__protobuf__.manifest)) diff --git a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/types/document_understanding.py b/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/types/document_understanding.py deleted file mode 100644 index 906848e939f7..000000000000 --- a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/types/document_understanding.py +++ /dev/null @@ -1,578 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2024 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -from __future__ import annotations - -from typing import MutableMapping, MutableSequence - -from google.protobuf import timestamp_pb2 # type: ignore -import proto # type: ignore - -from google.cloud.documentai_v1beta2.types import geometry - -__protobuf__ = proto.module( - package="google.cloud.documentai.v1beta2", - manifest={ - "BatchProcessDocumentsRequest", - "ProcessDocumentRequest", - "BatchProcessDocumentsResponse", - "ProcessDocumentResponse", - "OcrParams", - "TableExtractionParams", - "TableBoundHint", - "FormExtractionParams", - "KeyValuePairHint", - "EntityExtractionParams", - "AutoMlParams", - "InputConfig", - "OutputConfig", - "GcsSource", - "GcsDestination", - "OperationMetadata", - }, -) - - -class BatchProcessDocumentsRequest(proto.Message): - r"""Request to batch process documents as an asynchronous operation. The - output is written to Cloud Storage as JSON in the [Document] format. - - Attributes: - requests (MutableSequence[google.cloud.documentai_v1beta2.types.ProcessDocumentRequest]): - Required. Individual requests for each - document. - parent (str): - Target project and location to make a call. - - Format: ``projects/{project-id}/locations/{location-id}``. - - If no location is specified, a region will be chosen - automatically. - """ - - requests: MutableSequence["ProcessDocumentRequest"] = proto.RepeatedField( - proto.MESSAGE, - number=1, - message="ProcessDocumentRequest", - ) - parent: str = proto.Field( - proto.STRING, - number=2, - ) - - -class ProcessDocumentRequest(proto.Message): - r"""Request to process one document. - - Attributes: - parent (str): - Target project and location to make a call. - - Format: ``projects/{project-id}/locations/{location-id}``. - - If no location is specified, a region will be chosen - automatically. This field is only populated when used in - ProcessDocument method. - input_config (google.cloud.documentai_v1beta2.types.InputConfig): - Required. Information about the input file. - output_config (google.cloud.documentai_v1beta2.types.OutputConfig): - The desired output location. This field is - only needed in BatchProcessDocumentsRequest. - document_type (str): - Specifies a known document type for deeper - structure detection. Valid values are currently - "general" and "invoice". If not provided, - "general"\ is used as default. If any other - value is given, the request is rejected. - table_extraction_params (google.cloud.documentai_v1beta2.types.TableExtractionParams): - Controls table extraction behavior. If not - specified, the system will decide reasonable - defaults. - form_extraction_params (google.cloud.documentai_v1beta2.types.FormExtractionParams): - Controls form extraction behavior. If not - specified, the system will decide reasonable - defaults. - entity_extraction_params (google.cloud.documentai_v1beta2.types.EntityExtractionParams): - Controls entity extraction behavior. If not - specified, the system will decide reasonable - defaults. - ocr_params (google.cloud.documentai_v1beta2.types.OcrParams): - Controls OCR behavior. If not specified, the - system will decide reasonable defaults. - automl_params (google.cloud.documentai_v1beta2.types.AutoMlParams): - Controls AutoML model prediction behavior. - AutoMlParams cannot be used together with other - Params. - """ - - parent: str = proto.Field( - proto.STRING, - number=9, - ) - input_config: "InputConfig" = proto.Field( - proto.MESSAGE, - number=1, - message="InputConfig", - ) - output_config: "OutputConfig" = proto.Field( - proto.MESSAGE, - number=2, - message="OutputConfig", - ) - document_type: str = proto.Field( - proto.STRING, - number=3, - ) - table_extraction_params: "TableExtractionParams" = proto.Field( - proto.MESSAGE, - number=4, - message="TableExtractionParams", - ) - form_extraction_params: "FormExtractionParams" = proto.Field( - proto.MESSAGE, - number=5, - message="FormExtractionParams", - ) - entity_extraction_params: "EntityExtractionParams" = proto.Field( - proto.MESSAGE, - number=6, - message="EntityExtractionParams", - ) - ocr_params: "OcrParams" = proto.Field( - proto.MESSAGE, - number=7, - message="OcrParams", - ) - automl_params: "AutoMlParams" = proto.Field( - proto.MESSAGE, - number=8, - message="AutoMlParams", - ) - - -class BatchProcessDocumentsResponse(proto.Message): - r"""Response to an batch document processing request. This is - returned in the LRO Operation after the operation is complete. - - Attributes: - responses (MutableSequence[google.cloud.documentai_v1beta2.types.ProcessDocumentResponse]): - Responses for each individual document. - """ - - responses: MutableSequence["ProcessDocumentResponse"] = proto.RepeatedField( - proto.MESSAGE, - number=1, - message="ProcessDocumentResponse", - ) - - -class ProcessDocumentResponse(proto.Message): - r"""Response to a single document processing request. - - Attributes: - input_config (google.cloud.documentai_v1beta2.types.InputConfig): - Information about the input file. This is the - same as the corresponding input config in the - request. - output_config (google.cloud.documentai_v1beta2.types.OutputConfig): - The output location of the parsed responses. The responses - are written to this location as JSON-serialized ``Document`` - objects. - """ - - input_config: "InputConfig" = proto.Field( - proto.MESSAGE, - number=1, - message="InputConfig", - ) - output_config: "OutputConfig" = proto.Field( - proto.MESSAGE, - number=2, - message="OutputConfig", - ) - - -class OcrParams(proto.Message): - r"""Parameters to control Optical Character Recognition (OCR) - behavior. - - Attributes: - language_hints (MutableSequence[str]): - List of languages to use for OCR. In most cases, an empty - value yields the best results since it enables automatic - language detection. For languages based on the Latin - alphabet, setting ``language_hints`` is not needed. In rare - cases, when the language of the text in the image is known, - setting a hint will help get better results (although it - will be a significant hindrance if the hint is wrong). - Document processing returns an error if one or more of the - specified languages is not one of the supported languages. - """ - - language_hints: MutableSequence[str] = proto.RepeatedField( - proto.STRING, - number=1, - ) - - -class TableExtractionParams(proto.Message): - r"""Parameters to control table extraction behavior. - - Attributes: - enabled (bool): - Whether to enable table extraction. - table_bound_hints (MutableSequence[google.cloud.documentai_v1beta2.types.TableBoundHint]): - Optional. Table bounding box hints that can - be provided to complex cases which our algorithm - cannot locate the table(s) in. - header_hints (MutableSequence[str]): - Optional. Reserved for future use. - model_version (str): - Model version of the table extraction system. - Default is "builtin/stable". Specify - "builtin/latest" for the latest model. - """ - - enabled: bool = proto.Field( - proto.BOOL, - number=1, - ) - table_bound_hints: MutableSequence["TableBoundHint"] = proto.RepeatedField( - proto.MESSAGE, - number=2, - message="TableBoundHint", - ) - header_hints: MutableSequence[str] = proto.RepeatedField( - proto.STRING, - number=3, - ) - model_version: str = proto.Field( - proto.STRING, - number=4, - ) - - -class TableBoundHint(proto.Message): - r"""A hint for a table bounding box on the page for table - parsing. - - Attributes: - page_number (int): - Optional. Page number for multi-paged inputs - this hint applies to. If not provided, this hint - will apply to all pages by default. This value - is 1-based. - bounding_box (google.cloud.documentai_v1beta2.types.BoundingPoly): - Bounding box hint for a table on this page. The coordinates - must be normalized to [0,1] and the bounding box must be an - axis-aligned rectangle. - """ - - page_number: int = proto.Field( - proto.INT32, - number=1, - ) - bounding_box: geometry.BoundingPoly = proto.Field( - proto.MESSAGE, - number=2, - message=geometry.BoundingPoly, - ) - - -class FormExtractionParams(proto.Message): - r"""Parameters to control form extraction behavior. - - Attributes: - enabled (bool): - Whether to enable form extraction. - key_value_pair_hints (MutableSequence[google.cloud.documentai_v1beta2.types.KeyValuePairHint]): - Reserved for future use. - model_version (str): - Model version of the form extraction system. Default is - "builtin/stable". Specify "builtin/latest" for the latest - model. For custom form models, specify: - "custom/{model_name}". Model name format is - "bucket_name/path/to/modeldir" corresponding to - "gs://bucket_name/path/to/modeldir" where annotated examples - are stored. - """ - - enabled: bool = proto.Field( - proto.BOOL, - number=1, - ) - key_value_pair_hints: MutableSequence["KeyValuePairHint"] = proto.RepeatedField( - proto.MESSAGE, - number=2, - message="KeyValuePairHint", - ) - model_version: str = proto.Field( - proto.STRING, - number=3, - ) - - -class KeyValuePairHint(proto.Message): - r"""Reserved for future use. - - Attributes: - key (str): - The key text for the hint. - value_types (MutableSequence[str]): - Type of the value. This is case-insensitive, and could be - one of: ADDRESS, LOCATION, ORGANIZATION, PERSON, - PHONE_NUMBER, ID, NUMBER, EMAIL, PRICE, TERMS, DATE, NAME. - Types not in this list will be ignored. - """ - - key: str = proto.Field( - proto.STRING, - number=1, - ) - value_types: MutableSequence[str] = proto.RepeatedField( - proto.STRING, - number=2, - ) - - -class EntityExtractionParams(proto.Message): - r"""Parameters to control entity extraction behavior. - - Attributes: - enabled (bool): - Whether to enable entity extraction. - model_version (str): - Model version of the entity extraction. - Default is "builtin/stable". Specify - "builtin/latest" for the latest model. - """ - - enabled: bool = proto.Field( - proto.BOOL, - number=1, - ) - model_version: str = proto.Field( - proto.STRING, - number=2, - ) - - -class AutoMlParams(proto.Message): - r"""Parameters to control AutoML model prediction behavior. - - Attributes: - model (str): - Resource name of the AutoML model. - - Format: - ``projects/{project-id}/locations/{location-id}/models/{model-id}``. - """ - - model: str = proto.Field( - proto.STRING, - number=1, - ) - - -class InputConfig(proto.Message): - r"""The desired input location and metadata. - - This message has `oneof`_ fields (mutually exclusive fields). - For each oneof, at most one member field can be set at the same time. - Setting any member of the oneof automatically clears all other - members. - - .. _oneof: https://proto-plus-python.readthedocs.io/en/stable/fields.html#oneofs-mutually-exclusive-fields - - Attributes: - gcs_source (google.cloud.documentai_v1beta2.types.GcsSource): - The Google Cloud Storage location to read the - input from. This must be a single file. - - This field is a member of `oneof`_ ``source``. - contents (bytes): - Content in bytes, represented as a stream of bytes. Note: As - with all ``bytes`` fields, proto buffer messages use a pure - binary representation, whereas JSON representations use - base64. - - This field only works for synchronous ProcessDocument - method. - - This field is a member of `oneof`_ ``source``. - mime_type (str): - Required. Mimetype of the input. Current supported mimetypes - are application/pdf, image/tiff, and image/gif. In addition, - application/json type is supported for requests with - [ProcessDocumentRequest.automl_params][google.cloud.documentai.v1beta2.ProcessDocumentRequest.automl_params] - field set. The JSON file needs to be in - [Document][google.cloud.documentai.v1beta2.Document] format. - """ - - gcs_source: "GcsSource" = proto.Field( - proto.MESSAGE, - number=1, - oneof="source", - message="GcsSource", - ) - contents: bytes = proto.Field( - proto.BYTES, - number=3, - oneof="source", - ) - mime_type: str = proto.Field( - proto.STRING, - number=2, - ) - - -class OutputConfig(proto.Message): - r"""The desired output location and metadata. - - .. _oneof: https://proto-plus-python.readthedocs.io/en/stable/fields.html#oneofs-mutually-exclusive-fields - - Attributes: - gcs_destination (google.cloud.documentai_v1beta2.types.GcsDestination): - The Google Cloud Storage location to write - the output to. - - This field is a member of `oneof`_ ``destination``. - pages_per_shard (int): - The max number of pages to include into each output Document - shard JSON on Google Cloud Storage. - - The valid range is [1, 100]. If not specified, the default - value is 20. - - For example, for one pdf file with 100 pages, 100 parsed - pages will be produced. If ``pages_per_shard`` = 20, then 5 - Document shard JSON files each containing 20 parsed pages - will be written under the prefix - [OutputConfig.gcs_destination.uri][] and suffix - pages-x-to-y.json where x and y are 1-indexed page numbers. - - Example GCS outputs with 157 pages and pages_per_shard = 50: - - pages-001-to-050.json pages-051-to-100.json - pages-101-to-150.json pages-151-to-157.json - """ - - gcs_destination: "GcsDestination" = proto.Field( - proto.MESSAGE, - number=1, - oneof="destination", - message="GcsDestination", - ) - pages_per_shard: int = proto.Field( - proto.INT32, - number=2, - ) - - -class GcsSource(proto.Message): - r"""The Google Cloud Storage location where the input file will - be read from. - - Attributes: - uri (str): - - """ - - uri: str = proto.Field( - proto.STRING, - number=1, - ) - - -class GcsDestination(proto.Message): - r"""The Google Cloud Storage location where the output file will - be written to. - - Attributes: - uri (str): - - """ - - uri: str = proto.Field( - proto.STRING, - number=1, - ) - - -class OperationMetadata(proto.Message): - r"""Contains metadata for the BatchProcessDocuments operation. - - Attributes: - state (google.cloud.documentai_v1beta2.types.OperationMetadata.State): - The state of the current batch processing. - state_message (str): - A message providing more details about the - current state of processing. - create_time (google.protobuf.timestamp_pb2.Timestamp): - The creation time of the operation. - update_time (google.protobuf.timestamp_pb2.Timestamp): - The last update time of the operation. - """ - - class State(proto.Enum): - r""" - - Values: - STATE_UNSPECIFIED (0): - The default value. This value is used if the - state is omitted. - ACCEPTED (1): - Request is received. - WAITING (2): - Request operation is waiting for scheduling. - RUNNING (3): - Request is being processed. - SUCCEEDED (4): - The batch processing completed successfully. - CANCELLED (5): - The batch processing was cancelled. - FAILED (6): - The batch processing has failed. - """ - STATE_UNSPECIFIED = 0 - ACCEPTED = 1 - WAITING = 2 - RUNNING = 3 - SUCCEEDED = 4 - CANCELLED = 5 - FAILED = 6 - - state: State = proto.Field( - proto.ENUM, - number=1, - enum=State, - ) - state_message: str = proto.Field( - proto.STRING, - number=2, - ) - create_time: timestamp_pb2.Timestamp = proto.Field( - proto.MESSAGE, - number=3, - message=timestamp_pb2.Timestamp, - ) - update_time: timestamp_pb2.Timestamp = proto.Field( - proto.MESSAGE, - number=4, - message=timestamp_pb2.Timestamp, - ) - - -__all__ = tuple(sorted(__protobuf__.manifest)) diff --git a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/types/geometry.py b/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/types/geometry.py deleted file mode 100644 index 49e42b88416a..000000000000 --- a/packages/google-cloud-documentai/google/cloud/documentai_v1beta2/types/geometry.py +++ /dev/null @@ -1,100 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2024 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -from __future__ import annotations - -from typing import MutableMapping, MutableSequence - -import proto # type: ignore - -__protobuf__ = proto.module( - package="google.cloud.documentai.v1beta2", - manifest={ - "Vertex", - "NormalizedVertex", - "BoundingPoly", - }, -) - - -class Vertex(proto.Message): - r"""A vertex represents a 2D point in the image. - NOTE: the vertex coordinates are in the same scale as the - original image. - - Attributes: - x (int): - X coordinate. - y (int): - Y coordinate (starts from the top of the - image). - """ - - x: int = proto.Field( - proto.INT32, - number=1, - ) - y: int = proto.Field( - proto.INT32, - number=2, - ) - - -class NormalizedVertex(proto.Message): - r"""A vertex represents a 2D point in the image. - NOTE: the normalized vertex coordinates are relative to the - original image and range from 0 to 1. - - Attributes: - x (float): - X coordinate. - y (float): - Y coordinate (starts from the top of the - image). - """ - - x: float = proto.Field( - proto.FLOAT, - number=1, - ) - y: float = proto.Field( - proto.FLOAT, - number=2, - ) - - -class BoundingPoly(proto.Message): - r"""A bounding polygon for the detected image annotation. - - Attributes: - vertices (MutableSequence[google.cloud.documentai_v1beta2.types.Vertex]): - The bounding polygon vertices. - normalized_vertices (MutableSequence[google.cloud.documentai_v1beta2.types.NormalizedVertex]): - The bounding polygon normalized vertices. - """ - - vertices: MutableSequence["Vertex"] = proto.RepeatedField( - proto.MESSAGE, - number=1, - message="Vertex", - ) - normalized_vertices: MutableSequence["NormalizedVertex"] = proto.RepeatedField( - proto.MESSAGE, - number=2, - message="NormalizedVertex", - ) - - -__all__ = tuple(sorted(__protobuf__.manifest)) diff --git a/packages/google-cloud-documentai/scripts/fixup_documentai_v1beta2_keywords.py b/packages/google-cloud-documentai/scripts/fixup_documentai_v1beta2_keywords.py deleted file mode 100644 index f49c705fd83e..000000000000 --- a/packages/google-cloud-documentai/scripts/fixup_documentai_v1beta2_keywords.py +++ /dev/null @@ -1,177 +0,0 @@ -#! /usr/bin/env python3 -# -*- coding: utf-8 -*- -# Copyright 2024 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -import argparse -import os -import libcst as cst -import pathlib -import sys -from typing import (Any, Callable, Dict, List, Sequence, Tuple) - - -def partition( - predicate: Callable[[Any], bool], - iterator: Sequence[Any] -) -> Tuple[List[Any], List[Any]]: - """A stable, out-of-place partition.""" - results = ([], []) - - for i in iterator: - results[int(predicate(i))].append(i) - - # Returns trueList, falseList - return results[1], results[0] - - -class documentaiCallTransformer(cst.CSTTransformer): - CTRL_PARAMS: Tuple[str] = ('retry', 'timeout', 'metadata') - METHOD_TO_PARAMS: Dict[str, Tuple[str]] = { - 'batch_process_documents': ('requests', 'parent', ), - 'process_document': ('input_config', 'parent', 'output_config', 'document_type', 'table_extraction_params', 'form_extraction_params', 'entity_extraction_params', 'ocr_params', 'automl_params', ), - } - - def leave_Call(self, original: cst.Call, updated: cst.Call) -> cst.CSTNode: - try: - key = original.func.attr.value - kword_params = self.METHOD_TO_PARAMS[key] - except (AttributeError, KeyError): - # Either not a method from the API or too convoluted to be sure. - return updated - - # If the existing code is valid, keyword args come after positional args. - # Therefore, all positional args must map to the first parameters. - args, kwargs = partition(lambda a: not bool(a.keyword), updated.args) - if any(k.keyword.value == "request" for k in kwargs): - # We've already fixed this file, don't fix it again. - return updated - - kwargs, ctrl_kwargs = partition( - lambda a: a.keyword.value not in self.CTRL_PARAMS, - kwargs - ) - - args, ctrl_args = args[:len(kword_params)], args[len(kword_params):] - ctrl_kwargs.extend(cst.Arg(value=a.value, keyword=cst.Name(value=ctrl)) - for a, ctrl in zip(ctrl_args, self.CTRL_PARAMS)) - - request_arg = cst.Arg( - value=cst.Dict([ - cst.DictElement( - cst.SimpleString("'{}'".format(name)), -cst.Element(value=arg.value) - ) - # Note: the args + kwargs looks silly, but keep in mind that - # the control parameters had to be stripped out, and that - # those could have been passed positionally or by keyword. - for name, arg in zip(kword_params, args + kwargs)]), - keyword=cst.Name("request") - ) - - return updated.with_changes( - args=[request_arg] + ctrl_kwargs - ) - - -def fix_files( - in_dir: pathlib.Path, - out_dir: pathlib.Path, - *, - transformer=documentaiCallTransformer(), -): - """Duplicate the input dir to the output dir, fixing file method calls. - - Preconditions: - * in_dir is a real directory - * out_dir is a real, empty directory - """ - pyfile_gen = ( - pathlib.Path(os.path.join(root, f)) - for root, _, files in os.walk(in_dir) - for f in files if os.path.splitext(f)[1] == ".py" - ) - - for fpath in pyfile_gen: - with open(fpath, 'r') as f: - src = f.read() - - # Parse the code and insert method call fixes. - tree = cst.parse_module(src) - updated = tree.visit(transformer) - - # Create the path and directory structure for the new file. - updated_path = out_dir.joinpath(fpath.relative_to(in_dir)) - updated_path.parent.mkdir(parents=True, exist_ok=True) - - # Generate the updated source file at the corresponding path. - with open(updated_path, 'w') as f: - f.write(updated.code) - - -if __name__ == '__main__': - parser = argparse.ArgumentParser( - description="""Fix up source that uses the documentai client library. - -The existing sources are NOT overwritten but are copied to output_dir with changes made. - -Note: This tool operates at a best-effort level at converting positional - parameters in client method calls to keyword based parameters. - Cases where it WILL FAIL include - A) * or ** expansion in a method call. - B) Calls via function or method alias (includes free function calls) - C) Indirect or dispatched calls (e.g. the method is looked up dynamically) - - These all constitute false negatives. The tool will also detect false - positives when an API method shares a name with another method. -""") - parser.add_argument( - '-d', - '--input-directory', - required=True, - dest='input_dir', - help='the input directory to walk for python files to fix up', - ) - parser.add_argument( - '-o', - '--output-directory', - required=True, - dest='output_dir', - help='the directory to output files fixed via un-flattening', - ) - args = parser.parse_args() - input_dir = pathlib.Path(args.input_dir) - output_dir = pathlib.Path(args.output_dir) - if not input_dir.is_dir(): - print( - f"input directory '{input_dir}' does not exist or is not a directory", - file=sys.stderr, - ) - sys.exit(-1) - - if not output_dir.is_dir(): - print( - f"output directory '{output_dir}' does not exist or is not a directory", - file=sys.stderr, - ) - sys.exit(-1) - - if os.listdir(output_dir): - print( - f"output directory '{output_dir}' is not empty", - file=sys.stderr, - ) - sys.exit(-1) - - fix_files(input_dir, output_dir) diff --git a/packages/google-cloud-documentai/tests/unit/gapic/documentai_v1beta2/__init__.py b/packages/google-cloud-documentai/tests/unit/gapic/documentai_v1beta2/__init__.py deleted file mode 100644 index 8f6cf068242c..000000000000 --- a/packages/google-cloud-documentai/tests/unit/gapic/documentai_v1beta2/__init__.py +++ /dev/null @@ -1,15 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2024 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# diff --git a/packages/google-cloud-documentai/tests/unit/gapic/documentai_v1beta2/test_document_understanding_service.py b/packages/google-cloud-documentai/tests/unit/gapic/documentai_v1beta2/test_document_understanding_service.py deleted file mode 100644 index f7f64d32db7e..000000000000 --- a/packages/google-cloud-documentai/tests/unit/gapic/documentai_v1beta2/test_document_understanding_service.py +++ /dev/null @@ -1,3341 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2024 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -import os - -# try/except added for compatibility with python < 3.8 -try: - from unittest import mock - from unittest.mock import AsyncMock # pragma: NO COVER -except ImportError: # pragma: NO COVER - import mock - -import math - -from google.api_core import ( - future, - gapic_v1, - grpc_helpers, - grpc_helpers_async, - operation, - operations_v1, - path_template, -) -from google.api_core import api_core_version, client_options -from google.api_core import exceptions as core_exceptions -from google.api_core import operation_async # type: ignore -from google.api_core import retry as retries -import google.auth -from google.auth import credentials as ga_credentials -from google.auth.exceptions import MutualTLSChannelError -from google.cloud.location import locations_pb2 -from google.longrunning import operations_pb2 # type: ignore -from google.oauth2 import service_account -from google.rpc import status_pb2 # type: ignore -import grpc -from grpc.experimental import aio -from proto.marshal.rules import wrappers -from proto.marshal.rules.dates import DurationRule, TimestampRule -import pytest - -from google.cloud.documentai_v1beta2.services.document_understanding_service import ( - DocumentUnderstandingServiceAsyncClient, - DocumentUnderstandingServiceClient, - transports, -) -from google.cloud.documentai_v1beta2.types import ( - document, - document_understanding, - geometry, -) - - -def client_cert_source_callback(): - return b"cert bytes", b"key bytes" - - -# If default endpoint is localhost, then default mtls endpoint will be the same. -# This method modifies the default endpoint so the client can produce a different -# mtls endpoint for endpoint testing purposes. -def modify_default_endpoint(client): - return ( - "foo.googleapis.com" - if ("localhost" in client.DEFAULT_ENDPOINT) - else client.DEFAULT_ENDPOINT - ) - - -# If default endpoint template is localhost, then default mtls endpoint will be the same. -# This method modifies the default endpoint template so the client can produce a different -# mtls endpoint for endpoint testing purposes. -def modify_default_endpoint_template(client): - return ( - "test.{UNIVERSE_DOMAIN}" - if ("localhost" in client._DEFAULT_ENDPOINT_TEMPLATE) - else client._DEFAULT_ENDPOINT_TEMPLATE - ) - - -def test__get_default_mtls_endpoint(): - api_endpoint = "example.googleapis.com" - api_mtls_endpoint = "example.mtls.googleapis.com" - sandbox_endpoint = "example.sandbox.googleapis.com" - sandbox_mtls_endpoint = "example.mtls.sandbox.googleapis.com" - non_googleapi = "api.example.com" - - assert DocumentUnderstandingServiceClient._get_default_mtls_endpoint(None) is None - assert ( - DocumentUnderstandingServiceClient._get_default_mtls_endpoint(api_endpoint) - == api_mtls_endpoint - ) - assert ( - DocumentUnderstandingServiceClient._get_default_mtls_endpoint(api_mtls_endpoint) - == api_mtls_endpoint - ) - assert ( - DocumentUnderstandingServiceClient._get_default_mtls_endpoint(sandbox_endpoint) - == sandbox_mtls_endpoint - ) - assert ( - DocumentUnderstandingServiceClient._get_default_mtls_endpoint( - sandbox_mtls_endpoint - ) - == sandbox_mtls_endpoint - ) - assert ( - DocumentUnderstandingServiceClient._get_default_mtls_endpoint(non_googleapi) - == non_googleapi - ) - - -def test__read_environment_variables(): - assert DocumentUnderstandingServiceClient._read_environment_variables() == ( - False, - "auto", - None, - ) - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - assert DocumentUnderstandingServiceClient._read_environment_variables() == ( - True, - "auto", - None, - ) - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - assert DocumentUnderstandingServiceClient._read_environment_variables() == ( - False, - "auto", - None, - ) - - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "Unsupported"} - ): - with pytest.raises(ValueError) as excinfo: - DocumentUnderstandingServiceClient._read_environment_variables() - assert ( - str(excinfo.value) - == "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`" - ) - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}): - assert DocumentUnderstandingServiceClient._read_environment_variables() == ( - False, - "never", - None, - ) - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}): - assert DocumentUnderstandingServiceClient._read_environment_variables() == ( - False, - "always", - None, - ) - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}): - assert DocumentUnderstandingServiceClient._read_environment_variables() == ( - False, - "auto", - None, - ) - - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "Unsupported"}): - with pytest.raises(MutualTLSChannelError) as excinfo: - DocumentUnderstandingServiceClient._read_environment_variables() - assert ( - str(excinfo.value) - == "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`" - ) - - with mock.patch.dict(os.environ, {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "foo.com"}): - assert DocumentUnderstandingServiceClient._read_environment_variables() == ( - False, - "auto", - "foo.com", - ) - - -def test__get_client_cert_source(): - mock_provided_cert_source = mock.Mock() - mock_default_cert_source = mock.Mock() - - assert ( - DocumentUnderstandingServiceClient._get_client_cert_source(None, False) is None - ) - assert ( - DocumentUnderstandingServiceClient._get_client_cert_source( - mock_provided_cert_source, False - ) - is None - ) - assert ( - DocumentUnderstandingServiceClient._get_client_cert_source( - mock_provided_cert_source, True - ) - == mock_provided_cert_source - ) - - with mock.patch( - "google.auth.transport.mtls.has_default_client_cert_source", return_value=True - ): - with mock.patch( - "google.auth.transport.mtls.default_client_cert_source", - return_value=mock_default_cert_source, - ): - assert ( - DocumentUnderstandingServiceClient._get_client_cert_source(None, True) - is mock_default_cert_source - ) - assert ( - DocumentUnderstandingServiceClient._get_client_cert_source( - mock_provided_cert_source, "true" - ) - is mock_provided_cert_source - ) - - -@mock.patch.object( - DocumentUnderstandingServiceClient, - "_DEFAULT_ENDPOINT_TEMPLATE", - modify_default_endpoint_template(DocumentUnderstandingServiceClient), -) -@mock.patch.object( - DocumentUnderstandingServiceAsyncClient, - "_DEFAULT_ENDPOINT_TEMPLATE", - modify_default_endpoint_template(DocumentUnderstandingServiceAsyncClient), -) -def test__get_api_endpoint(): - api_override = "foo.com" - mock_client_cert_source = mock.Mock() - default_universe = DocumentUnderstandingServiceClient._DEFAULT_UNIVERSE - default_endpoint = ( - DocumentUnderstandingServiceClient._DEFAULT_ENDPOINT_TEMPLATE.format( - UNIVERSE_DOMAIN=default_universe - ) - ) - mock_universe = "bar.com" - mock_endpoint = ( - DocumentUnderstandingServiceClient._DEFAULT_ENDPOINT_TEMPLATE.format( - UNIVERSE_DOMAIN=mock_universe - ) - ) - - assert ( - DocumentUnderstandingServiceClient._get_api_endpoint( - api_override, mock_client_cert_source, default_universe, "always" - ) - == api_override - ) - assert ( - DocumentUnderstandingServiceClient._get_api_endpoint( - None, mock_client_cert_source, default_universe, "auto" - ) - == DocumentUnderstandingServiceClient.DEFAULT_MTLS_ENDPOINT - ) - assert ( - DocumentUnderstandingServiceClient._get_api_endpoint( - None, None, default_universe, "auto" - ) - == default_endpoint - ) - assert ( - DocumentUnderstandingServiceClient._get_api_endpoint( - None, None, default_universe, "always" - ) - == DocumentUnderstandingServiceClient.DEFAULT_MTLS_ENDPOINT - ) - assert ( - DocumentUnderstandingServiceClient._get_api_endpoint( - None, mock_client_cert_source, default_universe, "always" - ) - == DocumentUnderstandingServiceClient.DEFAULT_MTLS_ENDPOINT - ) - assert ( - DocumentUnderstandingServiceClient._get_api_endpoint( - None, None, mock_universe, "never" - ) - == mock_endpoint - ) - assert ( - DocumentUnderstandingServiceClient._get_api_endpoint( - None, None, default_universe, "never" - ) - == default_endpoint - ) - - with pytest.raises(MutualTLSChannelError) as excinfo: - DocumentUnderstandingServiceClient._get_api_endpoint( - None, mock_client_cert_source, mock_universe, "auto" - ) - assert ( - str(excinfo.value) - == "mTLS is not supported in any universe other than googleapis.com." - ) - - -def test__get_universe_domain(): - client_universe_domain = "foo.com" - universe_domain_env = "bar.com" - - assert ( - DocumentUnderstandingServiceClient._get_universe_domain( - client_universe_domain, universe_domain_env - ) - == client_universe_domain - ) - assert ( - DocumentUnderstandingServiceClient._get_universe_domain( - None, universe_domain_env - ) - == universe_domain_env - ) - assert ( - DocumentUnderstandingServiceClient._get_universe_domain(None, None) - == DocumentUnderstandingServiceClient._DEFAULT_UNIVERSE - ) - - with pytest.raises(ValueError) as excinfo: - DocumentUnderstandingServiceClient._get_universe_domain("", None) - assert str(excinfo.value) == "Universe Domain cannot be an empty string." - - -@pytest.mark.parametrize( - "client_class,transport_class,transport_name", - [ - ( - DocumentUnderstandingServiceClient, - transports.DocumentUnderstandingServiceGrpcTransport, - "grpc", - ), - ], -) -def test__validate_universe_domain(client_class, transport_class, transport_name): - client = client_class( - transport=transport_class(credentials=ga_credentials.AnonymousCredentials()) - ) - assert client._validate_universe_domain() == True - - # Test the case when universe is already validated. - assert client._validate_universe_domain() == True - - if transport_name == "grpc": - # Test the case where credentials are provided by the - # `local_channel_credentials`. The default universes in both match. - channel = grpc.secure_channel( - "http://localhost/", grpc.local_channel_credentials() - ) - client = client_class(transport=transport_class(channel=channel)) - assert client._validate_universe_domain() == True - - # Test the case where credentials do not exist: e.g. a transport is provided - # with no credentials. Validation should still succeed because there is no - # mismatch with non-existent credentials. - channel = grpc.secure_channel( - "http://localhost/", grpc.local_channel_credentials() - ) - transport = transport_class(channel=channel) - transport._credentials = None - client = client_class(transport=transport) - assert client._validate_universe_domain() == True - - # TODO: This is needed to cater for older versions of google-auth - # Make this test unconditional once the minimum supported version of - # google-auth becomes 2.23.0 or higher. - google_auth_major, google_auth_minor = [ - int(part) for part in google.auth.__version__.split(".")[0:2] - ] - if google_auth_major > 2 or (google_auth_major == 2 and google_auth_minor >= 23): - credentials = ga_credentials.AnonymousCredentials() - credentials._universe_domain = "foo.com" - # Test the case when there is a universe mismatch from the credentials. - client = client_class(transport=transport_class(credentials=credentials)) - with pytest.raises(ValueError) as excinfo: - client._validate_universe_domain() - assert ( - str(excinfo.value) - == "The configured universe domain (googleapis.com) does not match the universe domain found in the credentials (foo.com). If you haven't configured the universe domain explicitly, `googleapis.com` is the default." - ) - - # Test the case when there is a universe mismatch from the client. - # - # TODO: Make this test unconditional once the minimum supported version of - # google-api-core becomes 2.15.0 or higher. - api_core_major, api_core_minor = [ - int(part) for part in api_core_version.__version__.split(".")[0:2] - ] - if api_core_major > 2 or (api_core_major == 2 and api_core_minor >= 15): - client = client_class( - client_options={"universe_domain": "bar.com"}, - transport=transport_class( - credentials=ga_credentials.AnonymousCredentials(), - ), - ) - with pytest.raises(ValueError) as excinfo: - client._validate_universe_domain() - assert ( - str(excinfo.value) - == "The configured universe domain (bar.com) does not match the universe domain found in the credentials (googleapis.com). If you haven't configured the universe domain explicitly, `googleapis.com` is the default." - ) - - # Test that ValueError is raised if universe_domain is provided via client options and credentials is None - with pytest.raises(ValueError): - client._compare_universes("foo.bar", None) - - -@pytest.mark.parametrize( - "client_class,transport_name", - [ - (DocumentUnderstandingServiceClient, "grpc"), - (DocumentUnderstandingServiceAsyncClient, "grpc_asyncio"), - ], -) -def test_document_understanding_service_client_from_service_account_info( - client_class, transport_name -): - creds = ga_credentials.AnonymousCredentials() - with mock.patch.object( - service_account.Credentials, "from_service_account_info" - ) as factory: - factory.return_value = creds - info = {"valid": True} - client = client_class.from_service_account_info(info, transport=transport_name) - assert client.transport._credentials == creds - assert isinstance(client, client_class) - - assert client.transport._host == ("documentai.googleapis.com:443") - - -@pytest.mark.parametrize( - "transport_class,transport_name", - [ - (transports.DocumentUnderstandingServiceGrpcTransport, "grpc"), - (transports.DocumentUnderstandingServiceGrpcAsyncIOTransport, "grpc_asyncio"), - ], -) -def test_document_understanding_service_client_service_account_always_use_jwt( - transport_class, transport_name -): - with mock.patch.object( - service_account.Credentials, "with_always_use_jwt_access", create=True - ) as use_jwt: - creds = service_account.Credentials(None, None, None) - transport = transport_class(credentials=creds, always_use_jwt_access=True) - use_jwt.assert_called_once_with(True) - - with mock.patch.object( - service_account.Credentials, "with_always_use_jwt_access", create=True - ) as use_jwt: - creds = service_account.Credentials(None, None, None) - transport = transport_class(credentials=creds, always_use_jwt_access=False) - use_jwt.assert_not_called() - - -@pytest.mark.parametrize( - "client_class,transport_name", - [ - (DocumentUnderstandingServiceClient, "grpc"), - (DocumentUnderstandingServiceAsyncClient, "grpc_asyncio"), - ], -) -def test_document_understanding_service_client_from_service_account_file( - client_class, transport_name -): - creds = ga_credentials.AnonymousCredentials() - with mock.patch.object( - service_account.Credentials, "from_service_account_file" - ) as factory: - factory.return_value = creds - client = client_class.from_service_account_file( - "dummy/file/path.json", transport=transport_name - ) - assert client.transport._credentials == creds - assert isinstance(client, client_class) - - client = client_class.from_service_account_json( - "dummy/file/path.json", transport=transport_name - ) - assert client.transport._credentials == creds - assert isinstance(client, client_class) - - assert client.transport._host == ("documentai.googleapis.com:443") - - -def test_document_understanding_service_client_get_transport_class(): - transport = DocumentUnderstandingServiceClient.get_transport_class() - available_transports = [ - transports.DocumentUnderstandingServiceGrpcTransport, - ] - assert transport in available_transports - - transport = DocumentUnderstandingServiceClient.get_transport_class("grpc") - assert transport == transports.DocumentUnderstandingServiceGrpcTransport - - -@pytest.mark.parametrize( - "client_class,transport_class,transport_name", - [ - ( - DocumentUnderstandingServiceClient, - transports.DocumentUnderstandingServiceGrpcTransport, - "grpc", - ), - ( - DocumentUnderstandingServiceAsyncClient, - transports.DocumentUnderstandingServiceGrpcAsyncIOTransport, - "grpc_asyncio", - ), - ], -) -@mock.patch.object( - DocumentUnderstandingServiceClient, - "_DEFAULT_ENDPOINT_TEMPLATE", - modify_default_endpoint_template(DocumentUnderstandingServiceClient), -) -@mock.patch.object( - DocumentUnderstandingServiceAsyncClient, - "_DEFAULT_ENDPOINT_TEMPLATE", - modify_default_endpoint_template(DocumentUnderstandingServiceAsyncClient), -) -def test_document_understanding_service_client_client_options( - client_class, transport_class, transport_name -): - # Check that if channel is provided we won't create a new one. - with mock.patch.object( - DocumentUnderstandingServiceClient, "get_transport_class" - ) as gtc: - transport = transport_class(credentials=ga_credentials.AnonymousCredentials()) - client = client_class(transport=transport) - gtc.assert_not_called() - - # Check that if channel is provided via str we will create a new one. - with mock.patch.object( - DocumentUnderstandingServiceClient, "get_transport_class" - ) as gtc: - client = client_class(transport=transport_name) - gtc.assert_called() - - # Check the case api_endpoint is provided. - options = client_options.ClientOptions(api_endpoint="squid.clam.whelk") - with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class(transport=transport_name, client_options=options) - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host="squid.clam.whelk", - scopes=None, - client_cert_source_for_mtls=None, - quota_project_id=None, - client_info=transports.base.DEFAULT_CLIENT_INFO, - always_use_jwt_access=True, - api_audience=None, - ) - - # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is - # "never". - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}): - with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class(transport=transport_name) - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client._DEFAULT_ENDPOINT_TEMPLATE.format( - UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE - ), - scopes=None, - client_cert_source_for_mtls=None, - quota_project_id=None, - client_info=transports.base.DEFAULT_CLIENT_INFO, - always_use_jwt_access=True, - api_audience=None, - ) - - # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is - # "always". - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}): - with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class(transport=transport_name) - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client.DEFAULT_MTLS_ENDPOINT, - scopes=None, - client_cert_source_for_mtls=None, - quota_project_id=None, - client_info=transports.base.DEFAULT_CLIENT_INFO, - always_use_jwt_access=True, - api_audience=None, - ) - - # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has - # unsupported value. - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "Unsupported"}): - with pytest.raises(MutualTLSChannelError) as excinfo: - client = client_class(transport=transport_name) - assert ( - str(excinfo.value) - == "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`" - ) - - # Check the case GOOGLE_API_USE_CLIENT_CERTIFICATE has unsupported value. - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "Unsupported"} - ): - with pytest.raises(ValueError) as excinfo: - client = client_class(transport=transport_name) - assert ( - str(excinfo.value) - == "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`" - ) - - # Check the case quota_project_id is provided - options = client_options.ClientOptions(quota_project_id="octopus") - with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class(client_options=options, transport=transport_name) - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client._DEFAULT_ENDPOINT_TEMPLATE.format( - UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE - ), - scopes=None, - client_cert_source_for_mtls=None, - quota_project_id="octopus", - client_info=transports.base.DEFAULT_CLIENT_INFO, - always_use_jwt_access=True, - api_audience=None, - ) - # Check the case api_endpoint is provided - options = client_options.ClientOptions( - api_audience="https://language.googleapis.com" - ) - with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class(client_options=options, transport=transport_name) - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client._DEFAULT_ENDPOINT_TEMPLATE.format( - UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE - ), - scopes=None, - client_cert_source_for_mtls=None, - quota_project_id=None, - client_info=transports.base.DEFAULT_CLIENT_INFO, - always_use_jwt_access=True, - api_audience="https://language.googleapis.com", - ) - - -@pytest.mark.parametrize( - "client_class,transport_class,transport_name,use_client_cert_env", - [ - ( - DocumentUnderstandingServiceClient, - transports.DocumentUnderstandingServiceGrpcTransport, - "grpc", - "true", - ), - ( - DocumentUnderstandingServiceAsyncClient, - transports.DocumentUnderstandingServiceGrpcAsyncIOTransport, - "grpc_asyncio", - "true", - ), - ( - DocumentUnderstandingServiceClient, - transports.DocumentUnderstandingServiceGrpcTransport, - "grpc", - "false", - ), - ( - DocumentUnderstandingServiceAsyncClient, - transports.DocumentUnderstandingServiceGrpcAsyncIOTransport, - "grpc_asyncio", - "false", - ), - ], -) -@mock.patch.object( - DocumentUnderstandingServiceClient, - "_DEFAULT_ENDPOINT_TEMPLATE", - modify_default_endpoint_template(DocumentUnderstandingServiceClient), -) -@mock.patch.object( - DocumentUnderstandingServiceAsyncClient, - "_DEFAULT_ENDPOINT_TEMPLATE", - modify_default_endpoint_template(DocumentUnderstandingServiceAsyncClient), -) -@mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}) -def test_document_understanding_service_client_mtls_env_auto( - client_class, transport_class, transport_name, use_client_cert_env -): - # This tests the endpoint autoswitch behavior. Endpoint is autoswitched to the default - # mtls endpoint, if GOOGLE_API_USE_CLIENT_CERTIFICATE is "true" and client cert exists. - - # Check the case client_cert_source is provided. Whether client cert is used depends on - # GOOGLE_API_USE_CLIENT_CERTIFICATE value. - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} - ): - options = client_options.ClientOptions( - client_cert_source=client_cert_source_callback - ) - with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class(client_options=options, transport=transport_name) - - if use_client_cert_env == "false": - expected_client_cert_source = None - expected_host = client._DEFAULT_ENDPOINT_TEMPLATE.format( - UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE - ) - else: - expected_client_cert_source = client_cert_source_callback - expected_host = client.DEFAULT_MTLS_ENDPOINT - - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=expected_host, - scopes=None, - client_cert_source_for_mtls=expected_client_cert_source, - quota_project_id=None, - client_info=transports.base.DEFAULT_CLIENT_INFO, - always_use_jwt_access=True, - api_audience=None, - ) - - # Check the case ADC client cert is provided. Whether client cert is used depends on - # GOOGLE_API_USE_CLIENT_CERTIFICATE value. - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} - ): - with mock.patch.object(transport_class, "__init__") as patched: - with mock.patch( - "google.auth.transport.mtls.has_default_client_cert_source", - return_value=True, - ): - with mock.patch( - "google.auth.transport.mtls.default_client_cert_source", - return_value=client_cert_source_callback, - ): - if use_client_cert_env == "false": - expected_host = client._DEFAULT_ENDPOINT_TEMPLATE.format( - UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE - ) - expected_client_cert_source = None - else: - expected_host = client.DEFAULT_MTLS_ENDPOINT - expected_client_cert_source = client_cert_source_callback - - patched.return_value = None - client = client_class(transport=transport_name) - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=expected_host, - scopes=None, - client_cert_source_for_mtls=expected_client_cert_source, - quota_project_id=None, - client_info=transports.base.DEFAULT_CLIENT_INFO, - always_use_jwt_access=True, - api_audience=None, - ) - - # Check the case client_cert_source and ADC client cert are not provided. - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} - ): - with mock.patch.object(transport_class, "__init__") as patched: - with mock.patch( - "google.auth.transport.mtls.has_default_client_cert_source", - return_value=False, - ): - patched.return_value = None - client = client_class(transport=transport_name) - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client._DEFAULT_ENDPOINT_TEMPLATE.format( - UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE - ), - scopes=None, - client_cert_source_for_mtls=None, - quota_project_id=None, - client_info=transports.base.DEFAULT_CLIENT_INFO, - always_use_jwt_access=True, - api_audience=None, - ) - - -@pytest.mark.parametrize( - "client_class", - [DocumentUnderstandingServiceClient, DocumentUnderstandingServiceAsyncClient], -) -@mock.patch.object( - DocumentUnderstandingServiceClient, - "DEFAULT_ENDPOINT", - modify_default_endpoint(DocumentUnderstandingServiceClient), -) -@mock.patch.object( - DocumentUnderstandingServiceAsyncClient, - "DEFAULT_ENDPOINT", - modify_default_endpoint(DocumentUnderstandingServiceAsyncClient), -) -def test_document_understanding_service_client_get_mtls_endpoint_and_cert_source( - client_class, -): - mock_client_cert_source = mock.Mock() - - # Test the case GOOGLE_API_USE_CLIENT_CERTIFICATE is "true". - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - mock_api_endpoint = "foo" - options = client_options.ClientOptions( - client_cert_source=mock_client_cert_source, api_endpoint=mock_api_endpoint - ) - api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source( - options - ) - assert api_endpoint == mock_api_endpoint - assert cert_source == mock_client_cert_source - - # Test the case GOOGLE_API_USE_CLIENT_CERTIFICATE is "false". - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): - mock_client_cert_source = mock.Mock() - mock_api_endpoint = "foo" - options = client_options.ClientOptions( - client_cert_source=mock_client_cert_source, api_endpoint=mock_api_endpoint - ) - api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source( - options - ) - assert api_endpoint == mock_api_endpoint - assert cert_source is None - - # Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "never". - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}): - api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source() - assert api_endpoint == client_class.DEFAULT_ENDPOINT - assert cert_source is None - - # Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "always". - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}): - api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source() - assert api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT - assert cert_source is None - - # Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "auto" and default cert doesn't exist. - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - with mock.patch( - "google.auth.transport.mtls.has_default_client_cert_source", - return_value=False, - ): - api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source() - assert api_endpoint == client_class.DEFAULT_ENDPOINT - assert cert_source is None - - # Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "auto" and default cert exists. - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - with mock.patch( - "google.auth.transport.mtls.has_default_client_cert_source", - return_value=True, - ): - with mock.patch( - "google.auth.transport.mtls.default_client_cert_source", - return_value=mock_client_cert_source, - ): - ( - api_endpoint, - cert_source, - ) = client_class.get_mtls_endpoint_and_cert_source() - assert api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT - assert cert_source == mock_client_cert_source - - # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has - # unsupported value. - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "Unsupported"}): - with pytest.raises(MutualTLSChannelError) as excinfo: - client_class.get_mtls_endpoint_and_cert_source() - - assert ( - str(excinfo.value) - == "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`" - ) - - # Check the case GOOGLE_API_USE_CLIENT_CERTIFICATE has unsupported value. - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "Unsupported"} - ): - with pytest.raises(ValueError) as excinfo: - client_class.get_mtls_endpoint_and_cert_source() - - assert ( - str(excinfo.value) - == "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`" - ) - - -@pytest.mark.parametrize( - "client_class", - [DocumentUnderstandingServiceClient, DocumentUnderstandingServiceAsyncClient], -) -@mock.patch.object( - DocumentUnderstandingServiceClient, - "_DEFAULT_ENDPOINT_TEMPLATE", - modify_default_endpoint_template(DocumentUnderstandingServiceClient), -) -@mock.patch.object( - DocumentUnderstandingServiceAsyncClient, - "_DEFAULT_ENDPOINT_TEMPLATE", - modify_default_endpoint_template(DocumentUnderstandingServiceAsyncClient), -) -def test_document_understanding_service_client_client_api_endpoint(client_class): - mock_client_cert_source = client_cert_source_callback - api_override = "foo.com" - default_universe = DocumentUnderstandingServiceClient._DEFAULT_UNIVERSE - default_endpoint = ( - DocumentUnderstandingServiceClient._DEFAULT_ENDPOINT_TEMPLATE.format( - UNIVERSE_DOMAIN=default_universe - ) - ) - mock_universe = "bar.com" - mock_endpoint = ( - DocumentUnderstandingServiceClient._DEFAULT_ENDPOINT_TEMPLATE.format( - UNIVERSE_DOMAIN=mock_universe - ) - ) - - # If ClientOptions.api_endpoint is set and GOOGLE_API_USE_CLIENT_CERTIFICATE="true", - # use ClientOptions.api_endpoint as the api endpoint regardless. - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): - with mock.patch( - "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" - ): - options = client_options.ClientOptions( - client_cert_source=mock_client_cert_source, api_endpoint=api_override - ) - client = client_class( - client_options=options, - credentials=ga_credentials.AnonymousCredentials(), - ) - assert client.api_endpoint == api_override - - # If ClientOptions.api_endpoint is not set and GOOGLE_API_USE_MTLS_ENDPOINT="never", - # use the _DEFAULT_ENDPOINT_TEMPLATE populated with GDU as the api endpoint. - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}): - client = client_class(credentials=ga_credentials.AnonymousCredentials()) - assert client.api_endpoint == default_endpoint - - # If ClientOptions.api_endpoint is not set and GOOGLE_API_USE_MTLS_ENDPOINT="always", - # use the DEFAULT_MTLS_ENDPOINT as the api endpoint. - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}): - client = client_class(credentials=ga_credentials.AnonymousCredentials()) - assert client.api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT - - # If ClientOptions.api_endpoint is not set, GOOGLE_API_USE_MTLS_ENDPOINT="auto" (default), - # GOOGLE_API_USE_CLIENT_CERTIFICATE="false" (default), default cert source doesn't exist, - # and ClientOptions.universe_domain="bar.com", - # use the _DEFAULT_ENDPOINT_TEMPLATE populated with universe domain as the api endpoint. - options = client_options.ClientOptions() - universe_exists = hasattr(options, "universe_domain") - if universe_exists: - options = client_options.ClientOptions(universe_domain=mock_universe) - client = client_class( - client_options=options, credentials=ga_credentials.AnonymousCredentials() - ) - else: - client = client_class( - client_options=options, credentials=ga_credentials.AnonymousCredentials() - ) - assert client.api_endpoint == ( - mock_endpoint if universe_exists else default_endpoint - ) - assert client.universe_domain == ( - mock_universe if universe_exists else default_universe - ) - - # If ClientOptions does not have a universe domain attribute and GOOGLE_API_USE_MTLS_ENDPOINT="never", - # use the _DEFAULT_ENDPOINT_TEMPLATE populated with GDU as the api endpoint. - options = client_options.ClientOptions() - if hasattr(options, "universe_domain"): - delattr(options, "universe_domain") - with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}): - client = client_class( - client_options=options, credentials=ga_credentials.AnonymousCredentials() - ) - assert client.api_endpoint == default_endpoint - - -@pytest.mark.parametrize( - "client_class,transport_class,transport_name", - [ - ( - DocumentUnderstandingServiceClient, - transports.DocumentUnderstandingServiceGrpcTransport, - "grpc", - ), - ( - DocumentUnderstandingServiceAsyncClient, - transports.DocumentUnderstandingServiceGrpcAsyncIOTransport, - "grpc_asyncio", - ), - ], -) -def test_document_understanding_service_client_client_options_scopes( - client_class, transport_class, transport_name -): - # Check the case scopes are provided. - options = client_options.ClientOptions( - scopes=["1", "2"], - ) - with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class(client_options=options, transport=transport_name) - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client._DEFAULT_ENDPOINT_TEMPLATE.format( - UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE - ), - scopes=["1", "2"], - client_cert_source_for_mtls=None, - quota_project_id=None, - client_info=transports.base.DEFAULT_CLIENT_INFO, - always_use_jwt_access=True, - api_audience=None, - ) - - -@pytest.mark.parametrize( - "client_class,transport_class,transport_name,grpc_helpers", - [ - ( - DocumentUnderstandingServiceClient, - transports.DocumentUnderstandingServiceGrpcTransport, - "grpc", - grpc_helpers, - ), - ( - DocumentUnderstandingServiceAsyncClient, - transports.DocumentUnderstandingServiceGrpcAsyncIOTransport, - "grpc_asyncio", - grpc_helpers_async, - ), - ], -) -def test_document_understanding_service_client_client_options_credentials_file( - client_class, transport_class, transport_name, grpc_helpers -): - # Check the case credentials file is provided. - options = client_options.ClientOptions(credentials_file="credentials.json") - - with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class(client_options=options, transport=transport_name) - patched.assert_called_once_with( - credentials=None, - credentials_file="credentials.json", - host=client._DEFAULT_ENDPOINT_TEMPLATE.format( - UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE - ), - scopes=None, - client_cert_source_for_mtls=None, - quota_project_id=None, - client_info=transports.base.DEFAULT_CLIENT_INFO, - always_use_jwt_access=True, - api_audience=None, - ) - - -def test_document_understanding_service_client_client_options_from_dict(): - with mock.patch( - "google.cloud.documentai_v1beta2.services.document_understanding_service.transports.DocumentUnderstandingServiceGrpcTransport.__init__" - ) as grpc_transport: - grpc_transport.return_value = None - client = DocumentUnderstandingServiceClient( - client_options={"api_endpoint": "squid.clam.whelk"} - ) - grpc_transport.assert_called_once_with( - credentials=None, - credentials_file=None, - host="squid.clam.whelk", - scopes=None, - client_cert_source_for_mtls=None, - quota_project_id=None, - client_info=transports.base.DEFAULT_CLIENT_INFO, - always_use_jwt_access=True, - api_audience=None, - ) - - -@pytest.mark.parametrize( - "client_class,transport_class,transport_name,grpc_helpers", - [ - ( - DocumentUnderstandingServiceClient, - transports.DocumentUnderstandingServiceGrpcTransport, - "grpc", - grpc_helpers, - ), - ( - DocumentUnderstandingServiceAsyncClient, - transports.DocumentUnderstandingServiceGrpcAsyncIOTransport, - "grpc_asyncio", - grpc_helpers_async, - ), - ], -) -def test_document_understanding_service_client_create_channel_credentials_file( - client_class, transport_class, transport_name, grpc_helpers -): - # Check the case credentials file is provided. - options = client_options.ClientOptions(credentials_file="credentials.json") - - with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class(client_options=options, transport=transport_name) - patched.assert_called_once_with( - credentials=None, - credentials_file="credentials.json", - host=client._DEFAULT_ENDPOINT_TEMPLATE.format( - UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE - ), - scopes=None, - client_cert_source_for_mtls=None, - quota_project_id=None, - client_info=transports.base.DEFAULT_CLIENT_INFO, - always_use_jwt_access=True, - api_audience=None, - ) - - # test that the credentials from file are saved and used as the credentials. - with mock.patch.object( - google.auth, "load_credentials_from_file", autospec=True - ) as load_creds, mock.patch.object( - google.auth, "default", autospec=True - ) as adc, mock.patch.object( - grpc_helpers, "create_channel" - ) as create_channel: - creds = ga_credentials.AnonymousCredentials() - file_creds = ga_credentials.AnonymousCredentials() - load_creds.return_value = (file_creds, None) - adc.return_value = (creds, None) - client = client_class(client_options=options, transport=transport_name) - create_channel.assert_called_with( - "documentai.googleapis.com:443", - credentials=file_creds, - credentials_file=None, - quota_project_id=None, - default_scopes=("https://www.googleapis.com/auth/cloud-platform",), - scopes=None, - default_host="documentai.googleapis.com", - ssl_credentials=None, - options=[ - ("grpc.max_send_message_length", -1), - ("grpc.max_receive_message_length", -1), - ], - ) - - -@pytest.mark.parametrize( - "request_type", - [ - document_understanding.BatchProcessDocumentsRequest, - dict, - ], -) -def test_batch_process_documents(request_type, transport: str = "grpc"): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) - - # Everything is optional in proto3 as far as the runtime is concerned, - # and we are mocking out the actual API, so just send an empty request. - request = request_type() - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.batch_process_documents), "__call__" - ) as call: - # Designate an appropriate return value for the call. - call.return_value = operations_pb2.Operation(name="operations/spam") - response = client.batch_process_documents(request) - - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - request = document_understanding.BatchProcessDocumentsRequest() - assert args[0] == request - - # Establish that the response is the type that we expect. - assert isinstance(response, future.Future) - - -def test_batch_process_documents_empty_call(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.batch_process_documents), "__call__" - ) as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.batch_process_documents() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == document_understanding.BatchProcessDocumentsRequest() - - -def test_batch_process_documents_non_empty_request_with_auto_populated_field(): - # This test is a coverage failsafe to make sure that UUID4 fields are - # automatically populated, according to AIP-4235, with non-empty requests. - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Populate all string fields in the request which are not UUID4 - # since we want to check that UUID4 are populated automatically - # if they meet the requirements of AIP 4235. - request = document_understanding.BatchProcessDocumentsRequest( - parent="parent_value", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.batch_process_documents), "__call__" - ) as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.batch_process_documents(request=request) - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == document_understanding.BatchProcessDocumentsRequest( - parent="parent_value", - ) - - -def test_batch_process_documents_use_cached_wrapped_rpc(): - # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, - # instead of constructing them on each call - with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Should wrap all calls on client creation - assert wrapper_fn.call_count > 0 - wrapper_fn.reset_mock() - - # Ensure method has been cached - assert ( - client._transport.batch_process_documents - in client._transport._wrapped_methods - ) - - # Replace cached wrapped function with mock - mock_rpc = mock.Mock() - mock_rpc.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client._transport._wrapped_methods[ - client._transport.batch_process_documents - ] = mock_rpc - request = {} - client.batch_process_documents(request) - - # Establish that the underlying gRPC stub method was called. - assert mock_rpc.call_count == 1 - - # Operation methods call wrapper_fn to build a cached - # client._transport.operations_client instance on first rpc call. - # Subsequent calls should use the cached wrapper - wrapper_fn.reset_mock() - - client.batch_process_documents(request) - - # Establish that a new wrapper was not created for this call - assert wrapper_fn.call_count == 0 - assert mock_rpc.call_count == 2 - - -@pytest.mark.asyncio -async def test_batch_process_documents_empty_call_async(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.batch_process_documents), "__call__" - ) as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - operations_pb2.Operation(name="operations/spam") - ) - response = await client.batch_process_documents() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == document_understanding.BatchProcessDocumentsRequest() - - -@pytest.mark.asyncio -async def test_batch_process_documents_async_use_cached_wrapped_rpc( - transport: str = "grpc_asyncio", -): - # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, - # instead of constructing them on each call - with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) - - # Should wrap all calls on client creation - assert wrapper_fn.call_count > 0 - wrapper_fn.reset_mock() - - # Ensure method has been cached - assert ( - client._client._transport.batch_process_documents - in client._client._transport._wrapped_methods - ) - - # Replace cached wrapped function with mock - mock_rpc = mock.AsyncMock() - mock_rpc.return_value = mock.Mock() - client._client._transport._wrapped_methods[ - client._client._transport.batch_process_documents - ] = mock_rpc - - request = {} - await client.batch_process_documents(request) - - # Establish that the underlying gRPC stub method was called. - assert mock_rpc.call_count == 1 - - # Operation methods call wrapper_fn to build a cached - # client._transport.operations_client instance on first rpc call. - # Subsequent calls should use the cached wrapper - wrapper_fn.reset_mock() - - await client.batch_process_documents(request) - - # Establish that a new wrapper was not created for this call - assert wrapper_fn.call_count == 0 - assert mock_rpc.call_count == 2 - - -@pytest.mark.asyncio -async def test_batch_process_documents_async( - transport: str = "grpc_asyncio", - request_type=document_understanding.BatchProcessDocumentsRequest, -): - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) - - # Everything is optional in proto3 as far as the runtime is concerned, - # and we are mocking out the actual API, so just send an empty request. - request = request_type() - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.batch_process_documents), "__call__" - ) as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - operations_pb2.Operation(name="operations/spam") - ) - response = await client.batch_process_documents(request) - - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) - _, args, _ = call.mock_calls[0] - request = document_understanding.BatchProcessDocumentsRequest() - assert args[0] == request - - # Establish that the response is the type that we expect. - assert isinstance(response, future.Future) - - -@pytest.mark.asyncio -async def test_batch_process_documents_async_from_dict(): - await test_batch_process_documents_async(request_type=dict) - - -def test_batch_process_documents_field_headers(): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - - # Any value that is part of the HTTP/1.1 URI should be sent as - # a field header. Set these to a non-empty value. - request = document_understanding.BatchProcessDocumentsRequest() - - request.parent = "parent_value" - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.batch_process_documents), "__call__" - ) as call: - call.return_value = operations_pb2.Operation(name="operations/op") - client.batch_process_documents(request) - - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the field header was sent. - _, _, kw = call.mock_calls[0] - assert ( - "x-goog-request-params", - "parent=parent_value", - ) in kw["metadata"] - - -@pytest.mark.asyncio -async def test_batch_process_documents_field_headers_async(): - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - - # Any value that is part of the HTTP/1.1 URI should be sent as - # a field header. Set these to a non-empty value. - request = document_understanding.BatchProcessDocumentsRequest() - - request.parent = "parent_value" - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.batch_process_documents), "__call__" - ) as call: - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - operations_pb2.Operation(name="operations/op") - ) - await client.batch_process_documents(request) - - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the field header was sent. - _, _, kw = call.mock_calls[0] - assert ( - "x-goog-request-params", - "parent=parent_value", - ) in kw["metadata"] - - -def test_batch_process_documents_flattened(): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.batch_process_documents), "__call__" - ) as call: - # Designate an appropriate return value for the call. - call.return_value = operations_pb2.Operation(name="operations/op") - # Call the method with a truthy value for each flattened field, - # using the keyword arguments to the method. - client.batch_process_documents( - requests=[ - document_understanding.ProcessDocumentRequest(parent="parent_value") - ], - ) - - # Establish that the underlying call was made with the expected - # request object values. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - arg = args[0].requests - mock_val = [ - document_understanding.ProcessDocumentRequest(parent="parent_value") - ] - assert arg == mock_val - - -def test_batch_process_documents_flattened_error(): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.batch_process_documents( - document_understanding.BatchProcessDocumentsRequest(), - requests=[ - document_understanding.ProcessDocumentRequest(parent="parent_value") - ], - ) - - -@pytest.mark.asyncio -async def test_batch_process_documents_flattened_async(): - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client.transport.batch_process_documents), "__call__" - ) as call: - # Designate an appropriate return value for the call. - call.return_value = operations_pb2.Operation(name="operations/op") - - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - operations_pb2.Operation(name="operations/spam") - ) - # Call the method with a truthy value for each flattened field, - # using the keyword arguments to the method. - response = await client.batch_process_documents( - requests=[ - document_understanding.ProcessDocumentRequest(parent="parent_value") - ], - ) - - # Establish that the underlying call was made with the expected - # request object values. - assert len(call.mock_calls) - _, args, _ = call.mock_calls[0] - arg = args[0].requests - mock_val = [ - document_understanding.ProcessDocumentRequest(parent="parent_value") - ] - assert arg == mock_val - - -@pytest.mark.asyncio -async def test_batch_process_documents_flattened_error_async(): - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - await client.batch_process_documents( - document_understanding.BatchProcessDocumentsRequest(), - requests=[ - document_understanding.ProcessDocumentRequest(parent="parent_value") - ], - ) - - -@pytest.mark.parametrize( - "request_type", - [ - document_understanding.ProcessDocumentRequest, - dict, - ], -) -def test_process_document(request_type, transport: str = "grpc"): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) - - # Everything is optional in proto3 as far as the runtime is concerned, - # and we are mocking out the actual API, so just send an empty request. - request = request_type() - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.process_document), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = document.Document( - mime_type="mime_type_value", - text="text_value", - uri="uri_value", - ) - response = client.process_document(request) - - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - request = document_understanding.ProcessDocumentRequest() - assert args[0] == request - - # Establish that the response is the type that we expect. - assert isinstance(response, document.Document) - assert response.mime_type == "mime_type_value" - assert response.text == "text_value" - - -def test_process_document_empty_call(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.process_document), "__call__") as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.process_document() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == document_understanding.ProcessDocumentRequest() - - -def test_process_document_non_empty_request_with_auto_populated_field(): - # This test is a coverage failsafe to make sure that UUID4 fields are - # automatically populated, according to AIP-4235, with non-empty requests. - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Populate all string fields in the request which are not UUID4 - # since we want to check that UUID4 are populated automatically - # if they meet the requirements of AIP 4235. - request = document_understanding.ProcessDocumentRequest( - parent="parent_value", - document_type="document_type_value", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.process_document), "__call__") as call: - call.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client.process_document(request=request) - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == document_understanding.ProcessDocumentRequest( - parent="parent_value", - document_type="document_type_value", - ) - - -def test_process_document_use_cached_wrapped_rpc(): - # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, - # instead of constructing them on each call - with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - - # Should wrap all calls on client creation - assert wrapper_fn.call_count > 0 - wrapper_fn.reset_mock() - - # Ensure method has been cached - assert client._transport.process_document in client._transport._wrapped_methods - - # Replace cached wrapped function with mock - mock_rpc = mock.Mock() - mock_rpc.return_value.name = ( - "foo" # operation_request.operation in compute client(s) expect a string. - ) - client._transport._wrapped_methods[ - client._transport.process_document - ] = mock_rpc - request = {} - client.process_document(request) - - # Establish that the underlying gRPC stub method was called. - assert mock_rpc.call_count == 1 - - client.process_document(request) - - # Establish that a new wrapper was not created for this call - assert wrapper_fn.call_count == 0 - assert mock_rpc.call_count == 2 - - -@pytest.mark.asyncio -async def test_process_document_empty_call_async(): - # This test is a coverage failsafe to make sure that totally empty calls, - # i.e. request == None and no flattened fields passed, work. - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.process_document), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - document.Document( - mime_type="mime_type_value", - text="text_value", - ) - ) - response = await client.process_document() - call.assert_called() - _, args, _ = call.mock_calls[0] - assert args[0] == document_understanding.ProcessDocumentRequest() - - -@pytest.mark.asyncio -async def test_process_document_async_use_cached_wrapped_rpc( - transport: str = "grpc_asyncio", -): - # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, - # instead of constructing them on each call - with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) - - # Should wrap all calls on client creation - assert wrapper_fn.call_count > 0 - wrapper_fn.reset_mock() - - # Ensure method has been cached - assert ( - client._client._transport.process_document - in client._client._transport._wrapped_methods - ) - - # Replace cached wrapped function with mock - mock_rpc = mock.AsyncMock() - mock_rpc.return_value = mock.Mock() - client._client._transport._wrapped_methods[ - client._client._transport.process_document - ] = mock_rpc - - request = {} - await client.process_document(request) - - # Establish that the underlying gRPC stub method was called. - assert mock_rpc.call_count == 1 - - await client.process_document(request) - - # Establish that a new wrapper was not created for this call - assert wrapper_fn.call_count == 0 - assert mock_rpc.call_count == 2 - - -@pytest.mark.asyncio -async def test_process_document_async( - transport: str = "grpc_asyncio", - request_type=document_understanding.ProcessDocumentRequest, -): - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) - - # Everything is optional in proto3 as far as the runtime is concerned, - # and we are mocking out the actual API, so just send an empty request. - request = request_type() - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.process_document), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - document.Document( - mime_type="mime_type_value", - text="text_value", - ) - ) - response = await client.process_document(request) - - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) - _, args, _ = call.mock_calls[0] - request = document_understanding.ProcessDocumentRequest() - assert args[0] == request - - # Establish that the response is the type that we expect. - assert isinstance(response, document.Document) - assert response.mime_type == "mime_type_value" - assert response.text == "text_value" - - -@pytest.mark.asyncio -async def test_process_document_async_from_dict(): - await test_process_document_async(request_type=dict) - - -def test_process_document_field_headers(): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - - # Any value that is part of the HTTP/1.1 URI should be sent as - # a field header. Set these to a non-empty value. - request = document_understanding.ProcessDocumentRequest() - - request.parent = "parent_value" - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.process_document), "__call__") as call: - call.return_value = document.Document() - client.process_document(request) - - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the field header was sent. - _, _, kw = call.mock_calls[0] - assert ( - "x-goog-request-params", - "parent=parent_value", - ) in kw["metadata"] - - -@pytest.mark.asyncio -async def test_process_document_field_headers_async(): - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - - # Any value that is part of the HTTP/1.1 URI should be sent as - # a field header. Set these to a non-empty value. - request = document_understanding.ProcessDocumentRequest() - - request.parent = "parent_value" - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.process_document), "__call__") as call: - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(document.Document()) - await client.process_document(request) - - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the field header was sent. - _, _, kw = call.mock_calls[0] - assert ( - "x-goog-request-params", - "parent=parent_value", - ) in kw["metadata"] - - -def test_credentials_transport_error(): - # It is an error to provide credentials and a transport instance. - transport = transports.DocumentUnderstandingServiceGrpcTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - with pytest.raises(ValueError): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) - - # It is an error to provide a credentials file and a transport instance. - transport = transports.DocumentUnderstandingServiceGrpcTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - with pytest.raises(ValueError): - client = DocumentUnderstandingServiceClient( - client_options={"credentials_file": "credentials.json"}, - transport=transport, - ) - - # It is an error to provide an api_key and a transport instance. - transport = transports.DocumentUnderstandingServiceGrpcTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = DocumentUnderstandingServiceClient( - client_options=options, - transport=transport, - ) - - # It is an error to provide an api_key and a credential. - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = DocumentUnderstandingServiceClient( - client_options=options, credentials=ga_credentials.AnonymousCredentials() - ) - - # It is an error to provide scopes and a transport instance. - transport = transports.DocumentUnderstandingServiceGrpcTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - with pytest.raises(ValueError): - client = DocumentUnderstandingServiceClient( - client_options={"scopes": ["1", "2"]}, - transport=transport, - ) - - -def test_transport_instance(): - # A client may be instantiated with a custom transport instance. - transport = transports.DocumentUnderstandingServiceGrpcTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - client = DocumentUnderstandingServiceClient(transport=transport) - assert client.transport is transport - - -def test_transport_get_channel(): - # A client may be instantiated with a custom transport instance. - transport = transports.DocumentUnderstandingServiceGrpcTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - channel = transport.grpc_channel - assert channel - - transport = transports.DocumentUnderstandingServiceGrpcAsyncIOTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - channel = transport.grpc_channel - assert channel - - -@pytest.mark.parametrize( - "transport_class", - [ - transports.DocumentUnderstandingServiceGrpcTransport, - transports.DocumentUnderstandingServiceGrpcAsyncIOTransport, - ], -) -def test_transport_adc(transport_class): - # Test default credentials are used if not provided. - with mock.patch.object(google.auth, "default") as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport_class() - adc.assert_called_once() - - -@pytest.mark.parametrize( - "transport_name", - [ - "grpc", - ], -) -def test_transport_kind(transport_name): - transport = DocumentUnderstandingServiceClient.get_transport_class(transport_name)( - credentials=ga_credentials.AnonymousCredentials(), - ) - assert transport.kind == transport_name - - -def test_transport_grpc_default(): - # A client should use the gRPC transport by default. - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - assert isinstance( - client.transport, - transports.DocumentUnderstandingServiceGrpcTransport, - ) - - -def test_document_understanding_service_base_transport_error(): - # Passing both a credentials object and credentials_file should raise an error - with pytest.raises(core_exceptions.DuplicateCredentialArgs): - transport = transports.DocumentUnderstandingServiceTransport( - credentials=ga_credentials.AnonymousCredentials(), - credentials_file="credentials.json", - ) - - -def test_document_understanding_service_base_transport(): - # Instantiate the base transport. - with mock.patch( - "google.cloud.documentai_v1beta2.services.document_understanding_service.transports.DocumentUnderstandingServiceTransport.__init__" - ) as Transport: - Transport.return_value = None - transport = transports.DocumentUnderstandingServiceTransport( - credentials=ga_credentials.AnonymousCredentials(), - ) - - # Every method on the transport should just blindly - # raise NotImplementedError. - methods = ( - "batch_process_documents", - "process_document", - "get_location", - "list_locations", - "get_operation", - "cancel_operation", - "list_operations", - ) - for method in methods: - with pytest.raises(NotImplementedError): - getattr(transport, method)(request=object()) - - with pytest.raises(NotImplementedError): - transport.close() - - # Additionally, the LRO client (a property) should - # also raise NotImplementedError - with pytest.raises(NotImplementedError): - transport.operations_client - - # Catch all for all remaining methods and properties - remainder = [ - "kind", - ] - for r in remainder: - with pytest.raises(NotImplementedError): - getattr(transport, r)() - - -def test_document_understanding_service_base_transport_with_credentials_file(): - # Instantiate the base transport with a credentials file - with mock.patch.object( - google.auth, "load_credentials_from_file", autospec=True - ) as load_creds, mock.patch( - "google.cloud.documentai_v1beta2.services.document_understanding_service.transports.DocumentUnderstandingServiceTransport._prep_wrapped_messages" - ) as Transport: - Transport.return_value = None - load_creds.return_value = (ga_credentials.AnonymousCredentials(), None) - transport = transports.DocumentUnderstandingServiceTransport( - credentials_file="credentials.json", - quota_project_id="octopus", - ) - load_creds.assert_called_once_with( - "credentials.json", - scopes=None, - default_scopes=("https://www.googleapis.com/auth/cloud-platform",), - quota_project_id="octopus", - ) - - -def test_document_understanding_service_base_transport_with_adc(): - # Test the default credentials are used if credentials and credentials_file are None. - with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch( - "google.cloud.documentai_v1beta2.services.document_understanding_service.transports.DocumentUnderstandingServiceTransport._prep_wrapped_messages" - ) as Transport: - Transport.return_value = None - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport = transports.DocumentUnderstandingServiceTransport() - adc.assert_called_once() - - -def test_document_understanding_service_auth_adc(): - # If no credentials are provided, we should use ADC credentials. - with mock.patch.object(google.auth, "default", autospec=True) as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - DocumentUnderstandingServiceClient() - adc.assert_called_once_with( - scopes=None, - default_scopes=("https://www.googleapis.com/auth/cloud-platform",), - quota_project_id=None, - ) - - -@pytest.mark.parametrize( - "transport_class", - [ - transports.DocumentUnderstandingServiceGrpcTransport, - transports.DocumentUnderstandingServiceGrpcAsyncIOTransport, - ], -) -def test_document_understanding_service_transport_auth_adc(transport_class): - # If credentials and host are not provided, the transport class should use - # ADC credentials. - with mock.patch.object(google.auth, "default", autospec=True) as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport_class(quota_project_id="octopus", scopes=["1", "2"]) - adc.assert_called_once_with( - scopes=["1", "2"], - default_scopes=("https://www.googleapis.com/auth/cloud-platform",), - quota_project_id="octopus", - ) - - -@pytest.mark.parametrize( - "transport_class", - [ - transports.DocumentUnderstandingServiceGrpcTransport, - transports.DocumentUnderstandingServiceGrpcAsyncIOTransport, - ], -) -def test_document_understanding_service_transport_auth_gdch_credentials( - transport_class, -): - host = "https://language.com" - api_audience_tests = [None, "https://language2.com"] - api_audience_expect = [host, "https://language2.com"] - for t, e in zip(api_audience_tests, api_audience_expect): - with mock.patch.object(google.auth, "default", autospec=True) as adc: - gdch_mock = mock.MagicMock() - type(gdch_mock).with_gdch_audience = mock.PropertyMock( - return_value=gdch_mock - ) - adc.return_value = (gdch_mock, None) - transport_class(host=host, api_audience=t) - gdch_mock.with_gdch_audience.assert_called_once_with(e) - - -@pytest.mark.parametrize( - "transport_class,grpc_helpers", - [ - (transports.DocumentUnderstandingServiceGrpcTransport, grpc_helpers), - ( - transports.DocumentUnderstandingServiceGrpcAsyncIOTransport, - grpc_helpers_async, - ), - ], -) -def test_document_understanding_service_transport_create_channel( - transport_class, grpc_helpers -): - # If credentials and host are not provided, the transport class should use - # ADC credentials. - with mock.patch.object( - google.auth, "default", autospec=True - ) as adc, mock.patch.object( - grpc_helpers, "create_channel", autospec=True - ) as create_channel: - creds = ga_credentials.AnonymousCredentials() - adc.return_value = (creds, None) - transport_class(quota_project_id="octopus", scopes=["1", "2"]) - - create_channel.assert_called_with( - "documentai.googleapis.com:443", - credentials=creds, - credentials_file=None, - quota_project_id="octopus", - default_scopes=("https://www.googleapis.com/auth/cloud-platform",), - scopes=["1", "2"], - default_host="documentai.googleapis.com", - ssl_credentials=None, - options=[ - ("grpc.max_send_message_length", -1), - ("grpc.max_receive_message_length", -1), - ], - ) - - -@pytest.mark.parametrize( - "transport_class", - [ - transports.DocumentUnderstandingServiceGrpcTransport, - transports.DocumentUnderstandingServiceGrpcAsyncIOTransport, - ], -) -def test_document_understanding_service_grpc_transport_client_cert_source_for_mtls( - transport_class, -): - cred = ga_credentials.AnonymousCredentials() - - # Check ssl_channel_credentials is used if provided. - with mock.patch.object(transport_class, "create_channel") as mock_create_channel: - mock_ssl_channel_creds = mock.Mock() - transport_class( - host="squid.clam.whelk", - credentials=cred, - ssl_channel_credentials=mock_ssl_channel_creds, - ) - mock_create_channel.assert_called_once_with( - "squid.clam.whelk:443", - credentials=cred, - credentials_file=None, - scopes=None, - ssl_credentials=mock_ssl_channel_creds, - quota_project_id=None, - options=[ - ("grpc.max_send_message_length", -1), - ("grpc.max_receive_message_length", -1), - ], - ) - - # Check if ssl_channel_credentials is not provided, then client_cert_source_for_mtls - # is used. - with mock.patch.object(transport_class, "create_channel", return_value=mock.Mock()): - with mock.patch("grpc.ssl_channel_credentials") as mock_ssl_cred: - transport_class( - credentials=cred, - client_cert_source_for_mtls=client_cert_source_callback, - ) - expected_cert, expected_key = client_cert_source_callback() - mock_ssl_cred.assert_called_once_with( - certificate_chain=expected_cert, private_key=expected_key - ) - - -@pytest.mark.parametrize( - "transport_name", - [ - "grpc", - "grpc_asyncio", - ], -) -def test_document_understanding_service_host_no_port(transport_name): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - client_options=client_options.ClientOptions( - api_endpoint="documentai.googleapis.com" - ), - transport=transport_name, - ) - assert client.transport._host == ("documentai.googleapis.com:443") - - -@pytest.mark.parametrize( - "transport_name", - [ - "grpc", - "grpc_asyncio", - ], -) -def test_document_understanding_service_host_with_port(transport_name): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - client_options=client_options.ClientOptions( - api_endpoint="documentai.googleapis.com:8000" - ), - transport=transport_name, - ) - assert client.transport._host == ("documentai.googleapis.com:8000") - - -def test_document_understanding_service_grpc_transport_channel(): - channel = grpc.secure_channel("http://localhost/", grpc.local_channel_credentials()) - - # Check that channel is used if provided. - transport = transports.DocumentUnderstandingServiceGrpcTransport( - host="squid.clam.whelk", - channel=channel, - ) - assert transport.grpc_channel == channel - assert transport._host == "squid.clam.whelk:443" - assert transport._ssl_channel_credentials == None - - -def test_document_understanding_service_grpc_asyncio_transport_channel(): - channel = aio.secure_channel("http://localhost/", grpc.local_channel_credentials()) - - # Check that channel is used if provided. - transport = transports.DocumentUnderstandingServiceGrpcAsyncIOTransport( - host="squid.clam.whelk", - channel=channel, - ) - assert transport.grpc_channel == channel - assert transport._host == "squid.clam.whelk:443" - assert transport._ssl_channel_credentials == None - - -# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are -# removed from grpc/grpc_asyncio transport constructor. -@pytest.mark.parametrize( - "transport_class", - [ - transports.DocumentUnderstandingServiceGrpcTransport, - transports.DocumentUnderstandingServiceGrpcAsyncIOTransport, - ], -) -def test_document_understanding_service_transport_channel_mtls_with_client_cert_source( - transport_class, -): - with mock.patch( - "grpc.ssl_channel_credentials", autospec=True - ) as grpc_ssl_channel_cred: - with mock.patch.object( - transport_class, "create_channel" - ) as grpc_create_channel: - mock_ssl_cred = mock.Mock() - grpc_ssl_channel_cred.return_value = mock_ssl_cred - - mock_grpc_channel = mock.Mock() - grpc_create_channel.return_value = mock_grpc_channel - - cred = ga_credentials.AnonymousCredentials() - with pytest.warns(DeprecationWarning): - with mock.patch.object(google.auth, "default") as adc: - adc.return_value = (cred, None) - transport = transport_class( - host="squid.clam.whelk", - api_mtls_endpoint="mtls.squid.clam.whelk", - client_cert_source=client_cert_source_callback, - ) - adc.assert_called_once() - - grpc_ssl_channel_cred.assert_called_once_with( - certificate_chain=b"cert bytes", private_key=b"key bytes" - ) - grpc_create_channel.assert_called_once_with( - "mtls.squid.clam.whelk:443", - credentials=cred, - credentials_file=None, - scopes=None, - ssl_credentials=mock_ssl_cred, - quota_project_id=None, - options=[ - ("grpc.max_send_message_length", -1), - ("grpc.max_receive_message_length", -1), - ], - ) - assert transport.grpc_channel == mock_grpc_channel - assert transport._ssl_channel_credentials == mock_ssl_cred - - -# Remove this test when deprecated arguments (api_mtls_endpoint, client_cert_source) are -# removed from grpc/grpc_asyncio transport constructor. -@pytest.mark.parametrize( - "transport_class", - [ - transports.DocumentUnderstandingServiceGrpcTransport, - transports.DocumentUnderstandingServiceGrpcAsyncIOTransport, - ], -) -def test_document_understanding_service_transport_channel_mtls_with_adc( - transport_class, -): - mock_ssl_cred = mock.Mock() - with mock.patch.multiple( - "google.auth.transport.grpc.SslCredentials", - __init__=mock.Mock(return_value=None), - ssl_credentials=mock.PropertyMock(return_value=mock_ssl_cred), - ): - with mock.patch.object( - transport_class, "create_channel" - ) as grpc_create_channel: - mock_grpc_channel = mock.Mock() - grpc_create_channel.return_value = mock_grpc_channel - mock_cred = mock.Mock() - - with pytest.warns(DeprecationWarning): - transport = transport_class( - host="squid.clam.whelk", - credentials=mock_cred, - api_mtls_endpoint="mtls.squid.clam.whelk", - client_cert_source=None, - ) - - grpc_create_channel.assert_called_once_with( - "mtls.squid.clam.whelk:443", - credentials=mock_cred, - credentials_file=None, - scopes=None, - ssl_credentials=mock_ssl_cred, - quota_project_id=None, - options=[ - ("grpc.max_send_message_length", -1), - ("grpc.max_receive_message_length", -1), - ], - ) - assert transport.grpc_channel == mock_grpc_channel - - -def test_document_understanding_service_grpc_lro_client(): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc", - ) - transport = client.transport - - # Ensure that we have a api-core operations client. - assert isinstance( - transport.operations_client, - operations_v1.OperationsClient, - ) - - # Ensure that subsequent calls to the property send the exact same object. - assert transport.operations_client is transport.operations_client - - -def test_document_understanding_service_grpc_lro_async_client(): - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - transport = client.transport - - # Ensure that we have a api-core operations client. - assert isinstance( - transport.operations_client, - operations_v1.OperationsAsyncClient, - ) - - # Ensure that subsequent calls to the property send the exact same object. - assert transport.operations_client is transport.operations_client - - -def test_common_billing_account_path(): - billing_account = "squid" - expected = "billingAccounts/{billing_account}".format( - billing_account=billing_account, - ) - actual = DocumentUnderstandingServiceClient.common_billing_account_path( - billing_account - ) - assert expected == actual - - -def test_parse_common_billing_account_path(): - expected = { - "billing_account": "clam", - } - path = DocumentUnderstandingServiceClient.common_billing_account_path(**expected) - - # Check that the path construction is reversible. - actual = DocumentUnderstandingServiceClient.parse_common_billing_account_path(path) - assert expected == actual - - -def test_common_folder_path(): - folder = "whelk" - expected = "folders/{folder}".format( - folder=folder, - ) - actual = DocumentUnderstandingServiceClient.common_folder_path(folder) - assert expected == actual - - -def test_parse_common_folder_path(): - expected = { - "folder": "octopus", - } - path = DocumentUnderstandingServiceClient.common_folder_path(**expected) - - # Check that the path construction is reversible. - actual = DocumentUnderstandingServiceClient.parse_common_folder_path(path) - assert expected == actual - - -def test_common_organization_path(): - organization = "oyster" - expected = "organizations/{organization}".format( - organization=organization, - ) - actual = DocumentUnderstandingServiceClient.common_organization_path(organization) - assert expected == actual - - -def test_parse_common_organization_path(): - expected = { - "organization": "nudibranch", - } - path = DocumentUnderstandingServiceClient.common_organization_path(**expected) - - # Check that the path construction is reversible. - actual = DocumentUnderstandingServiceClient.parse_common_organization_path(path) - assert expected == actual - - -def test_common_project_path(): - project = "cuttlefish" - expected = "projects/{project}".format( - project=project, - ) - actual = DocumentUnderstandingServiceClient.common_project_path(project) - assert expected == actual - - -def test_parse_common_project_path(): - expected = { - "project": "mussel", - } - path = DocumentUnderstandingServiceClient.common_project_path(**expected) - - # Check that the path construction is reversible. - actual = DocumentUnderstandingServiceClient.parse_common_project_path(path) - assert expected == actual - - -def test_common_location_path(): - project = "winkle" - location = "nautilus" - expected = "projects/{project}/locations/{location}".format( - project=project, - location=location, - ) - actual = DocumentUnderstandingServiceClient.common_location_path(project, location) - assert expected == actual - - -def test_parse_common_location_path(): - expected = { - "project": "scallop", - "location": "abalone", - } - path = DocumentUnderstandingServiceClient.common_location_path(**expected) - - # Check that the path construction is reversible. - actual = DocumentUnderstandingServiceClient.parse_common_location_path(path) - assert expected == actual - - -def test_client_with_default_client_info(): - client_info = gapic_v1.client_info.ClientInfo() - - with mock.patch.object( - transports.DocumentUnderstandingServiceTransport, "_prep_wrapped_messages" - ) as prep: - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - client_info=client_info, - ) - prep.assert_called_once_with(client_info) - - with mock.patch.object( - transports.DocumentUnderstandingServiceTransport, "_prep_wrapped_messages" - ) as prep: - transport_class = DocumentUnderstandingServiceClient.get_transport_class() - transport = transport_class( - credentials=ga_credentials.AnonymousCredentials(), - client_info=client_info, - ) - prep.assert_called_once_with(client_info) - - -@pytest.mark.asyncio -async def test_transport_close_async(): - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="grpc_asyncio", - ) - with mock.patch.object( - type(getattr(client.transport, "grpc_channel")), "close" - ) as close: - async with client: - close.assert_not_called() - close.assert_called_once() - - -def test_cancel_operation(transport: str = "grpc"): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) - - # Everything is optional in proto3 as far as the runtime is concerned, - # and we are mocking out the actual API, so just send an empty request. - request = operations_pb2.CancelOperationRequest() - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = None - response = client.cancel_operation(request) - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the response is the type that we expect. - assert response is None - - -@pytest.mark.asyncio -async def test_cancel_operation_async(transport: str = "grpc_asyncio"): - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) - - # Everything is optional in proto3 as far as the runtime is concerned, - # and we are mocking out the actual API, so just send an empty request. - request = operations_pb2.CancelOperationRequest() - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) - response = await client.cancel_operation(request) - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the response is the type that we expect. - assert response is None - - -def test_cancel_operation_field_headers(): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - - # Any value that is part of the HTTP/1.1 URI should be sent as - # a field header. Set these to a non-empty value. - request = operations_pb2.CancelOperationRequest() - request.name = "locations" - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: - call.return_value = None - - client.cancel_operation(request) - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the field header was sent. - _, _, kw = call.mock_calls[0] - assert ( - "x-goog-request-params", - "name=locations", - ) in kw["metadata"] - - -@pytest.mark.asyncio -async def test_cancel_operation_field_headers_async(): - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - - # Any value that is part of the HTTP/1.1 URI should be sent as - # a field header. Set these to a non-empty value. - request = operations_pb2.CancelOperationRequest() - request.name = "locations" - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) - await client.cancel_operation(request) - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the field header was sent. - _, _, kw = call.mock_calls[0] - assert ( - "x-goog-request-params", - "name=locations", - ) in kw["metadata"] - - -def test_cancel_operation_from_dict(): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = None - - response = client.cancel_operation( - request={ - "name": "locations", - } - ) - call.assert_called() - - -@pytest.mark.asyncio -async def test_cancel_operation_from_dict_async(): - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.cancel_operation), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) - response = await client.cancel_operation( - request={ - "name": "locations", - } - ) - call.assert_called() - - -def test_get_operation(transport: str = "grpc"): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) - - # Everything is optional in proto3 as far as the runtime is concerned, - # and we are mocking out the actual API, so just send an empty request. - request = operations_pb2.GetOperationRequest() - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.get_operation), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = operations_pb2.Operation() - response = client.get_operation(request) - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the response is the type that we expect. - assert isinstance(response, operations_pb2.Operation) - - -@pytest.mark.asyncio -async def test_get_operation_async(transport: str = "grpc_asyncio"): - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) - - # Everything is optional in proto3 as far as the runtime is concerned, - # and we are mocking out the actual API, so just send an empty request. - request = operations_pb2.GetOperationRequest() - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.get_operation), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - operations_pb2.Operation() - ) - response = await client.get_operation(request) - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the response is the type that we expect. - assert isinstance(response, operations_pb2.Operation) - - -def test_get_operation_field_headers(): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - - # Any value that is part of the HTTP/1.1 URI should be sent as - # a field header. Set these to a non-empty value. - request = operations_pb2.GetOperationRequest() - request.name = "locations" - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.get_operation), "__call__") as call: - call.return_value = operations_pb2.Operation() - - client.get_operation(request) - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the field header was sent. - _, _, kw = call.mock_calls[0] - assert ( - "x-goog-request-params", - "name=locations", - ) in kw["metadata"] - - -@pytest.mark.asyncio -async def test_get_operation_field_headers_async(): - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - - # Any value that is part of the HTTP/1.1 URI should be sent as - # a field header. Set these to a non-empty value. - request = operations_pb2.GetOperationRequest() - request.name = "locations" - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.get_operation), "__call__") as call: - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - operations_pb2.Operation() - ) - await client.get_operation(request) - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the field header was sent. - _, _, kw = call.mock_calls[0] - assert ( - "x-goog-request-params", - "name=locations", - ) in kw["metadata"] - - -def test_get_operation_from_dict(): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.get_operation), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = operations_pb2.Operation() - - response = client.get_operation( - request={ - "name": "locations", - } - ) - call.assert_called() - - -@pytest.mark.asyncio -async def test_get_operation_from_dict_async(): - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.get_operation), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - operations_pb2.Operation() - ) - response = await client.get_operation( - request={ - "name": "locations", - } - ) - call.assert_called() - - -def test_list_operations(transport: str = "grpc"): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) - - # Everything is optional in proto3 as far as the runtime is concerned, - # and we are mocking out the actual API, so just send an empty request. - request = operations_pb2.ListOperationsRequest() - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.list_operations), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = operations_pb2.ListOperationsResponse() - response = client.list_operations(request) - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the response is the type that we expect. - assert isinstance(response, operations_pb2.ListOperationsResponse) - - -@pytest.mark.asyncio -async def test_list_operations_async(transport: str = "grpc_asyncio"): - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) - - # Everything is optional in proto3 as far as the runtime is concerned, - # and we are mocking out the actual API, so just send an empty request. - request = operations_pb2.ListOperationsRequest() - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.list_operations), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - operations_pb2.ListOperationsResponse() - ) - response = await client.list_operations(request) - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the response is the type that we expect. - assert isinstance(response, operations_pb2.ListOperationsResponse) - - -def test_list_operations_field_headers(): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - - # Any value that is part of the HTTP/1.1 URI should be sent as - # a field header. Set these to a non-empty value. - request = operations_pb2.ListOperationsRequest() - request.name = "locations" - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.list_operations), "__call__") as call: - call.return_value = operations_pb2.ListOperationsResponse() - - client.list_operations(request) - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the field header was sent. - _, _, kw = call.mock_calls[0] - assert ( - "x-goog-request-params", - "name=locations", - ) in kw["metadata"] - - -@pytest.mark.asyncio -async def test_list_operations_field_headers_async(): - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - - # Any value that is part of the HTTP/1.1 URI should be sent as - # a field header. Set these to a non-empty value. - request = operations_pb2.ListOperationsRequest() - request.name = "locations" - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.list_operations), "__call__") as call: - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - operations_pb2.ListOperationsResponse() - ) - await client.list_operations(request) - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the field header was sent. - _, _, kw = call.mock_calls[0] - assert ( - "x-goog-request-params", - "name=locations", - ) in kw["metadata"] - - -def test_list_operations_from_dict(): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.list_operations), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = operations_pb2.ListOperationsResponse() - - response = client.list_operations( - request={ - "name": "locations", - } - ) - call.assert_called() - - -@pytest.mark.asyncio -async def test_list_operations_from_dict_async(): - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.list_operations), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - operations_pb2.ListOperationsResponse() - ) - response = await client.list_operations( - request={ - "name": "locations", - } - ) - call.assert_called() - - -def test_list_locations(transport: str = "grpc"): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) - - # Everything is optional in proto3 as far as the runtime is concerned, - # and we are mocking out the actual API, so just send an empty request. - request = locations_pb2.ListLocationsRequest() - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.list_locations), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = locations_pb2.ListLocationsResponse() - response = client.list_locations(request) - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the response is the type that we expect. - assert isinstance(response, locations_pb2.ListLocationsResponse) - - -@pytest.mark.asyncio -async def test_list_locations_async(transport: str = "grpc_asyncio"): - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) - - # Everything is optional in proto3 as far as the runtime is concerned, - # and we are mocking out the actual API, so just send an empty request. - request = locations_pb2.ListLocationsRequest() - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.list_locations), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - locations_pb2.ListLocationsResponse() - ) - response = await client.list_locations(request) - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the response is the type that we expect. - assert isinstance(response, locations_pb2.ListLocationsResponse) - - -def test_list_locations_field_headers(): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - - # Any value that is part of the HTTP/1.1 URI should be sent as - # a field header. Set these to a non-empty value. - request = locations_pb2.ListLocationsRequest() - request.name = "locations" - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.list_locations), "__call__") as call: - call.return_value = locations_pb2.ListLocationsResponse() - - client.list_locations(request) - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the field header was sent. - _, _, kw = call.mock_calls[0] - assert ( - "x-goog-request-params", - "name=locations", - ) in kw["metadata"] - - -@pytest.mark.asyncio -async def test_list_locations_field_headers_async(): - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - - # Any value that is part of the HTTP/1.1 URI should be sent as - # a field header. Set these to a non-empty value. - request = locations_pb2.ListLocationsRequest() - request.name = "locations" - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.list_locations), "__call__") as call: - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - locations_pb2.ListLocationsResponse() - ) - await client.list_locations(request) - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the field header was sent. - _, _, kw = call.mock_calls[0] - assert ( - "x-goog-request-params", - "name=locations", - ) in kw["metadata"] - - -def test_list_locations_from_dict(): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.list_locations), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = locations_pb2.ListLocationsResponse() - - response = client.list_locations( - request={ - "name": "locations", - } - ) - call.assert_called() - - -@pytest.mark.asyncio -async def test_list_locations_from_dict_async(): - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.list_locations), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - locations_pb2.ListLocationsResponse() - ) - response = await client.list_locations( - request={ - "name": "locations", - } - ) - call.assert_called() - - -def test_get_location(transport: str = "grpc"): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) - - # Everything is optional in proto3 as far as the runtime is concerned, - # and we are mocking out the actual API, so just send an empty request. - request = locations_pb2.GetLocationRequest() - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.get_location), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = locations_pb2.Location() - response = client.get_location(request) - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the response is the type that we expect. - assert isinstance(response, locations_pb2.Location) - - -@pytest.mark.asyncio -async def test_get_location_async(transport: str = "grpc_asyncio"): - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) - - # Everything is optional in proto3 as far as the runtime is concerned, - # and we are mocking out the actual API, so just send an empty request. - request = locations_pb2.GetLocationRequest() - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.get_location), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - locations_pb2.Location() - ) - response = await client.get_location(request) - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the response is the type that we expect. - assert isinstance(response, locations_pb2.Location) - - -def test_get_location_field_headers(): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials() - ) - - # Any value that is part of the HTTP/1.1 URI should be sent as - # a field header. Set these to a non-empty value. - request = locations_pb2.GetLocationRequest() - request.name = "locations/abc" - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.get_location), "__call__") as call: - call.return_value = locations_pb2.Location() - - client.get_location(request) - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the field header was sent. - _, _, kw = call.mock_calls[0] - assert ( - "x-goog-request-params", - "name=locations/abc", - ) in kw["metadata"] - - -@pytest.mark.asyncio -async def test_get_location_field_headers_async(): - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials() - ) - - # Any value that is part of the HTTP/1.1 URI should be sent as - # a field header. Set these to a non-empty value. - request = locations_pb2.GetLocationRequest() - request.name = "locations/abc" - - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.get_location), "__call__") as call: - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - locations_pb2.Location() - ) - await client.get_location(request) - # Establish that the underlying gRPC stub method was called. - assert len(call.mock_calls) == 1 - _, args, _ = call.mock_calls[0] - assert args[0] == request - - # Establish that the field header was sent. - _, _, kw = call.mock_calls[0] - assert ( - "x-goog-request-params", - "name=locations/abc", - ) in kw["metadata"] - - -def test_get_location_from_dict(): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.list_locations), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = locations_pb2.Location() - - response = client.get_location( - request={ - "name": "locations/abc", - } - ) - call.assert_called() - - -@pytest.mark.asyncio -async def test_get_location_from_dict_async(): - client = DocumentUnderstandingServiceAsyncClient( - credentials=ga_credentials.AnonymousCredentials(), - ) - # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client.transport.list_locations), "__call__") as call: - # Designate an appropriate return value for the call. - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - locations_pb2.Location() - ) - response = await client.get_location( - request={ - "name": "locations", - } - ) - call.assert_called() - - -def test_transport_close(): - transports = { - "grpc": "_grpc_channel", - } - - for transport, close_name in transports.items(): - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport=transport - ) - with mock.patch.object( - type(getattr(client.transport, close_name)), "close" - ) as close: - with client: - close.assert_not_called() - close.assert_called_once() - - -def test_client_ctx(): - transports = [ - "grpc", - ] - for transport in transports: - client = DocumentUnderstandingServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport=transport - ) - # Test client calls underlying transport. - with mock.patch.object(type(client.transport), "close") as close: - close.assert_not_called() - with client: - pass - close.assert_called() - - -@pytest.mark.parametrize( - "client_class,transport_class", - [ - ( - DocumentUnderstandingServiceClient, - transports.DocumentUnderstandingServiceGrpcTransport, - ), - ( - DocumentUnderstandingServiceAsyncClient, - transports.DocumentUnderstandingServiceGrpcAsyncIOTransport, - ), - ], -) -def test_api_key_credentials(client_class, transport_class): - with mock.patch.object( - google.auth._default, "get_api_key_credentials", create=True - ) as get_api_key_credentials: - mock_cred = mock.Mock() - get_api_key_credentials.return_value = mock_cred - options = client_options.ClientOptions() - options.api_key = "api_key" - with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class(client_options=options) - patched.assert_called_once_with( - credentials=mock_cred, - credentials_file=None, - host=client._DEFAULT_ENDPOINT_TEMPLATE.format( - UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE - ), - scopes=None, - client_cert_source_for_mtls=None, - quota_project_id=None, - client_info=transports.base.DEFAULT_CLIENT_INFO, - always_use_jwt_access=True, - api_audience=None, - )