Skip to content

Commit d5dab1f

Browse files
authored
Document AI Warehouse: Document schema sample code (GoogleCloudPlatform#9934)
## Description PR contains document schema code samples create_document_schema_sample.py: To create a document schema in Document AI Warehouse get_document_schema_sample.py: To get a document schema based on document schema id list_document_schema_sample.py: To list the document schemas present in Document AI Warehouse delete_document_schema_sample.py: To delete a document schema from Document AI Warehouse ## Checklist - [x] I have followed [Sample Guidelines from AUTHORING_GUIDE.MD](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md) - [x] README is updated to include [all relevant information](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#readme-file) - [x] **Tests** pass: `nox -s py-3.9` (see [Test Environment Setup](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#test-environment-setup)) - [x] **Lint** pass: `nox -s lint` (see [Test Environment Setup](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#test-environment-setup)) - [ ] These samples need a new **API enabled** in testing projects to pass (let us know which ones) - [ ] These samples need a new/updated **env vars** in testing projects set to pass (let us know which ones) - [ ] This sample adds a new sample directory, and I updated the [CODEOWNERS file](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/.github/CODEOWNERS) with the codeowners for this sample - [ ] This sample adds a new **Product API**, and I updated the [Blunderbuss issue/PR auto-assigner](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/.github/blunderbuss.yml) with the codeowners for this sample - [x] Please **merge** this PR for me once it is approved
1 parent c0384c6 commit d5dab1f

7 files changed

Lines changed: 357 additions & 0 deletions

.github/auto-label.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ path:
3434
contact-center-insights: "contactcenterinsights"
3535
container: "container"
3636
container-analysis: "containeranalysis"
37+
contentwarehouse: "contentwarehouse"
3738
datacatalog: "datacatalog"
3839
datalabeling: "datalabeling"
3940
dataproc: "dataproc"
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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_schema]
18+
19+
from google.cloud import contentwarehouse
20+
21+
# TODO(developer): Uncomment these variables before running the sample.
22+
# project_number = 'YOUR_PROJECT_NUMBER'
23+
# location = 'YOUR_PROJECT_LOCATION' # Format is 'us' or 'eu'
24+
25+
26+
def sample_create_document_schema(project_number: str, location: str) -> None:
27+
"""Creates document schema.
28+
29+
Args:
30+
project_number: Google Cloud project number.
31+
location: Google Cloud project location.
32+
Returns:
33+
Response object.
34+
"""
35+
# Create a Schema Service client.
36+
document_schema_client = contentwarehouse.DocumentSchemaServiceClient()
37+
38+
property_definition = contentwarehouse.PropertyDefinition(
39+
name="stock_symbol", # Must be unique within a document schema (case insensitive)
40+
display_name="Searchable text",
41+
is_searchable=True,
42+
text_type_options=contentwarehouse.TextTypeOptions(),
43+
)
44+
# Initialize request argument(s)
45+
document_schema = contentwarehouse.DocumentSchema(
46+
display_name="My Test Schema",
47+
property_definitions=[property_definition],
48+
)
49+
50+
request = contentwarehouse.CreateDocumentSchemaRequest(
51+
# The full resource name of the location, e.g.:
52+
# projects/{project_number}/locations/{location}/
53+
parent=document_schema_client.common_location_path(project_number, location),
54+
document_schema=document_schema,
55+
)
56+
57+
# Make the request
58+
response = document_schema_client.create_document_schema(request=request)
59+
60+
# Print response
61+
print("Document Schema Created:", response)
62+
63+
return response
64+
65+
66+
# [END contentwarehouse_create_document_schema]
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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_schema_sample
19+
from contentwarehouse.snippets import delete_document_schema_sample
20+
from contentwarehouse.snippets import get_document_schema_sample
21+
from contentwarehouse.snippets import test_utilities
22+
23+
import pytest
24+
25+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
26+
location = "us"
27+
28+
29+
@pytest.mark.dependency(name="create")
30+
def test_create_document_schema(request: pytest.fixture) -> None:
31+
project_number = test_utilities.get_project_number(project_id)
32+
33+
response = create_document_schema_sample.sample_create_document_schema(
34+
project_number=project_number, location=location
35+
)
36+
37+
assert "display_name" in response
38+
39+
document_schema_id = response.name.split('/')[-1]
40+
41+
request.config.cache.set("document_schema_id", document_schema_id)
42+
43+
44+
@pytest.mark.dependency(name="get", depends=['create'])
45+
def test_get_document_schema(request: pytest.fixture) -> None:
46+
project_number = test_utilities.get_project_number(project_id)
47+
48+
document_schema_id = request.config.cache.get("document_schema_id", None)
49+
50+
response = get_document_schema_sample.sample_get_document_schema(
51+
project_number=project_number,
52+
location=location,
53+
document_schema_id=document_schema_id,
54+
)
55+
56+
assert "display_name" in response
57+
58+
59+
@pytest.mark.dependency(name="delete", depends=['get'])
60+
def test_delete_document_schema(request: pytest.fixture) -> None:
61+
project_number = test_utilities.get_project_number(project_id)
62+
63+
document_schema_id = request.config.cache.get("document_schema_id", None)
64+
65+
response = delete_document_schema_sample.sample_delete_document_schema(
66+
project_number=project_number,
67+
location=location,
68+
document_schema_id=document_schema_id,
69+
)
70+
71+
assert response is None
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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_schema]
18+
19+
from google.cloud import contentwarehouse
20+
21+
# TODO(developer): Uncomment these variables before running the sample.
22+
# project_number = 'YOUR_PROJECT_NUMBER'
23+
# location = 'YOUR_PROJECT_LOCATION' # Format is 'us' or 'eu'
24+
# document_schema_id = "YOUR_DOCUMENT SCHEMA_ID"
25+
26+
27+
def sample_delete_document_schema(
28+
project_number: str, location: str, document_schema_id: str
29+
) -> None:
30+
"""Deletes document schema.
31+
32+
Args:
33+
project_number: Google Cloud project number.
34+
location: Google Cloud project location.
35+
document_schema_id: Unique identifier for document schema
36+
Returns:
37+
None, if operation is successful
38+
"""
39+
# Create a client
40+
document_schema_client = contentwarehouse.DocumentSchemaServiceClient()
41+
42+
# The full resource name of the location, e.g.:
43+
# projects/{project_number}/locations/{location}/documentSchemas/{document_schema_id}
44+
document_schema_path = document_schema_client.document_schema_path(
45+
project=project_number,
46+
location=location,
47+
document_schema=document_schema_id,
48+
)
49+
50+
# Initialize request argument(s)
51+
request = contentwarehouse.DeleteDocumentSchemaRequest(
52+
name=document_schema_path,
53+
)
54+
55+
# Make the request
56+
response = document_schema_client.delete_document_schema(request=request)
57+
58+
return response
59+
60+
61+
# [END contentwarehouse_delete_document_schema]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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_schema]
18+
19+
from google.cloud import contentwarehouse
20+
21+
# TODO(developer): Uncomment these variables before running the sample.
22+
# project_number = 'YOUR_PROJECT_NUMBER'
23+
# location = 'YOUR_PROJECT_LOCATION' # Format is 'us' or 'eu'
24+
# document_schema_id = "YOUR_DOCUMENT SCHEMA_ID"
25+
26+
27+
def sample_get_document_schema(
28+
project_number: str, location: str, document_schema_id: str
29+
) -> None:
30+
"""Gets document schema details.
31+
32+
Args:
33+
project_number: Google Cloud project number.
34+
location: Google Cloud project location.
35+
document_schema_id: Unique identifier for document schema
36+
Returns:
37+
Response object.
38+
"""
39+
# Create a Schema Service client.
40+
document_schema_client = contentwarehouse.DocumentSchemaServiceClient()
41+
42+
# The full resource name of the location, e.g.:
43+
# projects/{project_number}/locations/{location}/documentSchemas/{document_schema_id}
44+
document_schema_path = document_schema_client.document_schema_path(
45+
project=project_number,
46+
location=location,
47+
document_schema=document_schema_id,
48+
)
49+
50+
# Initialize request argument(s)
51+
request = contentwarehouse.GetDocumentSchemaRequest(
52+
name=document_schema_path,
53+
)
54+
55+
# Make the request
56+
response = document_schema_client.get_document_schema(request=request)
57+
58+
# Handle the response
59+
print("Document Schema:", response)
60+
61+
return response
62+
63+
64+
# [END contentwarehouse_get_document_schema]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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_list_document_schemas]
18+
19+
from google.cloud import contentwarehouse
20+
21+
# TODO(developer): Uncomment these variables before running the sample.
22+
# project_number = 'YOUR_PROJECT_NUMBER'
23+
# location = 'YOUR_PROJECT_LOCATION' # Format is 'us' or 'eu'
24+
25+
26+
def sample_list_document_schemas(project_number: str, location: str) -> None:
27+
"""Lists document schemas.
28+
29+
Args:
30+
project_number: Google Cloud project number.
31+
location: Google Cloud project location.
32+
"""
33+
# Create a client
34+
document_schema_client = contentwarehouse.DocumentSchemaServiceClient()
35+
36+
# The full resource name of the location, e.g.:
37+
# projects/{project_number}/locations/{location}
38+
parent = document_schema_client.common_location_path(
39+
project=project_number, location=location
40+
)
41+
42+
# Initialize request argument(s)
43+
request = contentwarehouse.ListDocumentSchemasRequest(
44+
parent=parent,
45+
)
46+
47+
# Make the request
48+
page_result = document_schema_client.list_document_schemas(request=request)
49+
50+
# Print response
51+
responses = []
52+
print("Document Schemas:")
53+
for response in page_result:
54+
print(response)
55+
responses.append(response)
56+
57+
return responses
58+
59+
60+
# [END contentwarehouse_list_document_schemas]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 list_document_schema_sample
19+
from contentwarehouse.snippets import test_utilities
20+
21+
22+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
23+
location = "us"
24+
25+
26+
def test_list_document_schemas() -> None:
27+
project_number = test_utilities.get_project_number(project_id)
28+
29+
response = list_document_schema_sample.sample_list_document_schemas(
30+
project_number=project_number, location=location
31+
)
32+
33+
for schema in response:
34+
assert "display_name" in schema

0 commit comments

Comments
 (0)