Skip to content

Commit 9111110

Browse files
holtskinnerpartheagcf-owl-bot[bot]
authored
docs(samples): Updated Samples for v2.0.0 Client Library (#365)
* docs(samples): Updated Samples for v2.0.0 Client Library - Added Examples for FetchProcessorTypes, CreateProcessor, ListProcessors, DeleteProcessor, DisableProcessor, EnableProcessor - Added example parameters for processorVersion and FieldMask - Removed references to 'v1' in imports (default is v1) - Changed File types Documentation link in comments - Corrected Human Review test method name * docs(samples): Added Sample Code for Operations Functions * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * docs(samples): Added GetProcessor Sample - Also removed extra test function call * docs(samples): Addressed unit test issues * docs(samples): Adjusted imports to resovle presubmit errors Co-authored-by: Anthonios Partheniou <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 91bfeab commit 9111110

34 files changed

+989
-33
lines changed

documentai/snippets/batch_process_documents_sample.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
import re
1818

1919
from google.api_core.client_options import ClientOptions
20-
from google.cloud import documentai_v1 as documentai
21-
from google.cloud import storage
20+
from google.cloud import documentai, storage
2221

2322
# TODO(developer): Uncomment these variables before running the sample.
2423
# project_id = 'YOUR_PROJECT_ID'
2524
# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu'
26-
# processor_id = 'YOUR_PROCESSOR_ID' # Create processor in Cloud Console
25+
# processor_id = 'YOUR_PROCESSOR_ID' # Create processor before running sample
26+
# processor_version = "pretrained" # Optional. Processor version to use
2727
# gcs_input_uri = "YOUR_INPUT_URI" # Format: gs://bucket/directory/file.pdf
2828
# input_mime_type = "application/pdf"
2929
# gcs_output_bucket = "YOUR_OUTPUT_BUCKET_NAME" # Format: gs://bucket
@@ -76,6 +76,14 @@ def batch_process_documents(
7676
# You must create new processors in the Cloud Console first
7777
name = client.processor_path(project_id, location, processor_id)
7878

79+
# NOTE: Alternatively, specify the processor_version to specify a particular version of the processor to use
80+
# projects/{project_id}/locations/{location}/processors/{processor_id}/processorVersions/{processorVersion}
81+
#
82+
# name = client.processor_version_path(
83+
# project_id, location, processor_id, processor_version
84+
# )
85+
#
86+
7987
request = documentai.BatchProcessRequest(
8088
name=name,
8189
input_documents=input_config,

documentai/snippets/batch_process_documents_sample_bad_input_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ def test_batch_process_documents_with_bad_input(capsys):
4242
timeout=450,
4343
)
4444
out, _ = capsys.readouterr()
45-
assert "Failed to process" in out
45+
assert "Failed" in out
4646
except Exception as e:
47-
assert "Failed to process" in e.message
47+
assert "Internal error" in e.message
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
# [START documentai_cancel_operation]
17+
18+
from google.api_core.client_options import ClientOptions
19+
from google.api_core.exceptions import FailedPrecondition, NotFound
20+
from google.cloud import documentai
21+
from google.longrunning.operations_pb2 import CancelOperationRequest
22+
23+
# TODO(developer): Uncomment these variables before running the sample.
24+
# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu'
25+
# operation_name = 'YOUR_OPERATION_NAME' # Format is 'projects/project_id/locations/location/operations/operation_id'
26+
27+
28+
def cancel_operation_sample(location: str, operation_name: str):
29+
# You must set the api_endpoint if you use a location other than 'us', e.g.:
30+
opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
31+
32+
client = documentai.DocumentProcessorServiceClient(client_options=opts)
33+
34+
request = CancelOperationRequest(name=operation_name)
35+
36+
# Make CancelOperation request
37+
try:
38+
client.cancel_operation(request=request)
39+
print(f"Operation {operation_name} cancelled")
40+
except (FailedPrecondition, NotFound) as e:
41+
print(e.message)
42+
43+
44+
# [END documentai_cancel_operation]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
import os
17+
18+
from samples.snippets import cancel_operation_sample
19+
20+
location = "us"
21+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
22+
operation_id = "4311241022337572151"
23+
operation_name = f"projects/{project_id}/locations/{location}/operations/{operation_id}"
24+
25+
26+
def test_cancel_operation(capsys):
27+
cancel_operation_sample.cancel_operation_sample(
28+
location=location, operation_name=operation_name
29+
)
30+
out, _ = capsys.readouterr()
31+
32+
assert "Operation" in out
33+
assert "cancelled" in out
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
# [START documentai_create_processor]
17+
18+
from google.api_core.client_options import ClientOptions
19+
from google.cloud import documentai
20+
21+
# TODO(developer): Uncomment these variables before running the sample.
22+
# project_id = 'YOUR_PROJECT_ID'
23+
# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu'
24+
# processor_display_name = 'YOUR_PROCESSOR_DISPLAY_NAME' # Must be unique per project, e.g.: 'My Processor'
25+
# processor_type = 'YOUR_PROCESSOR_TYPE' # Use fetch_processor_types to get available processor types
26+
27+
28+
def create_processor_sample(
29+
project_id: str, location: str, processor_display_name: str, processor_type: str
30+
):
31+
# You must set the api_endpoint if you use a location other than 'us', e.g.:
32+
opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
33+
34+
client = documentai.DocumentProcessorServiceClient(client_options=opts)
35+
36+
# The full resource name of the location
37+
# e.g.: projects/project_id/locations/location
38+
parent = client.common_location_path(project_id, location)
39+
40+
# Create a processor
41+
processor = client.create_processor(
42+
parent=parent,
43+
processor=documentai.Processor(
44+
display_name=processor_display_name, type_=processor_type
45+
),
46+
)
47+
48+
# Print the processor information
49+
print(f"Processor Name: {processor.name}")
50+
print(f"Processor Display Name: {processor.display_name}")
51+
print(f"Processor Type: {processor.type_}")
52+
53+
54+
# [END documentai_create_processor]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
import os
17+
from uuid import uuid4
18+
19+
import mock
20+
21+
from samples.snippets import create_processor_sample
22+
23+
location = "us"
24+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
25+
processor_display_name = f"test-processor-{uuid4()}"
26+
processor_type = "OCR_PROCESSOR"
27+
28+
29+
@mock.patch("google.cloud.documentai.DocumentProcessorServiceClient.create_processor")
30+
@mock.patch("google.cloud.documentai.Processor")
31+
def test_create_processor(create_processor_mock, processor_mock, capsys):
32+
create_processor_mock.return_value = processor_mock
33+
34+
create_processor_sample.create_processor_sample(
35+
project_id=project_id,
36+
location=location,
37+
processor_display_name=processor_display_name,
38+
processor_type=processor_type,
39+
)
40+
41+
create_processor_mock.assert_called_once()
42+
43+
out, _ = capsys.readouterr()
44+
45+
assert "Processor Name:" in out
46+
assert "Processor Display Name:" in out
47+
assert "Processor Type:" in out
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
# [START documentai_delete_processor]
17+
18+
from google.api_core.client_options import ClientOptions
19+
from google.api_core.exceptions import NotFound
20+
from google.cloud import documentai
21+
22+
# TODO(developer): Uncomment these variables before running the sample.
23+
# project_id = 'YOUR_PROJECT_ID'
24+
# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu'
25+
# processor_id = 'YOUR_PROCESSOR_ID'
26+
27+
28+
def delete_processor_sample(project_id: str, location: str, processor_id: str):
29+
# You must set the api_endpoint if you use a location other than 'us', e.g.:
30+
opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
31+
32+
client = documentai.DocumentProcessorServiceClient(client_options=opts)
33+
34+
# The full resource name of the processor
35+
# e.g.: projects/project_id/locations/location/processors/processor_id
36+
processor_name = client.processor_path(project_id, location, processor_id)
37+
38+
# Delete a processor
39+
try:
40+
operation = client.delete_processor(name=processor_name)
41+
# Print operation details
42+
print(operation.operation.name)
43+
# Wait for operation to complete
44+
operation.result()
45+
except NotFound as e:
46+
print(e.message)
47+
48+
49+
# [END documentai_delete_processor]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
import os
17+
18+
import mock
19+
20+
from samples.snippets import delete_processor_sample
21+
22+
location = "us"
23+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
24+
processor_id = "aaaaaaaaa"
25+
parent = f"projects/{project_id}/locations/{location}/processors/{processor_id}"
26+
27+
28+
@mock.patch("google.cloud.documentai.DocumentProcessorServiceClient.delete_processor")
29+
@mock.patch("google.api_core.operation.Operation")
30+
def test_delete_processor(operation_mock, delete_processor_mock, capsys):
31+
delete_processor_mock.return_value = operation_mock
32+
33+
delete_processor_sample.delete_processor_sample(
34+
project_id=project_id, location=location, processor_id=processor_id
35+
)
36+
37+
delete_processor_mock.assert_called_once()
38+
39+
out, _ = capsys.readouterr()
40+
41+
assert "operation" in out
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
# [START documentai_disable_processor]
17+
18+
from google.api_core.client_options import ClientOptions
19+
from google.api_core.exceptions import FailedPrecondition
20+
from google.cloud import documentai
21+
22+
# TODO(developer): Uncomment these variables before running the sample.
23+
# project_id = 'YOUR_PROJECT_ID'
24+
# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu'
25+
# processor_id = 'YOUR_PROCESSOR_ID'
26+
27+
28+
def disable_processor_sample(project_id: str, location: str, processor_id: str):
29+
# You must set the api_endpoint if you use a location other than 'us', e.g.:
30+
opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
31+
32+
client = documentai.DocumentProcessorServiceClient(client_options=opts)
33+
34+
# The full resource name of the processor
35+
# e.g.: projects/project_id/locations/location/processors/processor_id
36+
processor_name = client.processor_path(project_id, location, processor_id)
37+
request = documentai.DisableProcessorRequest(name=processor_name)
38+
39+
# Make DisableProcessor request
40+
try:
41+
operation = client.disable_processor(request=request)
42+
43+
# Print operation name
44+
print(operation.operation.name)
45+
# Wait for operation to complete
46+
operation.result()
47+
# Cannot disable a processor that is already disabled
48+
except FailedPrecondition as e:
49+
print(e.message)
50+
51+
52+
# [END documentai_disable_processor]

0 commit comments

Comments
 (0)