8000 feat(contentwarehouse): Document AI Warehouse - Add Manage Documents … · apilaskowski/python-docs-samples@138219c · GitHub
[go: up one dir, main page]

Skip to content

Commit 138219c

Browse files
dmoonatDeepak Moonatholtskinnerdandhleeengelke
authored
feat(contentwarehouse): Document AI Warehouse - Add Manage Documents code samples (GoogleCloudPlatform#10578)
* create ACL sample code * Fix snippet-bot check * changes as suggested * fetch acl parametrize tests * fetch acl parameterize tests * Update noxfile_config.py * Ran Black Formatter for lint * Update Request body for Document vs Project ACL * Addressed linter issue * changes as suggested * Update contentwarehouse/snippets/fetch_acl_sample.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * correct user_id format * test: Update exception handling to account for product issue * Document AI Warehouse: create Set ACL sample code * Add exception handling * Resolve lint and permission issue * changes as suggested * add policy type * resolve lint error * Document AI Warehouse: document schema sample code * snippet-bot-check resolve * chore: Ran black formatting to fix some lint errors * chore: Added Auto-label for `contentwarehouse` * fix lint * Ran black format * Changes as suggested * Fix lint * Fix lint * Update search sample * Update search test * Ran nox -s blacken * Replace metadata dictionary to RequestMetadata instance * create, delete document sample * Modify samples * Add get_document sample * Add test case * Update delete_document * Add update_document sample * Ran blacken lint * Add return types * update update_document sample * update Update_document sample and test * Rename document test file * Update user_id to get input from environment variable * correct pylint issue * fix lint * Changes suggested * Ran nox * Fix import order * changes suggested * changes suggested --------- Co-authored-by: Deepak Moonat <dmoonat@google.com> Co-authored-by: Holt Skinner <13262395+holtskinner@users.noreply.github.com> Co-authored-by: Holt Skinner <holtskinner@google.com> Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> Co-authored-by: Charles Engelke <engelke@google.com>
1 parent b965d49 commit 138219c

File tree

5 files changed

+381
-0
lines changed

5 files changed

+381
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Copyright 2023 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+
17+
# [START contentwarehouse_create_document]
18+
19+
from typing import Optional
20+
21+
from google.cloud import contentwarehouse
22+
23+
24+
def sample_create_document(
25+
project_number: str,
26+
location: str,
27+
raw_document_path: str,
28+
raw_document_file_type: contentwarehouse.RawDocumentFileType,
29+
document_schema_id: str,
30+
user_id: str,
31+
reference_id: Optional[str] = None,
32+
) -> contentwarehouse.CreateDocumentResponse:
33+
"""Creates a document.
34+
35+
Args:
36+
project_number: Google Cloud project number.
37+
location: Google Cloud project location.
38+
raw_document_path: Raw document file in Cloud Storage path.
39+
raw_document_file_type: Document file type
40+
https://cloud.google.com/python/docs/
41+
reference/contentwarehouse/latest/
42+
google.cloud.contentwarehouse_v1.types.RawDocumentFileType.
43+
document_schema_id: Unique identifier for document schema.
44+
user_id: user:YOUR_SERVICE_ACCOUNT_ID or user:USER_EMAIL.
45+
reference_id: Identifier, must be unique per project and location.
46+
Returns:
47+
Response object.
48+
"""
49+
# Create a Document Service client
50+
client = contentwarehouse.DocumentServiceClient()
51+
52+
# Get document schema name
53+
document_schema_name = client.document_schema_path(
54+
project=project_number, location=location, document_schema=document_schema_id
55+
)
56+
57+
# Initialize request argument(s)
58+
document = contentwarehouse.Document(
59+
raw_document_path=raw_document_path,
60+
display_name="Order Invoice",
61+
plain_text="Sample Invoice Document",
62+
raw_document_file_type=raw_document_file_type,
63+
document_schema_name=document_schema_name,
64+
reference_id=reference_id,
65+
)
66+
67+
request_metadata = contentwarehouse.RequestMetadata(
68+
user_info=contentwarehouse.UserInfo(id=user_id)
69+
)
70+
71+
parent = client.common_location_path(project=project_number, location=location)
72+
73+
request = contentwarehouse.CreateDocumentRequest(
74+
parent=parent,
75+
request_metadata=request_metadata,
76+
document=document,
77+
)
78+
79+
# Make the request
80+
response = client.create_document(request=request)
81+
82+
return response
83+
84+
85+
# [END contentwarehouse_create_document]
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# # Copyright 2023 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 contentwarehouse.snippets import create_document_sample
19+
from contentwarehouse.snippets import create_document_schema_sample
20+
from contentwarehouse.snippets import delete_document_sample
21+
from contentwarehouse.snippets import delete_document_schema_sample
22+
from contentwarehouse.snippets import get_document_sample
23+
from contentwarehouse.snippets import test_utilities
24+
from contentwarehouse.snippets import update_document_sample
25+
from google.cloud import contentwarehouse
26+
from google.cloud.contentwarehouse import Document
27+
28+
import pytest
29+
30+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
31+
location = "us"
32+
raw_document_path = (
33+
"gs://cloud-samples-data/documentai/codelabs/warehouse/order-invoice.pdf"
34+
)
35+
raw_document_file_type = contentwarehouse.RawDocumentFileType.RAW_DOCUMENT_FILE_TYPE_PDF
36+
user_id = os.environ["user_id"]
37+
reference_id = "001"
38+
39+
40+
@pytest.mark.dependency(name="create_schema")
41+
def test_create_document_schema(request: pytest.fixture) -> None:
42+
project_number = test_utilities.get_project_number(project_id)
43+
44+
response = create_document_schema_sample.sample_create_document_schema(
45+
project_number=project_number, location=location
46+
)
47+
48+
assert "display_name" in response
49+
50+
document_schema_id = response.name.split("/")[-1]
51+
52+
request.config.cache.set("document_schema_id", document_schema_id)
53+
54+
55+
@pytest.mark.dependency(name="create_doc", depends=["create_schema"])
56+
def test_create_document(request: pytest.fixture) -> None:
57+
project_number = test_utilities.get_project_number(project_id)
58+
59+
document_schema_id = request.config.cache.get("document_schema_id", None)
60+
61+
response = create_document_sample.sample_create_document(
62+
project_number=project_number,
63+
location=location,
64+
raw_document_path=raw_document_path,
65+
raw_document_file_type=raw_document_file_type,
66+
document_schema_id=document_schema_id,
67+
user_id=user_id,
68+
reference_id=reference_id,
69+
)
70+
71+
assert "document" in response
72+
73+
request.config.cache.set("document_name", response.document.name)
74+
75+
76+
@pytest.mark.dependency(name="get_doc", depends=["create_doc"])
77+
def test_get_document(request: pytest.fixture) -> None:
78+
document_name = request.config.cache.get("document_name", None)
79+
80+
response = get_document_sample.sample_get_document(
81+
document_name=document_name, user_id=user_id
82+
)
83+
84+
assert "name" in response
85+
request.config.cache.set(
86+
"document",
87+
Document.to_json(
88+
response,
89+
including_default_value_fields=False,
90+
preserving_proto_field_name=False,
91+
),
92+
)
93+
94+
95+
@pytest.mark.dependency(name="update_doc", depends=["get_doc"])
96+
def test_update_document(request: pytest.fixture) -> None:
97+
document_name = request.config.cache.get("document_name", None)
98+
document_json = request.config.cache.get("document", None)
99+
100+
document = Document.from_json(document_json)
101+
102+
response = update_document_sample.sample_update_document(
103+
document_name=document_name,
104+
document=document,
105+
user_id=user_id,
106+
)
107+
108+
assert "document" in response
109+
110+
111+
@pytest.mark.dependency(name="delete_doc", depends=["update_doc"])
112+
def test_delete_document(request: pytest.fixture) -> None:
113+
document_name = request.config.cache.get("document_name", None)
114+
115+
response = delete_document_sample.sample_delete_document(
116+
document_name=document_name, user_id=user_id
117+
)
118+
119+
assert response is None
120+
121+
122+
@pytest.mark.dependency(name="delete_schema", depends=["delete_doc"])
123+
def test_delete_document_schema(request: pytest.fixture) -> None:
124+
project_number = test_utilities.get_project_number(project_id)
125+
126+
document_schema_id = request.config.cache.get("document_schema_id", None)
127+
128+
response = delete_document_schema_sample.sample_delete_document_schema(
129+
project_number=project_number,
130+
location=location,
131+
document_schema_id=document_schema_id,
132+
)
133+
134+
assert response is None
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# # Copyright 2023 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+
17+
# [START contentwarehouse_delete_document]
18+
19+
from google.cloud import contentwarehouse
20+
21+
22+
def sample_delete_document(document_name: str, user_id: str) -> None:
23+
"""Deletes a document.
24+
25+
Args:
26+
document_name: The resource name of the document.
27+
Format: 'projects/{project_number}/
28+
locations/{location}/documents/{document_id}'.
29+
user_id: user:YOUR_SERVICE_ACCOUNT_ID or user:USER_EMAIL.
30+
Returns:
31+
None, if operation is successful.
32+
"""
33+
34+
# Create a client
35+
client = contentwarehouse.DocumentServiceClient()
36+
37+
request_metadata = contentwarehouse.RequestMetadata(
38+
user_info=contentwarehouse.UserInfo(id=user_id)
39+
)
40+
41+
# Initialize request argument(s)
42+
request = contentwarehouse.DeleteDocumentRequest(
43+
name=document_name, request_metadata=request_metadata
44+
)
45+
46+
# Make the request
47+
response = client.delete_document(request=request)
48+
49+
return response
50+
51+
52+
# [END contentwarehouse_delete_document]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# # Copyright 2023 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+
17+
# [START contentwarehouse_get_document]
18+
19+
from google.cloud import contentwarehouse
20+
21+
22+
def sample_get_document(document_name: str, user_id: str) -> contentwarehouse.Document:
23+
"""Gets a document.
24+
25+
Args:
26+
document_name: The resource name of the document.
27+
Format: 'projects/{project_number}/
28+
locations/{location}/documents/{document_id}'.
29+
user_id: user:YOUR_SERVICE_ACCOUNT_ID or user:USER_EMAIL.
30+
Returns:
31+
Response object
32+
"""
33+
# Create a client
34+
client = contentwarehouse.DocumentServiceClient()
35+
36+
request_metadata = contentwarehouse.RequestMetadata(
37+
user_info=contentwarehouse.UserInfo(id=user_id)
38+
)
39+
40+
# Initialize request argument(s)
41+
request = contentwarehouse.GetDocumentRequest(
42+
name=document_name, request_metadata=request_metadata
43+
)
44+
45+
# Make the request
46+
response = client.get_document(request=request)
47+
48+
return response
49+
50+
51+
# [END contentwarehouse_get_document]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright 2023 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+
17+
# [START contentwarehouse_update_document]
18+
19+
from google.cloud import contentwarehouse
20+
21+
22+
def sample_update_document(
23+
document_name: str, document: contentwarehouse.Document, user_id: str
24+
) -> contentwarehouse.CreateDocumentResponse:
25+
"""Updates a document.
26+
27+
Args:
28+
document_name: The resource name of the document.
29+
Format: 'projects/{project_number}/
30+
locations/{location}/documents/{document_id}'.
31+
document: Document AI Warehouse Document object..
32+
user_id: user_id: user:YOUR_SERVICE_ACCOUNT_ID or user:USER_EMAIL.
33+
Returns:
34+
Response object.
35+
"""
36+
# Create a client
37+
client = contentwarehouse.DocumentServiceClient()
38+
39+
# Update document fields
40+
# For fields which can be updated, refer
41+
# https://cloud.google.com/python/docs/reference/contentwarehouse/
42+
# latest/google.cloud.contentwarehouse_v1.types.Document
43+
document.display_name = "Updated Order Invoice"
44+
45+
request_metadata = contentwarehouse.RequestMetadata(
46+
user_info=contentwarehouse.UserInfo(id=user_id)
47+
)
48+
49+
request = contentwarehouse.UpdateDocumentRequest(
50+
name=document_name, document=document, request_metadata=request_metadata
51+
)
52+
53+
# Make the request
54+
response = client.update_document(request=request)
55+
56+
return response
57+
58+
59+
# [END contentwarehouse_update_document]

0 commit comments

Comments
 (0)
0