This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
0e0000e
commit 74f2249
Showing
34 changed files
with
989 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Copyright 2022 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. | ||
# | ||
|
||
# [START documentai_cancel_operation] | ||
|
||
from google.api_core.client_options import ClientOptions | ||
from google.api_core.exceptions import FailedPrecondition, NotFound | ||
from google.cloud import documentai | ||
from google.longrunning.operations_pb2 import CancelOperationRequest | ||
|
||
# TODO(developer): Uncomment these variables before running the sample. | ||
# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu' | ||
# operation_name = 'YOUR_OPERATION_NAME' # Format is 'projects/project_id/locations/location/operations/operation_id' | ||
|
||
|
||
def cancel_operation_sample(location: str, operation_name: str): | ||
# You must set the api_endpoint if you use a location other than 'us', e.g.: | ||
opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com") | ||
|
||
client = documentai.DocumentProcessorServiceClient(client_options=opts) | ||
|
||
request = CancelOperationRequest(name=operation_name) | ||
|
||
# Make CancelOperation request | ||
try: | ||
client.cancel_operation(request=request) | ||
print(f"Operation {operation_name} cancelled") | ||
except (FailedPrecondition, NotFound) as e: | ||
print(e.message) | ||
|
||
|
||
# [END documentai_cancel_operation] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
import os | ||
|
||
from samples.snippets import cancel_operation_sample | ||
|
||
location = "us" | ||
project_id = os.environ["GOOGLE_CLOUD_PROJECT"] | ||
operation_id = "4311241022337572151" | ||
operation_name = f"projects/{project_id}/locations/{location}/operations/{operation_id}" | ||
|
||
|
||
def test_cancel_operation(capsys): | ||
cancel_operation_sample.cancel_operation_sample( | ||
location=location, operation_name=operation_name | ||
) | ||
out, _ = capsys.readouterr() | ||
|
||
assert "Operation" in out | ||
assert "cancelled" in out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Copyright 2022 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. | ||
# | ||
|
||
# [START documentai_create_processor] | ||
|
||
from google.api_core.client_options import ClientOptions | ||
from google.cloud import documentai | ||
|
||
# TODO(developer): Uncomment these variables before running the sample. | ||
# project_id = 'YOUR_PROJECT_ID' | ||
# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu' | ||
# processor_display_name = 'YOUR_PROCESSOR_DISPLAY_NAME' # Must be unique per project, e.g.: 'My Processor' | ||
# processor_type = 'YOUR_PROCESSOR_TYPE' # Use fetch_processor_types to get available processor types | ||
|
||
|
||
def create_processor_sample( | ||
project_id: str, location: str, processor_display_name: str, processor_type: str | ||
): | ||
# You must set the api_endpoint if you use a location other than 'us', e.g.: | ||
opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com") | ||
|
||
client = documentai.DocumentProcessorServiceClient(client_options=opts) | ||
|
||
# The full resource name of the location | ||
# e.g.: projects/project_id/locations/location | ||
parent = client.common_location_path(project_id, location) | ||
|
||
# Create a processor | ||
processor = client.create_processor( | ||
parent=parent, | ||
processor=documentai.Processor( | ||
display_name=processor_display_name, type_=processor_type | ||
), | ||
) | ||
|
||
# Print the processor information | ||
print(f"Processor Name: {processor.name}") | ||
print(f"Processor Display Name: {processor.display_name}") | ||
print(f"Processor Type: {processor.type_}") | ||
|
||
|
||
# [END documentai_create_processor] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
import os | ||
from uuid import uuid4 | ||
|
||
import mock | ||
|
||
from samples.snippets import create_processor_sample | ||
|
||
location = "us" | ||
project_id = os.environ["GOOGLE_CLOUD_PROJECT"] | ||
processor_display_name = f"test-processor-{uuid4()}" | ||
processor_type = "OCR_PROCESSOR" | ||
|
||
|
||
@mock.patch("google.cloud.documentai.DocumentProcessorServiceClient.create_processor") | ||
@mock.patch("google.cloud.documentai.Processor") | ||
def test_create_processor(create_processor_mock, processor_mock, capsys): | ||
create_processor_mock.return_value = processor_mock | ||
|
||
create_processor_sample.create_processor_sample( | ||
project_id=project_id, | ||
location=location, | ||
processor_display_name=processor_display_name, | ||
processor_type=processor_type, | ||
) | ||
|
||
create_processor_mock.assert_called_once() | ||
|
||
out, _ = capsys.readouterr() | ||
|
||
assert "Processor Name:" in out | ||
assert "Processor Display Name:" in out | ||
assert "Processor Type:" in out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Copyright 2022 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. | ||
# | ||
|
||
# [START documentai_delete_processor] | ||
|
||
from google.api_core.client_options import ClientOptions | ||
from google.api_core.exceptions import NotFound | ||
from google.cloud import documentai | ||
|
||
# TODO(developer): Uncomment these variables before running the sample. | ||
# project_id = 'YOUR_PROJECT_ID' | ||
# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu' | ||
# processor_id = 'YOUR_PROCESSOR_ID' | ||
|
||
|
||
def delete_processor_sample(project_id: str, location: str, processor_id: str): | ||
# You must set the api_endpoint if you use a location other than 'us', e.g.: | ||
opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com") | ||
|
||
client = documentai.DocumentProcessorServiceClient(client_options=opts) | ||
|
||
# The full resource name of the processor | ||
# e.g.: projects/project_id/locations/location/processors/processor_id | ||
processor_name = client.processor_path(project_id, location, processor_id) | ||
|
||
# Delete a processor | ||
try: | ||
operation = client.delete_processor(name=processor_name) | ||
# Print operation details | ||
print(operation.operation.name) | ||
# Wait for operation to complete | ||
operation.result() | ||
except NotFound as e: | ||
print(e.message) | ||
|
||
|
||
# [END documentai_delete_processor] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
import os | ||
|
||
import mock | ||
|
||
from samples.snippets import delete_processor_sample | ||
|
||
location = "us" | ||
project_id = os.environ["GOOGLE_CLOUD_PROJECT"] | ||
processor_id = "aaaaaaaaa" | ||
parent = f"projects/{project_id}/locations/{location}/processors/{processor_id}" | ||
|
||
|
||
@mock.patch("google.cloud.documentai.DocumentProcessorServiceClient.delete_processor") | ||
@mock.patch("google.api_core.operation.Operation") | ||
def test_delete_processor(operation_mock, delete_processor_mock, capsys): | ||
delete_processor_mock.return_value = operation_mock | ||
|
||
delete_processor_sample.delete_processor_sample( | ||
project_id=project_id, location=location, processor_id=processor_id | ||
) | ||
|
||
delete_processor_mock.assert_called_once() | ||
|
||
out, _ = capsys.readouterr() | ||
|
||
assert "operation" in out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Copyright 2022 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. | ||
# | ||
|
||
# [START documentai_disable_processor] | ||
|
||
from google.api_core.client_options import ClientOptions | ||
from google.api_core.exceptions import FailedPrecondition | ||
from google.cloud import documentai | ||
|
||
# TODO(developer): Uncomment these variables before running the sample. | ||
# project_id = 'YOUR_PROJECT_ID' | ||
# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu' | ||
# processor_id = 'YOUR_PROCESSOR_ID' | ||
|
||
|
||
def disable_processor_sample(project_id: str, location: str, processor_id: str): | ||
# You must set the api_endpoint if you use a location other than 'us', e.g.: | ||
opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com") | ||
|
||
client = documentai.DocumentProcessorServiceClient(client_options=opts) | ||
|
||
# The full resource name of the processor | ||
# e.g.: projects/project_id/locations/location/processors/processor_id | ||
processor_name = client.processor_path(project_id, location, processor_id) | ||
request = documentai.DisableProcessorRequest(name=processor_name) | ||
|
||
# Make DisableProcessor request | ||
try: | ||
operation = client.disable_processor(request=request) | ||
|
||
# Print operation name | ||
print(operation.operation.name) | ||
# Wait for operation to complete | ||
operation.result() | ||
# Cannot disable a processor that is already disabled | ||
except FailedPrecondition as e: | ||
print(e.message) | ||
|
||
|
||
# [END documentai_disable_processor] |
Oops, something went wrong.