From 7bdedd1eb6feb266ebf4f663ce572a185883f024 Mon Sep 17 00:00:00 2001 From: Holt Skinner <13262395+holtskinner@users.noreply.github.com> Date: Sun, 27 Nov 2022 07:34:47 -0600 Subject: [PATCH] fix(samples): Fix Typos in Batch process & get processor Samples (#420) --- ...batch_process_documents_processor_version_sample.py | 7 ++++--- ..._process_documents_processor_version_sample_test.py | 2 +- samples/snippets/batch_process_documents_sample.py | 7 ++++--- .../batch_process_documents_sample_bad_input_test.py | 10 ++++++---- .../snippets/batch_process_documents_sample_test.py | 2 +- samples/snippets/get_processor_sample.py | 2 +- 6 files changed, 17 insertions(+), 13 deletions(-) diff --git a/samples/snippets/batch_process_documents_processor_version_sample.py b/samples/snippets/batch_process_documents_processor_version_sample.py index 1ef32afe..c8ed26c9 100644 --- a/samples/snippets/batch_process_documents_processor_version_sample.py +++ b/samples/snippets/batch_process_documents_processor_version_sample.py @@ -17,7 +17,7 @@ import re from google.api_core.client_options import ClientOptions -from google.api_core.exceptions import RetryError +from google.api_core.exceptions import InternalServerError, RetryError from google.cloud import documentai, storage # TODO(developer): Uncomment these variables before running the sample. @@ -66,7 +66,8 @@ def batch_process_documents_processor_version( # # Cloud Storage URI for the Output Directory - destination_uri = f"{gcs_output_bucket}/{gcs_output_uri_prefix}/" + # This must end with a trailing forward slash `/` + destination_uri = f"{gcs_output_bucket}/{gcs_output_uri_prefix}" gcs_output_config = documentai.DocumentOutputConfig.GcsOutputConfig( gcs_uri=destination_uri, field_mask=field_mask @@ -97,7 +98,7 @@ def batch_process_documents_processor_version( print(f"Waiting for operation {operation.operation.name} to complete...") operation.result(timeout=timeout) # Catch exception when operation doesn't finish before timeout - except (RetryError) as e: + except (RetryError, InternalServerError) as e: print(e.message) # NOTE: Can also use callbacks for asynchronous processing diff --git a/samples/snippets/batch_process_documents_processor_version_sample_test.py b/samples/snippets/batch_process_documents_processor_version_sample_test.py index 9eb33a0c..ded9f439 100644 --- a/samples/snippets/batch_process_documents_processor_version_sample_test.py +++ b/samples/snippets/batch_process_documents_processor_version_sample_test.py @@ -27,7 +27,7 @@ processor_version_id = "pretrained-form-parser-v1.0-2020-09-23" gcs_input_uri = "gs://cloud-samples-data/documentai/invoice.pdf" input_mime_type = "application/pdf" -gcs_output_uri_prefix = uuid4() +gcs_output_uri_prefix = f"{uuid4()}/" field_mask = "text,pages.pageNumber" BUCKET_NAME = f"document-ai-python-{uuid4()}" diff --git a/samples/snippets/batch_process_documents_sample.py b/samples/snippets/batch_process_documents_sample.py index 4c9c97a7..5e272e75 100644 --- a/samples/snippets/batch_process_documents_sample.py +++ b/samples/snippets/batch_process_documents_sample.py @@ -17,7 +17,7 @@ import re from google.api_core.client_options import ClientOptions -from google.api_core.exceptions import RetryError +from google.api_core.exceptions import InternalServerError, RetryError from google.cloud import documentai, storage # TODO(developer): Uncomment these variables before running the sample. @@ -64,7 +64,8 @@ def batch_process_documents( # # Cloud Storage URI for the Output Directory - destination_uri = f"{gcs_output_bucket}/{gcs_output_uri_prefix}/" + # This must end with a trailing forward slash `/` + destination_uri = f"{gcs_output_bucket}/{gcs_output_uri_prefix}" gcs_output_config = documentai.DocumentOutputConfig.GcsOutputConfig( gcs_uri=destination_uri, field_mask=field_mask @@ -93,7 +94,7 @@ def batch_process_documents( print(f"Waiting for operation {operation.operation.name} to complete...") operation.result(timeout=timeout) # Catch exception when operation doesn't finish before timeout - except (RetryError) as e: + except (RetryError, InternalServerError) as e: print(e.message) # NOTE: Can also use callbacks for asynchronous processing diff --git a/samples/snippets/batch_process_documents_sample_bad_input_test.py b/samples/snippets/batch_process_documents_sample_bad_input_test.py index a130f63d..78c40bd6 100644 --- a/samples/snippets/batch_process_documents_sample_bad_input_test.py +++ b/samples/snippets/batch_process_documents_sample_bad_input_test.py @@ -16,6 +16,7 @@ import os from uuid import uuid4 +from google.api_core.exceptions import InternalServerError, RetryError from samples.snippets import batch_process_documents_sample location = "us" @@ -25,7 +26,7 @@ input_mime_type = "application/pdf" # following bucket contains .csv file which will cause the sample to fail. gcs_output_full_uri_with_wrong_type = "gs://documentai-beta-samples" -gcs_output_uri_prefix = "test" +gcs_output_uri_prefix = "test/" BUCKET_NAME = f"document-ai-python-{uuid4()}" @@ -41,7 +42,8 @@ def test_batch_process_documents_with_bad_input(capsys): gcs_output_uri_prefix=gcs_output_uri_prefix, timeout=450, ) + except ValueError: out, _ = capsys.readouterr() - assert "Failed" in out - except Exception as e: - assert "Failed" in e.message + assert "Failed" in out or "error" in out + except (InternalServerError, RetryError) as e: + assert "error" in e.message diff --git a/samples/snippets/batch_process_documents_sample_test.py b/samples/snippets/batch_process_documents_sample_test.py index 5cca811b..7cf45006 100644 --- a/samples/snippets/batch_process_documents_sample_test.py +++ b/samples/snippets/batch_process_documents_sample_test.py @@ -26,7 +26,7 @@ processor_id = "90484cfdedb024f6" gcs_input_uri = "gs://cloud-samples-data/documentai/invoice.pdf" input_mime_type = "application/pdf" -gcs_output_uri_prefix = uuid4() +gcs_output_uri_prefix = f"{uuid4()}/" field_mask = "text,pages.pageNumber" BUCKET_NAME = f"document-ai-python-{uuid4()}" diff --git a/samples/snippets/get_processor_sample.py b/samples/snippets/get_processor_sample.py index 580b7e71..d4d91ee4 100644 --- a/samples/snippets/get_processor_sample.py +++ b/samples/snippets/get_processor_sample.py @@ -31,7 +31,7 @@ def get_processor_sample(project_id: str, location: str, processor_id: str): client = documentai.DocumentProcessorServiceClient(client_options=opts) # The full resource name of the processor, e.g.: - # projects/project_id/locations/location/processor/processor_id + # projects/{project_id}/locations/{location}/processors/{processor_id} name = client.processor_path(project_id, location, processor_id) # Make GetProcessor request