diff --git a/docs/documentai_v1beta3/document_processor_service.rst b/docs/documentai_v1beta3/document_processor_service.rst index 6a2caa2b..4d8d5f9e 100644 --- a/docs/documentai_v1beta3/document_processor_service.rst +++ b/docs/documentai_v1beta3/document_processor_service.rst @@ -4,3 +4,7 @@ DocumentProcessorService .. automodule:: google.cloud.documentai_v1beta3.services.document_processor_service :members: :inherited-members: + +.. automodule:: google.cloud.documentai_v1beta3.services.document_processor_service.pagers + :members: + :inherited-members: diff --git a/google/cloud/documentai_v1beta3/__init__.py b/google/cloud/documentai_v1beta3/__init__.py index 5b1d91b3..9fcd12ae 100644 --- a/google/cloud/documentai_v1beta3/__init__.py +++ b/google/cloud/documentai_v1beta3/__init__.py @@ -27,7 +27,20 @@ from .types.document_processor_service import BatchProcessMetadata from .types.document_processor_service import BatchProcessRequest from .types.document_processor_service import BatchProcessResponse +from .types.document_processor_service import CreateProcessorRequest +from .types.document_processor_service import DeleteProcessorMetadata +from .types.document_processor_service import DeleteProcessorRequest +from .types.document_processor_service import DisableProcessorMetadata +from .types.document_processor_service import DisableProcessorRequest +from .types.document_processor_service import DisableProcessorResponse +from .types.document_processor_service import EnableProcessorMetadata +from .types.document_processor_service import EnableProcessorRequest +from .types.document_processor_service import EnableProcessorResponse +from .types.document_processor_service import FetchProcessorTypesRequest +from .types.document_processor_service import FetchProcessorTypesResponse from .types.document_processor_service import HumanReviewStatus +from .types.document_processor_service import ListProcessorsRequest +from .types.document_processor_service import ListProcessorsResponse from .types.document_processor_service import ProcessRequest from .types.document_processor_service import ProcessResponse from .types.document_processor_service import ReviewDocumentOperationMetadata @@ -37,6 +50,8 @@ from .types.geometry import NormalizedVertex from .types.geometry import Vertex from .types.operation_metadata import CommonOperationMetadata +from .types.processor import Processor +from .types.processor_type import ProcessorType __all__ = ( "DocumentProcessorServiceAsyncClient", @@ -46,16 +61,31 @@ "BatchProcessResponse", "BoundingPoly", "CommonOperationMetadata", + "CreateProcessorRequest", + "DeleteProcessorMetadata", + "DeleteProcessorRequest", + "DisableProcessorMetadata", + "DisableProcessorRequest", + "DisableProcessorResponse", "Document", "DocumentOutputConfig", "DocumentProcessorServiceClient", + "EnableProcessorMetadata", + "EnableProcessorRequest", + "EnableProcessorResponse", + "FetchProcessorTypesRequest", + "FetchProcessorTypesResponse", "GcsDocument", "GcsDocuments", "GcsPrefix", "HumanReviewStatus", + "ListProcessorsRequest", + "ListProcessorsResponse", "NormalizedVertex", "ProcessRequest", "ProcessResponse", + "Processor", + "ProcessorType", "RawDocument", "ReviewDocumentOperationMetadata", "ReviewDocumentRequest", diff --git a/google/cloud/documentai_v1beta3/gapic_metadata.json b/google/cloud/documentai_v1beta3/gapic_metadata.json index 61a5b92c..1e2c3550 100644 --- a/google/cloud/documentai_v1beta3/gapic_metadata.json +++ b/google/cloud/documentai_v1beta3/gapic_metadata.json @@ -15,6 +15,36 @@ "batch_process_documents" ] }, + "CreateProcessor": { + "methods": [ + "create_processor" + ] + }, + "DeleteProcessor": { + "methods": [ + "delete_processor" + ] + }, + "DisableProcessor": { + "methods": [ + "disable_processor" + ] + }, + "EnableProcessor": { + "methods": [ + "enable_processor" + ] + }, + "FetchProcessorTypes": { + "methods": [ + "fetch_processor_types" + ] + }, + "ListProcessors": { + "methods": [ + "list_processors" + ] + }, "ProcessDocument": { "methods": [ "process_document" @@ -35,6 +65,36 @@ "batch_process_documents" ] }, + "CreateProcessor": { + "methods": [ + "create_processor" + ] + }, + "DeleteProcessor": { + "methods": [ + "delete_processor" + ] + }, + "DisableProcessor": { + "methods": [ + "disable_processor" + ] + }, + "EnableProcessor": { + "methods": [ + "enable_processor" + ] + }, + "FetchProcessorTypes": { + "methods": [ + "fetch_processor_types" + ] + }, + "ListProcessors": { + "methods": [ + "list_processors" + ] + }, "ProcessDocument": { "methods": [ "process_document" diff --git a/google/cloud/documentai_v1beta3/services/document_processor_service/async_client.py b/google/cloud/documentai_v1beta3/services/document_processor_service/async_client.py index ac20ff44..4e190a5a 100644 --- a/google/cloud/documentai_v1beta3/services/document_processor_service/async_client.py +++ b/google/cloud/documentai_v1beta3/services/document_processor_service/async_client.py @@ -28,8 +28,14 @@ from google.api_core import operation # type: ignore from google.api_core import operation_async # type: ignore +from google.cloud.documentai_v1beta3.services.document_processor_service import pagers from google.cloud.documentai_v1beta3.types import document from google.cloud.documentai_v1beta3.types import document_processor_service +from google.cloud.documentai_v1beta3.types import processor +from google.cloud.documentai_v1beta3.types import processor as gcd_processor +from google.cloud.documentai_v1beta3.types import processor_type +from google.protobuf import empty_pb2 # type: ignore +from google.protobuf import timestamp_pb2 # type: ignore from .transports.base import DocumentProcessorServiceTransport, DEFAULT_CLIENT_INFO from .transports.grpc_asyncio import DocumentProcessorServiceGrpcAsyncIOTransport from .client import DocumentProcessorServiceClient @@ -58,6 +64,12 @@ class DocumentProcessorServiceAsyncClient: parse_processor_path = staticmethod( DocumentProcessorServiceClient.parse_processor_path ) + processor_type_path = staticmethod( + DocumentProcessorServiceClient.processor_type_path + ) + parse_processor_type_path = staticmethod( + DocumentProcessorServiceClient.parse_processor_type_path + ) common_billing_account_path = staticmethod( DocumentProcessorServiceClient.common_billing_account_path ) @@ -355,6 +367,463 @@ async def batch_process_documents( # Done; return the response. return response + async def fetch_processor_types( + self, + request: document_processor_service.FetchProcessorTypesRequest = None, + *, + parent: str = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> document_processor_service.FetchProcessorTypesResponse: + r"""Fetches processor types. + + Args: + request (:class:`google.cloud.documentai_v1beta3.types.FetchProcessorTypesRequest`): + The request object. Request message for fetch processor + types. + parent (:class:`str`): + Required. The project of processor + type to list. Format: + projects/{project}/locations/{location} + + This corresponds to the ``parent`` 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.cloud.documentai_v1beta3.types.FetchProcessorTypesResponse: + Response message for fetch processor + types. + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([parent]) + 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." + ) + + request = document_processor_service.FetchProcessorTypesRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if parent is not None: + request.parent = parent + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = gapic_v1.method_async.wrap_method( + self._client._transport.fetch_processor_types, + 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((("parent", request.parent),)), + ) + + # Send the request. + response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Done; return the response. + return response + + async def list_processors( + self, + request: document_processor_service.ListProcessorsRequest = None, + *, + parent: str = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> pagers.ListProcessorsAsyncPager: + r"""Lists all processors which belong to this project. + + Args: + request (:class:`google.cloud.documentai_v1beta3.types.ListProcessorsRequest`): + The request object. Request message for list all + processors belongs to a project. + parent (:class:`str`): + Required. The parent (project and + location) which owns this collection of + Processors. Format: + projects/{project}/locations/{location} + + This corresponds to the ``parent`` 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.cloud.documentai_v1beta3.services.document_processor_service.pagers.ListProcessorsAsyncPager: + Response message for list processors. + Iterating over this object will yield + results and resolve additional pages + automatically. + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([parent]) + 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." + ) + + request = document_processor_service.ListProcessorsRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if parent is not None: + request.parent = parent + + # 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_processors, + 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((("parent", request.parent),)), + ) + + # Send the request. + response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # This method is paged; wrap the response in a pager, which provides + # an `__aiter__` convenience method. + response = pagers.ListProcessorsAsyncPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + + # Done; return the response. + return response + + async def create_processor( + self, + request: document_processor_service.CreateProcessorRequest = None, + *, + parent: str = None, + processor: gcd_processor.Processor = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> gcd_processor.Processor: + r"""Creates a processor from the type processor that the + user chose. The processor will be at "ENABLED" state by + default after its creation. + + Args: + request (:class:`google.cloud.documentai_v1beta3.types.CreateProcessorRequest`): + The request object. Request message for create a + processor. Notice this request is sent to a regionalized + backend service, and if the processor type is not + available on that region, the creation will fail. + parent (:class:`str`): + Required. The parent (project and + location) under which to create the + processor. Format: + projects/{project}/locations/{location} + + This corresponds to the ``parent`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + processor (:class:`google.cloud.documentai_v1beta3.types.Processor`): + Required. The processor to be created, requires + [processor_type] and [display_name] to be set. Also, the + processor is under CMEK if CMEK fields are set. + + This corresponds to the ``processor`` 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.cloud.documentai_v1beta3.types.Processor: + The first-class citizen for + DocumentAI. Each processor defines how + to extract structural information from a + document. + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([parent, processor]) + 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." + ) + + request = document_processor_service.CreateProcessorRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if parent is not None: + request.parent = parent + if processor is not None: + request.processor = processor + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = gapic_v1.method_async.wrap_method( + self._client._transport.create_processor, + 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((("parent", request.parent),)), + ) + + # Send the request. + response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Done; return the response. + return response + + async def delete_processor( + self, + request: document_processor_service.DeleteProcessorRequest = None, + *, + name: str = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operation_async.AsyncOperation: + r"""Deletes the processor, unloads all deployed model + artifacts if it was enabled and then deletes all + artifacts associated with this processor. + + Args: + request (:class:`google.cloud.documentai_v1beta3.types.DeleteProcessorRequest`): + The request object. Request message for the delete + processor method. + name (:class:`str`): + Required. The processor resource name + to be deleted. + + This corresponds to the ``name`` 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_async.AsyncOperation: + An object representing a long-running operation. + + The result type for the operation will be :class:`google.protobuf.empty_pb2.Empty` A generic empty message that you can re-use to avoid defining duplicated + empty messages in your APIs. A typical example is to + use it as the request or the response type of an API + method. For instance: + + service Foo { + rpc Bar(google.protobuf.Empty) returns + (google.protobuf.Empty); + + } + + The JSON representation for Empty is empty JSON + object {}. + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([name]) + 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." + ) + + request = document_processor_service.DeleteProcessorRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if name is not None: + request.name = name + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = gapic_v1.method_async.wrap_method( + self._client._transport.delete_processor, + 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),)), + ) + + # 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, + empty_pb2.Empty, + metadata_type=document_processor_service.DeleteProcessorMetadata, + ) + + # Done; return the response. + return response + + async def enable_processor( + self, + request: document_processor_service.EnableProcessorRequest = None, + *, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operation_async.AsyncOperation: + r"""Enables a processor + + Args: + request (:class:`google.cloud.documentai_v1beta3.types.EnableProcessorRequest`): + The request object. Request message for the enable + processor 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: + 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_v1beta3.types.EnableProcessorResponse` + Response message for the enable processor method. + + """ + # Create or coerce a protobuf request object. + request = document_processor_service.EnableProcessorRequest(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.enable_processor, + 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),)), + ) + + # 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_processor_service.EnableProcessorResponse, + metadata_type=document_processor_service.EnableProcessorMetadata, + ) + + # Done; return the response. + return response + + async def disable_processor( + self, + request: document_processor_service.DisableProcessorRequest = None, + *, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operation_async.AsyncOperation: + r"""Disables a processor + + Args: + request (:class:`google.cloud.documentai_v1beta3.types.DisableProcessorRequest`): + The request object. Request message for the disable + processor 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: + 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_v1beta3.types.DisableProcessorResponse` + Response message for the disable processor method. + + """ + # Create or coerce a protobuf request object. + request = document_processor_service.DisableProcessorRequest(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.disable_processor, + 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),)), + ) + + # 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_processor_service.DisableProcessorResponse, + metadata_type=document_processor_service.DisableProcessorMetadata, + ) + + # Done; return the response. + return response + async def review_document( self, request: document_processor_service.ReviewDocumentRequest = None, @@ -370,7 +839,7 @@ async def review_document( Args: request (:class:`google.cloud.documentai_v1beta3.types.ReviewDocumentRequest`): The request object. Request message for review document - method. + method. Next Id: 6. human_review_config (:class:`str`): Required. The resource name of the HumanReviewConfig that the document will diff --git a/google/cloud/documentai_v1beta3/services/document_processor_service/client.py b/google/cloud/documentai_v1beta3/services/document_processor_service/client.py index 6cbccb9a..e2c8589f 100644 --- a/google/cloud/documentai_v1beta3/services/document_processor_service/client.py +++ b/google/cloud/documentai_v1beta3/services/document_processor_service/client.py @@ -32,8 +32,14 @@ from google.api_core import operation # type: ignore from google.api_core import operation_async # type: ignore +from google.cloud.documentai_v1beta3.services.document_processor_service import pagers from google.cloud.documentai_v1beta3.types import document from google.cloud.documentai_v1beta3.types import document_processor_service +from google.cloud.documentai_v1beta3.types import processor +from google.cloud.documentai_v1beta3.types import processor as gcd_processor +from google.cloud.documentai_v1beta3.types import processor_type +from google.protobuf import empty_pb2 # type: ignore +from google.protobuf import timestamp_pb2 # type: ignore from .transports.base import DocumentProcessorServiceTransport, DEFAULT_CLIENT_INFO from .transports.grpc import DocumentProcessorServiceGrpcTransport from .transports.grpc_asyncio import DocumentProcessorServiceGrpcAsyncIOTransport @@ -196,6 +202,22 @@ def parse_processor_path(path: str) -> Dict[str, str]: ) return m.groupdict() if m else {} + @staticmethod + def processor_type_path(project: str, location: str, processor_type: str,) -> str: + """Returns a fully-qualified processor_type string.""" + return "projects/{project}/locations/{location}/processorTypes/{processor_type}".format( + project=project, location=location, processor_type=processor_type, + ) + + @staticmethod + def parse_processor_type_path(path: str) -> Dict[str, str]: + """Parses a processor_type path into its component segments.""" + m = re.match( + r"^projects/(?P.+?)/locations/(?P.+?)/processorTypes/(?P.+?)$", + path, + ) + return m.groupdict() if m else {} + @staticmethod def common_billing_account_path(billing_account: str,) -> str: """Returns a fully-qualified billing_account string.""" @@ -525,6 +547,467 @@ def batch_process_documents( # Done; return the response. return response + def fetch_processor_types( + self, + request: document_processor_service.FetchProcessorTypesRequest = None, + *, + parent: str = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> document_processor_service.FetchProcessorTypesResponse: + r"""Fetches processor types. + + Args: + request (google.cloud.documentai_v1beta3.types.FetchProcessorTypesRequest): + The request object. Request message for fetch processor + types. + parent (str): + Required. The project of processor + type to list. Format: + projects/{project}/locations/{location} + + This corresponds to the ``parent`` 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.cloud.documentai_v1beta3.types.FetchProcessorTypesResponse: + Response message for fetch processor + types. + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([parent]) + 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." + ) + + # Minor optimization to avoid making a copy if the user passes + # in a document_processor_service.FetchProcessorTypesRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance( + request, document_processor_service.FetchProcessorTypesRequest + ): + request = document_processor_service.FetchProcessorTypesRequest(request) + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if parent is not None: + request.parent = parent + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[self._transport.fetch_processor_types] + + # 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),)), + ) + + # Send the request. + response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Done; return the response. + return response + + def list_processors( + self, + request: document_processor_service.ListProcessorsRequest = None, + *, + parent: str = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> pagers.ListProcessorsPager: + r"""Lists all processors which belong to this project. + + Args: + request (google.cloud.documentai_v1beta3.types.ListProcessorsRequest): + The request object. Request message for list all + processors belongs to a project. + parent (str): + Required. The parent (project and + location) which owns this collection of + Processors. Format: + projects/{project}/locations/{location} + + This corresponds to the ``parent`` 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.cloud.documentai_v1beta3.services.document_processor_service.pagers.ListProcessorsPager: + Response message for list processors. + Iterating over this object will yield + results and resolve additional pages + automatically. + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([parent]) + 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." + ) + + # Minor optimization to avoid making a copy if the user passes + # in a document_processor_service.ListProcessorsRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, document_processor_service.ListProcessorsRequest): + request = document_processor_service.ListProcessorsRequest(request) + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if parent is not None: + request.parent = parent + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[self._transport.list_processors] + + # 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),)), + ) + + # Send the request. + response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListProcessorsPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + + # Done; return the response. + return response + + def create_processor( + self, + request: document_processor_service.CreateProcessorRequest = None, + *, + parent: str = None, + processor: gcd_processor.Processor = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> gcd_processor.Processor: + r"""Creates a processor from the type processor that the + user chose. The processor will be at "ENABLED" state by + default after its creation. + + Args: + request (google.cloud.documentai_v1beta3.types.CreateProcessorRequest): + The request object. Request message for create a + processor. Notice this request is sent to a regionalized + backend service, and if the processor type is not + available on that region, the creation will fail. + parent (str): + Required. The parent (project and + location) under which to create the + processor. Format: + projects/{project}/locations/{location} + + This corresponds to the ``parent`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + processor (google.cloud.documentai_v1beta3.types.Processor): + Required. The processor to be created, requires + [processor_type] and [display_name] to be set. Also, the + processor is under CMEK if CMEK fields are set. + + This corresponds to the ``processor`` 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.cloud.documentai_v1beta3.types.Processor: + The first-class citizen for + DocumentAI. Each processor defines how + to extract structural information from a + document. + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([parent, processor]) + 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." + ) + + # Minor optimization to avoid making a copy if the user passes + # in a document_processor_service.CreateProcessorRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, document_processor_service.CreateProcessorRequest): + request = document_processor_service.CreateProcessorRequest(request) + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if parent is not None: + request.parent = parent + if processor is not None: + request.processor = processor + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[self._transport.create_processor] + + # 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),)), + ) + + # Send the request. + response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Done; return the response. + return response + + def delete_processor( + self, + request: document_processor_service.DeleteProcessorRequest = None, + *, + name: str = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operation.Operation: + r"""Deletes the processor, unloads all deployed model + artifacts if it was enabled and then deletes all + artifacts associated with this processor. + + Args: + request (google.cloud.documentai_v1beta3.types.DeleteProcessorRequest): + The request object. Request message for the delete + processor method. + name (str): + Required. The processor resource name + to be deleted. + + This corresponds to the ``name`` 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.protobuf.empty_pb2.Empty` A generic empty message that you can re-use to avoid defining duplicated + empty messages in your APIs. A typical example is to + use it as the request or the response type of an API + method. For instance: + + service Foo { + rpc Bar(google.protobuf.Empty) returns + (google.protobuf.Empty); + + } + + The JSON representation for Empty is empty JSON + object {}. + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([name]) + 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." + ) + + # Minor optimization to avoid making a copy if the user passes + # in a document_processor_service.DeleteProcessorRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, document_processor_service.DeleteProcessorRequest): + request = document_processor_service.DeleteProcessorRequest(request) + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if name is not None: + request.name = name + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[self._transport.delete_processor] + + # 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),)), + ) + + # 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, + empty_pb2.Empty, + metadata_type=document_processor_service.DeleteProcessorMetadata, + ) + + # Done; return the response. + return response + + def enable_processor( + self, + request: document_processor_service.EnableProcessorRequest = None, + *, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operation.Operation: + r"""Enables a processor + + Args: + request (google.cloud.documentai_v1beta3.types.EnableProcessorRequest): + The request object. Request message for the enable + processor 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: + google.api_core.operation.Operation: + An object representing a long-running operation. + + The result type for the operation will be + :class:`google.cloud.documentai_v1beta3.types.EnableProcessorResponse` + Response message for the enable processor method. + + """ + # Create or coerce a protobuf request object. + # Minor optimization to avoid making a copy if the user passes + # in a document_processor_service.EnableProcessorRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, document_processor_service.EnableProcessorRequest): + request = document_processor_service.EnableProcessorRequest(request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[self._transport.enable_processor] + + # 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),)), + ) + + # 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_processor_service.EnableProcessorResponse, + metadata_type=document_processor_service.EnableProcessorMetadata, + ) + + # Done; return the response. + return response + + def disable_processor( + self, + request: document_processor_service.DisableProcessorRequest = None, + *, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operation.Operation: + r"""Disables a processor + + Args: + request (google.cloud.documentai_v1beta3.types.DisableProcessorRequest): + The request object. Request message for the disable + processor 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: + google.api_core.operation.Operation: + An object representing a long-running operation. + + The result type for the operation will be + :class:`google.cloud.documentai_v1beta3.types.DisableProcessorResponse` + Response message for the disable processor method. + + """ + # Create or coerce a protobuf request object. + # Minor optimization to avoid making a copy if the user passes + # in a document_processor_service.DisableProcessorRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, document_processor_service.DisableProcessorRequest): + request = document_processor_service.DisableProcessorRequest(request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[self._transport.disable_processor] + + # 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),)), + ) + + # 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_processor_service.DisableProcessorResponse, + metadata_type=document_processor_service.DisableProcessorMetadata, + ) + + # Done; return the response. + return response + def review_document( self, request: document_processor_service.ReviewDocumentRequest = None, @@ -540,7 +1023,7 @@ def review_document( Args: request (google.cloud.documentai_v1beta3.types.ReviewDocumentRequest): The request object. Request message for review document - method. + method. Next Id: 6. human_review_config (str): Required. The resource name of the HumanReviewConfig that the document will diff --git a/google/cloud/documentai_v1beta3/services/document_processor_service/pagers.py b/google/cloud/documentai_v1beta3/services/document_processor_service/pagers.py new file mode 100644 index 00000000..37b1e9b7 --- /dev/null +++ b/google/cloud/documentai_v1beta3/services/document_processor_service/pagers.py @@ -0,0 +1,160 @@ +# -*- coding: utf-8 -*- +# Copyright 2020 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 ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.documentai_v1beta3.types import document_processor_service +from google.cloud.documentai_v1beta3.types import processor + + +class ListProcessorsPager: + """A pager for iterating through ``list_processors`` requests. + + This class thinly wraps an initial + :class:`google.cloud.documentai_v1beta3.types.ListProcessorsResponse` object, and + provides an ``__iter__`` method to iterate through its + ``processors`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``ListProcessors`` requests and continue to iterate + through the ``processors`` field on the + corresponding responses. + + All the usual :class:`google.cloud.documentai_v1beta3.types.ListProcessorsResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., document_processor_service.ListProcessorsResponse], + request: document_processor_service.ListProcessorsRequest, + response: document_processor_service.ListProcessorsResponse, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.documentai_v1beta3.types.ListProcessorsRequest): + The initial request object. + response (google.cloud.documentai_v1beta3.types.ListProcessorsResponse): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = document_processor_service.ListProcessorsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[document_processor_service.ListProcessorsResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[processor.Processor]: + for page in self.pages: + yield from page.processors + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListProcessorsAsyncPager: + """A pager for iterating through ``list_processors`` requests. + + This class thinly wraps an initial + :class:`google.cloud.documentai_v1beta3.types.ListProcessorsResponse` object, and + provides an ``__aiter__`` method to iterate through its + ``processors`` field. + + If there are more pages, the ``__aiter__`` method will make additional + ``ListProcessors`` requests and continue to iterate + through the ``processors`` field on the + corresponding responses. + + All the usual :class:`google.cloud.documentai_v1beta3.types.ListProcessorsResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[ + ..., Awaitable[document_processor_service.ListProcessorsResponse] + ], + request: document_processor_service.ListProcessorsRequest, + response: document_processor_service.ListProcessorsResponse, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiates the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.documentai_v1beta3.types.ListProcessorsRequest): + The initial request object. + response (google.cloud.documentai_v1beta3.types.ListProcessorsResponse): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = document_processor_service.ListProcessorsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + async def pages( + self, + ) -> AsyncIterable[document_processor_service.ListProcessorsResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = await self._method(self._request, metadata=self._metadata) + yield self._response + + def __aiter__(self) -> AsyncIterable[processor.Processor]: + async def async_generator(): + async for page in self.pages: + for response in page.processors: + yield response + + return async_generator() + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/google/cloud/documentai_v1beta3/services/document_processor_service/transports/base.py b/google/cloud/documentai_v1beta3/services/document_processor_service/transports/base.py index 0d7623ed..d44bd04e 100644 --- a/google/cloud/documentai_v1beta3/services/document_processor_service/transports/base.py +++ b/google/cloud/documentai_v1beta3/services/document_processor_service/transports/base.py @@ -27,6 +27,7 @@ from google.auth import credentials as ga_credentials # type: ignore from google.cloud.documentai_v1beta3.types import document_processor_service +from google.cloud.documentai_v1beta3.types import processor as gcd_processor from google.longrunning import operations_pb2 # type: ignore try: @@ -198,6 +199,26 @@ def _prep_wrapped_messages(self, client_info): default_timeout=120.0, client_info=client_info, ), + self.fetch_processor_types: gapic_v1.method.wrap_method( + self.fetch_processor_types, + default_timeout=None, + client_info=client_info, + ), + self.list_processors: gapic_v1.method.wrap_method( + self.list_processors, default_timeout=None, client_info=client_info, + ), + self.create_processor: gapic_v1.method.wrap_method( + self.create_processor, default_timeout=None, client_info=client_info, + ), + self.delete_processor: gapic_v1.method.wrap_method( + self.delete_processor, default_timeout=None, client_info=client_info, + ), + self.enable_processor: gapic_v1.method.wrap_method( + self.enable_processor, default_timeout=None, client_info=client_info, + ), + self.disable_processor: gapic_v1.method.wrap_method( + self.disable_processor, default_timeout=None, client_info=client_info, + ), self.review_document: gapic_v1.method.wrap_method( self.review_document, default_retry=retries.Retry( @@ -241,6 +262,66 @@ def batch_process_documents( ]: raise NotImplementedError() + @property + def fetch_processor_types( + self, + ) -> Callable[ + [document_processor_service.FetchProcessorTypesRequest], + Union[ + document_processor_service.FetchProcessorTypesResponse, + Awaitable[document_processor_service.FetchProcessorTypesResponse], + ], + ]: + raise NotImplementedError() + + @property + def list_processors( + self, + ) -> Callable[ + [document_processor_service.ListProcessorsRequest], + Union[ + document_processor_service.ListProcessorsResponse, + Awaitable[document_processor_service.ListProcessorsResponse], + ], + ]: + raise NotImplementedError() + + @property + def create_processor( + self, + ) -> Callable[ + [document_processor_service.CreateProcessorRequest], + Union[gcd_processor.Processor, Awaitable[gcd_processor.Processor]], + ]: + raise NotImplementedError() + + @property + def delete_processor( + self, + ) -> Callable[ + [document_processor_service.DeleteProcessorRequest], + Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]], + ]: + raise NotImplementedError() + + @property + def enable_processor( + self, + ) -> Callable[ + [document_processor_service.EnableProcessorRequest], + Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]], + ]: + raise NotImplementedError() + + @property + def disable_processor( + self, + ) -> Callable[ + [document_processor_service.DisableProcessorRequest], + Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]], + ]: + raise NotImplementedError() + @property def review_document( self, diff --git a/google/cloud/documentai_v1beta3/services/document_processor_service/transports/grpc.py b/google/cloud/documentai_v1beta3/services/document_processor_service/transports/grpc.py index 3e00e89f..0d2ab17a 100644 --- a/google/cloud/documentai_v1beta3/services/document_processor_service/transports/grpc.py +++ b/google/cloud/documentai_v1beta3/services/document_processor_service/transports/grpc.py @@ -26,6 +26,7 @@ import grpc # type: ignore from google.cloud.documentai_v1beta3.types import document_processor_service +from google.cloud.documentai_v1beta3.types import processor as gcd_processor from google.longrunning import operations_pb2 # type: ignore from .base import DocumentProcessorServiceTransport, DEFAULT_CLIENT_INFO @@ -300,6 +301,180 @@ def batch_process_documents( ) return self._stubs["batch_process_documents"] + @property + def fetch_processor_types( + self, + ) -> Callable[ + [document_processor_service.FetchProcessorTypesRequest], + document_processor_service.FetchProcessorTypesResponse, + ]: + r"""Return a callable for the fetch processor types method over gRPC. + + Fetches processor types. + + Returns: + Callable[[~.FetchProcessorTypesRequest], + ~.FetchProcessorTypesResponse]: + 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 "fetch_processor_types" not in self._stubs: + self._stubs["fetch_processor_types"] = self.grpc_channel.unary_unary( + "/google.cloud.documentai.v1beta3.DocumentProcessorService/FetchProcessorTypes", + request_serializer=document_processor_service.FetchProcessorTypesRequest.serialize, + response_deserializer=document_processor_service.FetchProcessorTypesResponse.deserialize, + ) + return self._stubs["fetch_processor_types"] + + @property + def list_processors( + self, + ) -> Callable[ + [document_processor_service.ListProcessorsRequest], + document_processor_service.ListProcessorsResponse, + ]: + r"""Return a callable for the list processors method over gRPC. + + Lists all processors which belong to this project. + + Returns: + Callable[[~.ListProcessorsRequest], + ~.ListProcessorsResponse]: + 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 "list_processors" not in self._stubs: + self._stubs["list_processors"] = self.grpc_channel.unary_unary( + "/google.cloud.documentai.v1beta3.DocumentProcessorService/ListProcessors", + request_serializer=document_processor_service.ListProcessorsRequest.serialize, + response_deserializer=document_processor_service.ListProcessorsResponse.deserialize, + ) + return self._stubs["list_processors"] + + @property + def create_processor( + self, + ) -> Callable[ + [document_processor_service.CreateProcessorRequest], gcd_processor.Processor + ]: + r"""Return a callable for the create processor method over gRPC. + + Creates a processor from the type processor that the + user chose. The processor will be at "ENABLED" state by + default after its creation. + + Returns: + Callable[[~.CreateProcessorRequest], + ~.Processor]: + 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 "create_processor" not in self._stubs: + self._stubs["create_processor"] = self.grpc_channel.unary_unary( + "/google.cloud.documentai.v1beta3.DocumentProcessorService/CreateProcessor", + request_serializer=document_processor_service.CreateProcessorRequest.serialize, + response_deserializer=gcd_processor.Processor.deserialize, + ) + return self._stubs["create_processor"] + + @property + def delete_processor( + self, + ) -> Callable[ + [document_processor_service.DeleteProcessorRequest], operations_pb2.Operation + ]: + r"""Return a callable for the delete processor method over gRPC. + + Deletes the processor, unloads all deployed model + artifacts if it was enabled and then deletes all + artifacts associated with this processor. + + Returns: + Callable[[~.DeleteProcessorRequest], + ~.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 "delete_processor" not in self._stubs: + self._stubs["delete_processor"] = self.grpc_channel.unary_unary( + "/google.cloud.documentai.v1beta3.DocumentProcessorService/DeleteProcessor", + request_serializer=document_processor_service.DeleteProcessorRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["delete_processor"] + + @property + def enable_processor( + self, + ) -> Callable[ + [document_processor_service.EnableProcessorRequest], operations_pb2.Operation + ]: + r"""Return a callable for the enable processor method over gRPC. + + Enables a processor + + Returns: + Callable[[~.EnableProcessorRequest], + ~.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 "enable_processor" not in self._stubs: + self._stubs["enable_processor"] = self.grpc_channel.unary_unary( + "/google.cloud.documentai.v1beta3.DocumentProcessorService/EnableProcessor", + request_serializer=document_processor_service.EnableProcessorRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["enable_processor"] + + @property + def disable_processor( + self, + ) -> Callable[ + [document_processor_service.DisableProcessorRequest], operations_pb2.Operation + ]: + r"""Return a callable for the disable processor method over gRPC. + + Disables a processor + + Returns: + Callable[[~.DisableProcessorRequest], + ~.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 "disable_processor" not in self._stubs: + self._stubs["disable_processor"] = self.grpc_channel.unary_unary( + "/google.cloud.documentai.v1beta3.DocumentProcessorService/DisableProcessor", + request_serializer=document_processor_service.DisableProcessorRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["disable_processor"] + @property def review_document( self, diff --git a/google/cloud/documentai_v1beta3/services/document_processor_service/transports/grpc_asyncio.py b/google/cloud/documentai_v1beta3/services/document_processor_service/transports/grpc_asyncio.py index 84c7318a..884dfc82 100644 --- a/google/cloud/documentai_v1beta3/services/document_processor_service/transports/grpc_asyncio.py +++ b/google/cloud/documentai_v1beta3/services/document_processor_service/transports/grpc_asyncio.py @@ -27,6 +27,7 @@ from grpc.experimental import aio # type: ignore from google.cloud.documentai_v1beta3.types import document_processor_service +from google.cloud.documentai_v1beta3.types import processor as gcd_processor from google.longrunning import operations_pb2 # type: ignore from .base import DocumentProcessorServiceTransport, DEFAULT_CLIENT_INFO from .grpc import DocumentProcessorServiceGrpcTransport @@ -306,6 +307,184 @@ def batch_process_documents( ) return self._stubs["batch_process_documents"] + @property + def fetch_processor_types( + self, + ) -> Callable[ + [document_processor_service.FetchProcessorTypesRequest], + Awaitable[document_processor_service.FetchProcessorTypesResponse], + ]: + r"""Return a callable for the fetch processor types method over gRPC. + + Fetches processor types. + + Returns: + Callable[[~.FetchProcessorTypesRequest], + Awaitable[~.FetchProcessorTypesResponse]]: + 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 "fetch_processor_types" not in self._stubs: + self._stubs["fetch_processor_types"] = self.grpc_channel.unary_unary( + "/google.cloud.documentai.v1beta3.DocumentProcessorService/FetchProcessorTypes", + request_serializer=document_processor_service.FetchProcessorTypesRequest.serialize, + response_deserializer=document_processor_service.FetchProcessorTypesResponse.deserialize, + ) + return self._stubs["fetch_processor_types"] + + @property + def list_processors( + self, + ) -> Callable[ + [document_processor_service.ListProcessorsRequest], + Awaitable[document_processor_service.ListProcessorsResponse], + ]: + r"""Return a callable for the list processors method over gRPC. + + Lists all processors which belong to this project. + + Returns: + Callable[[~.ListProcessorsRequest], + Awaitable[~.ListProcessorsResponse]]: + 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 "list_processors" not in self._stubs: + self._stubs["list_processors"] = self.grpc_channel.unary_unary( + "/google.cloud.documentai.v1beta3.DocumentProcessorService/ListProcessors", + request_serializer=document_processor_service.ListProcessorsRequest.serialize, + response_deserializer=document_processor_service.ListProcessorsResponse.deserialize, + ) + return self._stubs["list_processors"] + + @property + def create_processor( + self, + ) -> Callable[ + [document_processor_service.CreateProcessorRequest], + Awaitable[gcd_processor.Processor], + ]: + r"""Return a callable for the create processor method over gRPC. + + Creates a processor from the type processor that the + user chose. The processor will be at "ENABLED" state by + default after its creation. + + Returns: + Callable[[~.CreateProcessorRequest], + Awaitable[~.Processor]]: + 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 "create_processor" not in self._stubs: + self._stubs["create_processor"] = self.grpc_channel.unary_unary( + "/google.cloud.documentai.v1beta3.DocumentProcessorService/CreateProcessor", + request_serializer=document_processor_service.CreateProcessorRequest.serialize, + response_deserializer=gcd_processor.Processor.deserialize, + ) + return self._stubs["create_processor"] + + @property + def delete_processor( + self, + ) -> Callable[ + [document_processor_service.DeleteProcessorRequest], + Awaitable[operations_pb2.Operation], + ]: + r"""Return a callable for the delete processor method over gRPC. + + Deletes the processor, unloads all deployed model + artifacts if it was enabled and then deletes all + artifacts associated with this processor. + + Returns: + Callable[[~.DeleteProcessorRequest], + 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 "delete_processor" not in self._stubs: + self._stubs["delete_processor"] = self.grpc_channel.unary_unary( + "/google.cloud.documentai.v1beta3.DocumentProcessorService/DeleteProcessor", + request_serializer=document_processor_service.DeleteProcessorRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["delete_processor"] + + @property + def enable_processor( + self, + ) -> Callable[ + [document_processor_service.EnableProcessorRequest], + Awaitable[operations_pb2.Operation], + ]: + r"""Return a callable for the enable processor method over gRPC. + + Enables a processor + + Returns: + Callable[[~.EnableProcessorRequest], + 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 "enable_processor" not in self._stubs: + self._stubs["enable_processor"] = self.grpc_channel.unary_unary( + "/google.cloud.documentai.v1beta3.DocumentProcessorService/EnableProcessor", + request_serializer=document_processor_service.EnableProcessorRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["enable_processor"] + + @property + def disable_processor( + self, + ) -> Callable[ + [document_processor_service.DisableProcessorRequest], + Awaitable[operations_pb2.Operation], + ]: + r"""Return a callable for the disable processor method over gRPC. + + Disables a processor + + Returns: + Callable[[~.DisableProcessorRequest], + 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 "disable_processor" not in self._stubs: + self._stubs["disable_processor"] = self.grpc_channel.unary_unary( + "/google.cloud.documentai.v1beta3.DocumentProcessorService/DisableProcessor", + request_serializer=document_processor_service.DisableProcessorRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["disable_processor"] + @property def review_document( self, diff --git a/google/cloud/documentai_v1beta3/types/__init__.py b/google/cloud/documentai_v1beta3/types/__init__.py index 677a7bcb..60aab2df 100644 --- a/google/cloud/documentai_v1beta3/types/__init__.py +++ b/google/cloud/documentai_v1beta3/types/__init__.py @@ -26,7 +26,20 @@ BatchProcessMetadata, BatchProcessRequest, BatchProcessResponse, + CreateProcessorRequest, + DeleteProcessorMetadata, + DeleteProcessorRequest, + DisableProcessorMetadata, + DisableProcessorRequest, + DisableProcessorResponse, + EnableProcessorMetadata, + EnableProcessorRequest, + EnableProcessorResponse, + FetchProcessorTypesRequest, + FetchProcessorTypesResponse, HumanReviewStatus, + ListProcessorsRequest, + ListProcessorsResponse, ProcessRequest, ProcessResponse, ReviewDocumentOperationMetadata, @@ -39,6 +52,8 @@ Vertex, ) from .operation_metadata import CommonOperationMetadata +from .processor import Processor +from .processor_type import ProcessorType __all__ = ( "Document", @@ -51,7 +66,20 @@ "BatchProcessMetadata", "BatchProcessRequest", "BatchProcessResponse", + "CreateProcessorRequest", + "DeleteProcessorMetadata", + "DeleteProcessorRequest", + "DisableProcessorMetadata", + "DisableProcessorRequest", + "DisableProcessorResponse", + "EnableProcessorMetadata", + "EnableProcessorRequest", + "EnableProcessorResponse", + "FetchProcessorTypesRequest", + "FetchProcessorTypesResponse", "HumanReviewStatus", + "ListProcessorsRequest", + "ListProcessorsResponse", "ProcessRequest", "ProcessResponse", "ReviewDocumentOperationMetadata", @@ -61,4 +89,6 @@ "NormalizedVertex", "Vertex", "CommonOperationMetadata", + "Processor", + "ProcessorType", ) diff --git a/google/cloud/documentai_v1beta3/types/document.py b/google/cloud/documentai_v1beta3/types/document.py index bb808541..ef3fc26d 100644 --- a/google/cloud/documentai_v1beta3/types/document.py +++ b/google/cloud/documentai_v1beta3/types/document.py @@ -212,6 +212,8 @@ class Page(proto.Message): form_fields (Sequence[google.cloud.documentai_v1beta3.types.Document.Page.FormField]): A list of visually detected form fields on the page. + provenance (google.cloud.documentai_v1beta3.types.Document.Provenance): + The history of this page. """ class Dimension(proto.Message): @@ -556,6 +558,8 @@ class FormField(proto.Message): - blank (this indicates the field_value is normal text) - "unfilled_checkbox" - "filled_checkbox". + provenance (google.cloud.documentai_v1beta3.types.Document.Provenance): + The history of this annotation. """ field_name = proto.Field( @@ -571,6 +575,9 @@ class FormField(proto.Message): proto.MESSAGE, number=4, message="Document.Page.DetectedLanguage", ) value_type = proto.Field(proto.STRING, number=5,) + provenance = proto.Field( + proto.MESSAGE, number=8, message="Document.Provenance", + ) class DetectedLanguage(proto.Message): r"""Detected language for a structural component. @@ -619,6 +626,9 @@ class DetectedLanguage(proto.Message): form_fields = proto.RepeatedField( proto.MESSAGE, number=11, message="Document.Page.FormField", ) + provenance = proto.Field( + proto.MESSAGE, number=16, message="Document.Provenance", + ) class Entity(proto.Message): r"""A phrase in the text that is a known entity type, such as a @@ -823,7 +833,9 @@ class PageRef(proto.Message): Required. Index into the [Document.pages][google.cloud.documentai.v1beta3.Document.pages] element, for example using [Document.pages][page_refs.page] - to locate the related page element. + 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_v1beta3.types.Document.PageAnchor.PageRef.LayoutType): Optional. The type of the layout element that is being referenced if any. @@ -903,11 +915,16 @@ class Parent(proto.Message): revision (int): The index of the [Document.revisions] identifying the parent revision. + index (int): + The index of the parent revisions + corresponding collection of items (eg. list of + entities, properties within entities, etc.) id (int): The id of the parent provenance. """ revision = proto.Field(proto.INT32, number=1,) + index = proto.Field(proto.INT32, number=3,) id = proto.Field(proto.INT32, number=2,) revision = proto.Field(proto.INT32, number=1,) diff --git a/google/cloud/documentai_v1beta3/types/document_processor_service.py b/google/cloud/documentai_v1beta3/types/document_processor_service.py index 81038589..157f7676 100644 --- a/google/cloud/documentai_v1beta3/types/document_processor_service.py +++ b/google/cloud/documentai_v1beta3/types/document_processor_service.py @@ -18,6 +18,8 @@ from google.cloud.documentai_v1beta3.types import document as gcd_document from google.cloud.documentai_v1beta3.types import document_io from google.cloud.documentai_v1beta3.types import operation_metadata +from google.cloud.documentai_v1beta3.types import processor as gcd_processor +from google.cloud.documentai_v1beta3.types import processor_type from google.protobuf import timestamp_pb2 # type: ignore from google.rpc import status_pb2 # type: ignore @@ -31,6 +33,19 @@ "BatchProcessRequest", "BatchProcessResponse", "BatchProcessMetadata", + "FetchProcessorTypesRequest", + "FetchProcessorTypesResponse", + "ListProcessorsRequest", + "ListProcessorsResponse", + "CreateProcessorRequest", + "DeleteProcessorRequest", + "DeleteProcessorMetadata", + "EnableProcessorRequest", + "EnableProcessorResponse", + "EnableProcessorMetadata", + "DisableProcessorRequest", + "DisableProcessorResponse", + "DisableProcessorMetadata", "ReviewDocumentRequest", "ReviewDocumentResponse", "ReviewDocumentOperationMetadata", @@ -258,8 +273,183 @@ class IndividualProcessStatus(proto.Message): ) +class FetchProcessorTypesRequest(proto.Message): + r"""Request message for fetch processor types. + Attributes: + parent (str): + Required. The project of processor type to + list. Format: + projects/{project}/locations/{location} + """ + + parent = proto.Field(proto.STRING, number=1,) + + +class FetchProcessorTypesResponse(proto.Message): + r"""Response message for fetch processor types. + Attributes: + processor_types (Sequence[google.cloud.documentai_v1beta3.types.ProcessorType]): + The list of processor types. + """ + + processor_types = proto.RepeatedField( + proto.MESSAGE, number=1, message=processor_type.ProcessorType, + ) + + +class ListProcessorsRequest(proto.Message): + r"""Request message for list all processors belongs to a project. + Attributes: + parent (str): + Required. The parent (project and location) + which owns this collection of Processors. + Format: projects/{project}/locations/{location} + page_size (int): + The maximum number of processors to return. + If unspecified, at most 50 processors will be + returned. The maximum value is 100; values above + 100 will be coerced to 100. + page_token (str): + We will return the processors sorted by + creation time. The page token will point to the + next processor. + """ + + parent = proto.Field(proto.STRING, number=1,) + page_size = proto.Field(proto.INT32, number=2,) + page_token = proto.Field(proto.STRING, number=3,) + + +class ListProcessorsResponse(proto.Message): + r"""Response message for list processors. + Attributes: + processors (Sequence[google.cloud.documentai_v1beta3.types.Processor]): + The list of processors. + next_page_token (str): + Points to the next processor, otherwise + empty. + """ + + @property + def raw_page(self): + return self + + processors = proto.RepeatedField( + proto.MESSAGE, number=1, message=gcd_processor.Processor, + ) + next_page_token = proto.Field(proto.STRING, number=2,) + + +class CreateProcessorRequest(proto.Message): + r"""Request message for create a processor. Notice this request + is sent to a regionalized backend service, and if the processor + type is not available on that region, the creation will fail. + + Attributes: + parent (str): + Required. The parent (project and location) + under which to create the processor. Format: + projects/{project}/locations/{location} + processor (google.cloud.documentai_v1beta3.types.Processor): + Required. The processor to be created, requires + [processor_type] and [display_name] to be set. Also, the + processor is under CMEK if CMEK fields are set. + """ + + parent = proto.Field(proto.STRING, number=1,) + processor = proto.Field(proto.MESSAGE, number=2, message=gcd_processor.Processor,) + + +class DeleteProcessorRequest(proto.Message): + r"""Request message for the delete processor method. + Attributes: + name (str): + Required. The processor resource name to be + deleted. + """ + + name = proto.Field(proto.STRING, number=1,) + + +class DeleteProcessorMetadata(proto.Message): + r"""The long running operation metadata for delete processor + method. + + Attributes: + common_metadata (google.cloud.documentai_v1beta3.types.CommonOperationMetadata): + The basic metadata of the long running + operation. + """ + + common_metadata = proto.Field( + proto.MESSAGE, number=5, message=operation_metadata.CommonOperationMetadata, + ) + + +class EnableProcessorRequest(proto.Message): + r"""Request message for the enable processor method. + Attributes: + name (str): + Required. The processor resource name to be + enabled. + """ + + name = proto.Field(proto.STRING, number=1,) + + +class EnableProcessorResponse(proto.Message): + r"""Response message for the enable processor method. """ + + +class EnableProcessorMetadata(proto.Message): + r"""The long running operation metadata for enable processor + method. + + Attributes: + common_metadata (google.cloud.documentai_v1beta3.types.CommonOperationMetadata): + The basic metadata of the long running + operation. + """ + + common_metadata = proto.Field( + proto.MESSAGE, number=5, message=operation_metadata.CommonOperationMetadata, + ) + + +class DisableProcessorRequest(proto.Message): + r"""Request message for the disable processor method. + Attributes: + name (str): + Required. The processor resource name to be + disabled. + """ + + name = proto.Field(proto.STRING, number=1,) + + +class DisableProcessorResponse(proto.Message): + r"""Response message for the disable processor method. """ + + +class DisableProcessorMetadata(proto.Message): + r"""The long running operation metadata for disable processor + method. + + Attributes: + common_metadata (google.cloud.documentai_v1beta3.types.CommonOperationMetadata): + The basic metadata of the long running + operation. + """ + + common_metadata = proto.Field( + proto.MESSAGE, number=5, message=operation_metadata.CommonOperationMetadata, + ) + + class ReviewDocumentRequest(proto.Message): r"""Request message for review document method. + Next Id: 6. + Attributes: inline_document (google.cloud.documentai_v1beta3.types.Document): An inline document proto. @@ -269,13 +459,25 @@ class ReviewDocumentRequest(proto.Message): reviewed with. document (google.cloud.documentai_v1beta3.types.Document): The document that needs human review. + enable_schema_validation (bool): + Whether the validation should be performed on + the ad-hoc review request. + priority (google.cloud.documentai_v1beta3.types.ReviewDocumentRequest.Priority): + The priority of the human review task. """ + class Priority(proto.Enum): + r"""The priority level of the human review task.""" + DEFAULT = 0 + URGENT = 1 + inline_document = proto.Field( proto.MESSAGE, number=4, oneof="source", message=gcd_document.Document, ) human_review_config = proto.Field(proto.STRING, number=1,) document = proto.Field(proto.MESSAGE, number=2, message=gcd_document.Document,) + enable_schema_validation = proto.Field(proto.BOOL, number=3,) + priority = proto.Field(proto.ENUM, number=5, enum=Priority,) class ReviewDocumentResponse(proto.Message): diff --git a/google/cloud/documentai_v1beta3/types/processor.py b/google/cloud/documentai_v1beta3/types/processor.py new file mode 100644 index 00000000..0c613e7d --- /dev/null +++ b/google/cloud/documentai_v1beta3/types/processor.py @@ -0,0 +1,76 @@ +# -*- coding: utf-8 -*- +# Copyright 2020 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 proto # type: ignore + +from google.protobuf import timestamp_pb2 # type: ignore + + +__protobuf__ = proto.module( + package="google.cloud.documentai.v1beta3", manifest={"Processor",}, +) + + +class Processor(proto.Message): + r"""The first-class citizen for DocumentAI. Each processor + defines how to extract structural information from a document. + + Attributes: + name (str): + Output only. Immutable. The resource name of + the processor. Format: + projects/{project}/locations/{location}/processors/{processor} + type_ (str): + The processor type. + display_name (str): + The display name of the processor. + state (google.cloud.documentai_v1beta3.types.Processor.State): + Output only. The state of the processor. + default_processor_version (str): + The default processor version. + process_endpoint (str): + Output only. Immutable. The http endpoint + that can be called to invoke processing. + create_time (google.protobuf.timestamp_pb2.Timestamp): + The time the processor was created. + kms_key_name (str): + The KMS key used for encryption/decryption in + CMEK scenarios. See + https://cloud.google.com/security-key- + management. + """ + + class State(proto.Enum): + r"""The possible states of the processor.""" + STATE_UNSPECIFIED = 0 + ENABLED = 1 + DISABLED = 2 + ENABLING = 3 + DISABLING = 4 + CREATING = 5 + FAILED = 6 + DELETING = 7 + + name = proto.Field(proto.STRING, number=1,) + type_ = proto.Field(proto.STRING, number=2,) + display_name = proto.Field(proto.STRING, number=3,) + state = proto.Field(proto.ENUM, number=4, enum=State,) + default_processor_version = proto.Field(proto.STRING, number=9,) + process_endpoint = proto.Field(proto.STRING, number=6,) + create_time = proto.Field(proto.MESSAGE, number=7, message=timestamp_pb2.Timestamp,) + kms_key_name = proto.Field(proto.STRING, number=8,) + + +__all__ = tuple(sorted(__protobuf__.manifest)) diff --git a/google/cloud/documentai_v1beta3/types/processor_type.py b/google/cloud/documentai_v1beta3/types/processor_type.py new file mode 100644 index 00000000..9f0ec773 --- /dev/null +++ b/google/cloud/documentai_v1beta3/types/processor_type.py @@ -0,0 +1,65 @@ +# -*- coding: utf-8 -*- +# Copyright 2020 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 proto # type: ignore + + +__protobuf__ = proto.module( + package="google.cloud.documentai.v1beta3", manifest={"ProcessorType",}, +) + + +class ProcessorType(proto.Message): + r"""A processor type is responsible for performing a certain + document understanding task on a certain type of document. + + Attributes: + name (str): + The resource name of the processor type. + type_ (str): + The type of the processor. + category (str): + The processor category. + available_locations (Sequence[google.cloud.documentai_v1beta3.types.ProcessorType.LocationInfo]): + The locations in which this processor is + available. + allow_creation (bool): + Whether the processor type allows creation. + If yes, user can create a processor of this + processor type. Otherwise, user needs to require + for whitelisting. + """ + + class LocationInfo(proto.Message): + r"""The location information about where the processor is + available. + + Attributes: + location_id (str): + The location id. + """ + + location_id = proto.Field(proto.STRING, number=1,) + + name = proto.Field(proto.STRING, number=1,) + type_ = proto.Field(proto.STRING, number=2,) + category = proto.Field(proto.STRING, number=3,) + available_locations = proto.RepeatedField( + proto.MESSAGE, number=4, message=LocationInfo, + ) + allow_creation = proto.Field(proto.BOOL, number=6,) + + +__all__ = tuple(sorted(__protobuf__.manifest)) diff --git a/tests/unit/gapic/documentai_v1beta3/test_document_processor_service.py b/tests/unit/gapic/documentai_v1beta3/test_document_processor_service.py index 7ce09e4e..8c210ecf 100644 --- a/tests/unit/gapic/documentai_v1beta3/test_document_processor_service.py +++ b/tests/unit/gapic/documentai_v1beta3/test_document_processor_service.py @@ -40,6 +40,7 @@ from google.cloud.documentai_v1beta3.services.document_processor_service import ( DocumentProcessorServiceClient, ) +from google.cloud.documentai_v1beta3.services.document_processor_service import pagers from google.cloud.documentai_v1beta3.services.document_processor_service import ( transports, ) @@ -53,6 +54,9 @@ from google.cloud.documentai_v1beta3.types import document_io from google.cloud.documentai_v1beta3.types import document_processor_service from google.cloud.documentai_v1beta3.types import geometry +from google.cloud.documentai_v1beta3.types import processor +from google.cloud.documentai_v1beta3.types import processor as gcd_processor +from google.cloud.documentai_v1beta3.types import processor_type from google.longrunning import operations_pb2 from google.oauth2 import service_account from google.protobuf import any_pb2 # type: ignore @@ -956,6 +960,1336 @@ async def test_batch_process_documents_flattened_error_async(): ) +def test_fetch_processor_types( + transport: str = "grpc", + request_type=document_processor_service.FetchProcessorTypesRequest, +): + client = DocumentProcessorServiceClient( + 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.fetch_processor_types), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = document_processor_service.FetchProcessorTypesResponse() + response = client.fetch_processor_types(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.FetchProcessorTypesRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, document_processor_service.FetchProcessorTypesResponse) + + +def test_fetch_processor_types_from_dict(): + test_fetch_processor_types(request_type=dict) + + +def test_fetch_processor_types_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 = DocumentProcessorServiceClient( + 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.fetch_processor_types), "__call__" + ) as call: + client.fetch_processor_types() + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.FetchProcessorTypesRequest() + + +@pytest.mark.asyncio +async def test_fetch_processor_types_async( + transport: str = "grpc_asyncio", + request_type=document_processor_service.FetchProcessorTypesRequest, +): + client = DocumentProcessorServiceAsyncClient( + 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.fetch_processor_types), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + document_processor_service.FetchProcessorTypesResponse() + ) + response = await client.fetch_processor_types(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.FetchProcessorTypesRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, document_processor_service.FetchProcessorTypesResponse) + + +@pytest.mark.asyncio +async def test_fetch_processor_types_async_from_dict(): + await test_fetch_processor_types_async(request_type=dict) + + +def test_fetch_processor_types_field_headers(): + client = DocumentProcessorServiceClient( + 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_processor_service.FetchProcessorTypesRequest() + + request.parent = "parent/value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.fetch_processor_types), "__call__" + ) as call: + call.return_value = document_processor_service.FetchProcessorTypesResponse() + client.fetch_processor_types(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_fetch_processor_types_field_headers_async(): + client = DocumentProcessorServiceAsyncClient( + 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_processor_service.FetchProcessorTypesRequest() + + request.parent = "parent/value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.fetch_processor_types), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + document_processor_service.FetchProcessorTypesResponse() + ) + await client.fetch_processor_types(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_fetch_processor_types_flattened(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.fetch_processor_types), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = document_processor_service.FetchProcessorTypesResponse() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.fetch_processor_types(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] + assert args[0].parent == "parent_value" + + +def test_fetch_processor_types_flattened_error(): + client = DocumentProcessorServiceClient( + 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.fetch_processor_types( + document_processor_service.FetchProcessorTypesRequest(), + parent="parent_value", + ) + + +@pytest.mark.asyncio +async def test_fetch_processor_types_flattened_async(): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.fetch_processor_types), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = document_processor_service.FetchProcessorTypesResponse() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + document_processor_service.FetchProcessorTypesResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.fetch_processor_types(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] + assert args[0].parent == "parent_value" + + +@pytest.mark.asyncio +async def test_fetch_processor_types_flattened_error_async(): + client = DocumentProcessorServiceAsyncClient( + 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.fetch_processor_types( + document_processor_service.FetchProcessorTypesRequest(), + parent="parent_value", + ) + + +def test_list_processors( + transport: str = "grpc", + request_type=document_processor_service.ListProcessorsRequest, +): + client = DocumentProcessorServiceClient( + 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.list_processors), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = document_processor_service.ListProcessorsResponse( + next_page_token="next_page_token_value", + ) + response = client.list_processors(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.ListProcessorsRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListProcessorsPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_processors_from_dict(): + test_list_processors(request_type=dict) + + +def test_list_processors_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 = DocumentProcessorServiceClient( + 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.list_processors), "__call__") as call: + client.list_processors() + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.ListProcessorsRequest() + + +@pytest.mark.asyncio +async def test_list_processors_async( + transport: str = "grpc_asyncio", + request_type=document_processor_service.ListProcessorsRequest, +): + client = DocumentProcessorServiceAsyncClient( + 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.list_processors), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + document_processor_service.ListProcessorsResponse( + next_page_token="next_page_token_value", + ) + ) + response = await client.list_processors(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.ListProcessorsRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListProcessorsAsyncPager) + assert response.next_page_token == "next_page_token_value" + + +@pytest.mark.asyncio +async def test_list_processors_async_from_dict(): + await test_list_processors_async(request_type=dict) + + +def test_list_processors_field_headers(): + client = DocumentProcessorServiceClient( + 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_processor_service.ListProcessorsRequest() + + request.parent = "parent/value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_processors), "__call__") as call: + call.return_value = document_processor_service.ListProcessorsResponse() + client.list_processors(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_list_processors_field_headers_async(): + client = DocumentProcessorServiceAsyncClient( + 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_processor_service.ListProcessorsRequest() + + request.parent = "parent/value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_processors), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + document_processor_service.ListProcessorsResponse() + ) + await client.list_processors(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_list_processors_flattened(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_processors), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = document_processor_service.ListProcessorsResponse() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.list_processors(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] + assert args[0].parent == "parent_value" + + +def test_list_processors_flattened_error(): + client = DocumentProcessorServiceClient( + 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.list_processors( + document_processor_service.ListProcessorsRequest(), parent="parent_value", + ) + + +@pytest.mark.asyncio +async def test_list_processors_flattened_async(): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_processors), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = document_processor_service.ListProcessorsResponse() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + document_processor_service.ListProcessorsResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.list_processors(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] + assert args[0].parent == "parent_value" + + +@pytest.mark.asyncio +async def test_list_processors_flattened_error_async(): + client = DocumentProcessorServiceAsyncClient( + 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.list_processors( + document_processor_service.ListProcessorsRequest(), parent="parent_value", + ) + + +def test_list_processors_pager(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials, + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_processors), "__call__") as call: + # Set the response to a series of pages. + call.side_effect = ( + document_processor_service.ListProcessorsResponse( + processors=[ + processor.Processor(), + processor.Processor(), + processor.Processor(), + ], + next_page_token="abc", + ), + document_processor_service.ListProcessorsResponse( + processors=[], next_page_token="def", + ), + document_processor_service.ListProcessorsResponse( + processors=[processor.Processor(),], next_page_token="ghi", + ), + document_processor_service.ListProcessorsResponse( + processors=[processor.Processor(), processor.Processor(),], + ), + RuntimeError, + ) + + metadata = () + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", ""),)), + ) + pager = client.list_processors(request={}) + + assert pager._metadata == metadata + + results = [i for i in pager] + assert len(results) == 6 + assert all(isinstance(i, processor.Processor) for i in results) + + +def test_list_processors_pages(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials, + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_processors), "__call__") as call: + # Set the response to a series of pages. + call.side_effect = ( + document_processor_service.ListProcessorsResponse( + processors=[ + processor.Processor(), + processor.Processor(), + processor.Processor(), + ], + next_page_token="abc", + ), + document_processor_service.ListProcessorsResponse( + processors=[], next_page_token="def", + ), + document_processor_service.ListProcessorsResponse( + processors=[processor.Processor(),], next_page_token="ghi", + ), + document_processor_service.ListProcessorsResponse( + processors=[processor.Processor(), processor.Processor(),], + ), + RuntimeError, + ) + pages = list(client.list_processors(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.asyncio +async def test_list_processors_async_pager(): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials, + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_processors), "__call__", new_callable=mock.AsyncMock + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + document_processor_service.ListProcessorsResponse( + processors=[ + processor.Processor(), + processor.Processor(), + processor.Processor(), + ], + next_page_token="abc", + ), + document_processor_service.ListProcessorsResponse( + processors=[], next_page_token="def", + ), + document_processor_service.ListProcessorsResponse( + processors=[processor.Processor(),], next_page_token="ghi", + ), + document_processor_service.ListProcessorsResponse( + processors=[processor.Processor(), processor.Processor(),], + ), + RuntimeError, + ) + async_pager = await client.list_processors(request={},) + assert async_pager.next_page_token == "abc" + responses = [] + async for response in async_pager: + responses.append(response) + + assert len(responses) == 6 + assert all(isinstance(i, processor.Processor) for i in responses) + + +@pytest.mark.asyncio +async def test_list_processors_async_pages(): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials, + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_processors), "__call__", new_callable=mock.AsyncMock + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + document_processor_service.ListProcessorsResponse( + processors=[ + processor.Processor(), + processor.Processor(), + processor.Processor(), + ], + next_page_token="abc", + ), + document_processor_service.ListProcessorsResponse( + processors=[], next_page_token="def", + ), + document_processor_service.ListProcessorsResponse( + processors=[processor.Processor(),], next_page_token="ghi", + ), + document_processor_service.ListProcessorsResponse( + processors=[processor.Processor(), processor.Processor(),], + ), + RuntimeError, + ) + pages = [] + async for page_ in (await client.list_processors(request={})).pages: + pages.append(page_) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +def test_create_processor( + transport: str = "grpc", + request_type=document_processor_service.CreateProcessorRequest, +): + client = DocumentProcessorServiceClient( + 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.create_processor), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = gcd_processor.Processor( + name="name_value", + type_="type__value", + display_name="display_name_value", + state=gcd_processor.Processor.State.ENABLED, + default_processor_version="default_processor_version_value", + process_endpoint="process_endpoint_value", + kms_key_name="kms_key_name_value", + ) + response = client.create_processor(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.CreateProcessorRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, gcd_processor.Processor) + assert response.name == "name_value" + assert response.type_ == "type__value" + assert response.display_name == "display_name_value" + assert response.state == gcd_processor.Processor.State.ENABLED + assert response.default_processor_version == "default_processor_version_value" + assert response.process_endpoint == "process_endpoint_value" + assert response.kms_key_name == "kms_key_name_value" + + +def test_create_processor_from_dict(): + test_create_processor(request_type=dict) + + +def test_create_processor_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 = DocumentProcessorServiceClient( + 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.create_processor), "__call__") as call: + client.create_processor() + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.CreateProcessorRequest() + + +@pytest.mark.asyncio +async def test_create_processor_async( + transport: str = "grpc_asyncio", + request_type=document_processor_service.CreateProcessorRequest, +): + client = DocumentProcessorServiceAsyncClient( + 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.create_processor), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + gcd_processor.Processor( + name="name_value", + type_="type__value", + display_name="display_name_value", + state=gcd_processor.Processor.State.ENABLED, + default_processor_version="default_processor_version_value", + process_endpoint="process_endpoint_value", + kms_key_name="kms_key_name_value", + ) + ) + response = await client.create_processor(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.CreateProcessorRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, gcd_processor.Processor) + assert response.name == "name_value" + assert response.type_ == "type__value" + assert response.display_name == "display_name_value" + assert response.state == gcd_processor.Processor.State.ENABLED + assert response.default_processor_version == "default_processor_version_value" + assert response.process_endpoint == "process_endpoint_value" + assert response.kms_key_name == "kms_key_name_value" + + +@pytest.mark.asyncio +async def test_create_processor_async_from_dict(): + await test_create_processor_async(request_type=dict) + + +def test_create_processor_field_headers(): + client = DocumentProcessorServiceClient( + 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_processor_service.CreateProcessorRequest() + + request.parent = "parent/value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_processor), "__call__") as call: + call.return_value = gcd_processor.Processor() + client.create_processor(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_create_processor_field_headers_async(): + client = DocumentProcessorServiceAsyncClient( + 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_processor_service.CreateProcessorRequest() + + request.parent = "parent/value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_processor), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + gcd_processor.Processor() + ) + await client.create_processor(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_create_processor_flattened(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_processor), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = gcd_processor.Processor() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.create_processor( + parent="parent_value", processor=gcd_processor.Processor(name="name_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] + assert args[0].parent == "parent_value" + assert args[0].processor == gcd_processor.Processor(name="name_value") + + +def test_create_processor_flattened_error(): + client = DocumentProcessorServiceClient( + 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.create_processor( + document_processor_service.CreateProcessorRequest(), + parent="parent_value", + processor=gcd_processor.Processor(name="name_value"), + ) + + +@pytest.mark.asyncio +async def test_create_processor_flattened_async(): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.create_processor), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = gcd_processor.Processor() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + gcd_processor.Processor() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.create_processor( + parent="parent_value", processor=gcd_processor.Processor(name="name_value"), + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0].parent == "parent_value" + assert args[0].processor == gcd_processor.Processor(name="name_value") + + +@pytest.mark.asyncio +async def test_create_processor_flattened_error_async(): + client = DocumentProcessorServiceAsyncClient( + 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.create_processor( + document_processor_service.CreateProcessorRequest(), + parent="parent_value", + processor=gcd_processor.Processor(name="name_value"), + ) + + +def test_delete_processor( + transport: str = "grpc", + request_type=document_processor_service.DeleteProcessorRequest, +): + client = DocumentProcessorServiceClient( + 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.delete_processor), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/spam") + response = client.delete_processor(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.DeleteProcessorRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) + + +def test_delete_processor_from_dict(): + test_delete_processor(request_type=dict) + + +def test_delete_processor_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 = DocumentProcessorServiceClient( + 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.delete_processor), "__call__") as call: + client.delete_processor() + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.DeleteProcessorRequest() + + +@pytest.mark.asyncio +async def test_delete_processor_async( + transport: str = "grpc_asyncio", + request_type=document_processor_service.DeleteProcessorRequest, +): + client = DocumentProcessorServiceAsyncClient( + 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.delete_processor), "__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.delete_processor(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.DeleteProcessorRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) + + +@pytest.mark.asyncio +async def test_delete_processor_async_from_dict(): + await test_delete_processor_async(request_type=dict) + + +def test_delete_processor_field_headers(): + client = DocumentProcessorServiceClient( + 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_processor_service.DeleteProcessorRequest() + + request.name = "name/value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_processor), "__call__") as call: + call.return_value = operations_pb2.Operation(name="operations/op") + client.delete_processor(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=name/value",) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_delete_processor_field_headers_async(): + client = DocumentProcessorServiceAsyncClient( + 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_processor_service.DeleteProcessorRequest() + + request.name = "name/value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_processor), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/op") + ) + await client.delete_processor(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", "name=name/value",) in kw["metadata"] + + +def test_delete_processor_flattened(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_processor), "__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.delete_processor(name="name_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] + assert args[0].name == "name_value" + + +def test_delete_processor_flattened_error(): + client = DocumentProcessorServiceClient( + 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.delete_processor( + document_processor_service.DeleteProcessorRequest(), name="name_value", + ) + + +@pytest.mark.asyncio +async def test_delete_processor_flattened_async(): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.delete_processor), "__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.delete_processor(name="name_value",) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0].name == "name_value" + + +@pytest.mark.asyncio +async def test_delete_processor_flattened_error_async(): + client = DocumentProcessorServiceAsyncClient( + 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.delete_processor( + document_processor_service.DeleteProcessorRequest(), name="name_value", + ) + + +def test_enable_processor( + transport: str = "grpc", + request_type=document_processor_service.EnableProcessorRequest, +): + client = DocumentProcessorServiceClient( + 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.enable_processor), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/spam") + response = client.enable_processor(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.EnableProcessorRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) + + +def test_enable_processor_from_dict(): + test_enable_processor(request_type=dict) + + +def test_enable_processor_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 = DocumentProcessorServiceClient( + 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.enable_processor), "__call__") as call: + client.enable_processor() + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.EnableProcessorRequest() + + +@pytest.mark.asyncio +async def test_enable_processor_async( + transport: str = "grpc_asyncio", + request_type=document_processor_service.EnableProcessorRequest, +): + client = DocumentProcessorServiceAsyncClient( + 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.enable_processor), "__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.enable_processor(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.EnableProcessorRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) + + +@pytest.mark.asyncio +async def test_enable_processor_async_from_dict(): + await test_enable_processor_async(request_type=dict) + + +def test_enable_processor_field_headers(): + client = DocumentProcessorServiceClient( + 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_processor_service.EnableProcessorRequest() + + request.name = "name/value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.enable_processor), "__call__") as call: + call.return_value = operations_pb2.Operation(name="operations/op") + client.enable_processor(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=name/value",) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_enable_processor_field_headers_async(): + client = DocumentProcessorServiceAsyncClient( + 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_processor_service.EnableProcessorRequest() + + request.name = "name/value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.enable_processor), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/op") + ) + await client.enable_processor(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", "name=name/value",) in kw["metadata"] + + +def test_disable_processor( + transport: str = "grpc", + request_type=document_processor_service.DisableProcessorRequest, +): + client = DocumentProcessorServiceClient( + 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.disable_processor), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/spam") + response = client.disable_processor(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.DisableProcessorRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) + + +def test_disable_processor_from_dict(): + test_disable_processor(request_type=dict) + + +def test_disable_processor_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 = DocumentProcessorServiceClient( + 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.disable_processor), "__call__" + ) as call: + client.disable_processor() + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.DisableProcessorRequest() + + +@pytest.mark.asyncio +async def test_disable_processor_async( + transport: str = "grpc_asyncio", + request_type=document_processor_service.DisableProcessorRequest, +): + client = DocumentProcessorServiceAsyncClient( + 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.disable_processor), "__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.disable_processor(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.DisableProcessorRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) + + +@pytest.mark.asyncio +async def test_disable_processor_async_from_dict(): + await test_disable_processor_async(request_type=dict) + + +def test_disable_processor_field_headers(): + client = DocumentProcessorServiceClient( + 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_processor_service.DisableProcessorRequest() + + request.name = "name/value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.disable_processor), "__call__" + ) as call: + call.return_value = operations_pb2.Operation(name="operations/op") + client.disable_processor(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=name/value",) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_disable_processor_field_headers_async(): + client = DocumentProcessorServiceAsyncClient( + 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_processor_service.DisableProcessorRequest() + + request.name = "name/value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.disable_processor), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/op") + ) + await client.disable_processor(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", "name=name/value",) in kw["metadata"] + + def test_review_document( transport: str = "grpc", request_type=document_processor_service.ReviewDocumentRequest, @@ -1277,6 +2611,12 @@ def test_document_processor_service_base_transport(): methods = ( "process_document", "batch_process_documents", + "fetch_processor_types", + "list_processors", + "create_processor", + "delete_processor", + "enable_processor", + "disable_processor", "review_document", ) for method in methods: @@ -1785,8 +3125,34 @@ def test_parse_processor_path(): assert expected == actual +def test_processor_type_path(): + project = "squid" + location = "clam" + processor_type = "whelk" + expected = "projects/{project}/locations/{location}/processorTypes/{processor_type}".format( + project=project, location=location, processor_type=processor_type, + ) + actual = DocumentProcessorServiceClient.processor_type_path( + project, location, processor_type + ) + assert expected == actual + + +def test_parse_processor_type_path(): + expected = { + "project": "octopus", + "location": "oyster", + "processor_type": "nudibranch", + } + path = DocumentProcessorServiceClient.processor_type_path(**expected) + + # Check that the path construction is reversible. + actual = DocumentProcessorServiceClient.parse_processor_type_path(path) + assert expected == actual + + def test_common_billing_account_path(): - billing_account = "squid" + billing_account = "cuttlefish" expected = "billingAccounts/{billing_account}".format( billing_account=billing_account, ) @@ -1796,7 +3162,7 @@ def test_common_billing_account_path(): def test_parse_common_billing_account_path(): expected = { - "billing_account": "clam", + "billing_account": "mussel", } path = DocumentProcessorServiceClient.common_billing_account_path(**expected) @@ -1806,7 +3172,7 @@ def test_parse_common_billing_account_path(): def test_common_folder_path(): - folder = "whelk" + folder = "winkle" expected = "folders/{folder}".format(folder=folder,) actual = DocumentProcessorServiceClient.common_folder_path(folder) assert expected == actual @@ -1814,7 +3180,7 @@ def test_common_folder_path(): def test_parse_common_folder_path(): expected = { - "folder": "octopus", + "folder": "nautilus", } path = DocumentProcessorServiceClient.common_folder_path(**expected) @@ -1824,7 +3190,7 @@ def test_parse_common_folder_path(): def test_common_organization_path(): - organization = "oyster" + organization = "scallop" expected = "organizations/{organization}".format(organization=organization,) actual = DocumentProcessorServiceClient.common_organization_path(organization) assert expected == actual @@ -1832,7 +3198,7 @@ def test_common_organization_path(): def test_parse_common_organization_path(): expected = { - "organization": "nudibranch", + "organization": "abalone", } path = DocumentProcessorServiceClient.common_organization_path(**expected) @@ -1842,7 +3208,7 @@ def test_parse_common_organization_path(): def test_common_project_path(): - project = "cuttlefish" + project = "squid" expected = "projects/{project}".format(project=project,) actual = DocumentProcessorServiceClient.common_project_path(project) assert expected == actual @@ -1850,7 +3216,7 @@ def test_common_project_path(): def test_parse_common_project_path(): expected = { - "project": "mussel", + "project": "clam", } path = DocumentProcessorServiceClient.common_project_path(**expected) @@ -1860,8 +3226,8 @@ def test_parse_common_project_path(): def test_common_location_path(): - project = "winkle" - location = "nautilus" + project = "whelk" + location = "octopus" expected = "projects/{project}/locations/{location}".format( project=project, location=location, ) @@ -1871,8 +3237,8 @@ def test_common_location_path(): def test_parse_common_location_path(): expected = { - "project": "scallop", - "location": "abalone", + "project": "oyster", + "location": "nudibranch", } path = DocumentProcessorServiceClient.common_location_path(**expected)