|
| 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_folder_link_document] |
| 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 = "us" # Format is 'us' or 'eu' |
| 24 | +# user_id = "user:xxxx@example.com" # Format is "user:xxxx@example.com" |
| 25 | + |
| 26 | + |
| 27 | +def create_folder( |
| 28 | + project_number: str, location: str, user_id: str |
| 29 | +) -> contentwarehouse.Document: |
| 30 | + # Create a Schema Service client |
| 31 | + document_schema_client = contentwarehouse.DocumentSchemaServiceClient() |
| 32 | + |
| 33 | + # The full resource name of the location, e.g.: |
| 34 | + # projects/{project_number}/locations/{location} |
| 35 | + parent = document_schema_client.common_location_path( |
| 36 | + project=project_number, location=location |
| 37 | + ) |
| 38 | + |
| 39 | + # Define Folder Schema Request |
| 40 | + create_folder_schema_request = contentwarehouse.CreateDocumentSchemaRequest( |
| 41 | + parent=parent, |
| 42 | + document_schema=contentwarehouse.DocumentSchema( |
| 43 | + display_name="Test Folder Schema ", |
| 44 | + document_is_folder=True, |
| 45 | + ), |
| 46 | + ) |
| 47 | + |
| 48 | + # Create a Folder Schema |
| 49 | + folder_schema = document_schema_client.create_document_schema( |
| 50 | + request=create_folder_schema_request |
| 51 | + ) |
| 52 | + |
| 53 | + # Create a Document(Folder) Service client |
| 54 | + folder_client = contentwarehouse.DocumentServiceClient() |
| 55 | + |
| 56 | + # Define Folder |
| 57 | + folder = contentwarehouse.Document( |
| 58 | + display_name="My Test Folder", |
| 59 | + document_schema_name=folder_schema.name, |
| 60 | + ) |
| 61 | + |
| 62 | + # Define Request to create Folder |
| 63 | + create_folder_request = contentwarehouse.CreateDocumentRequest( |
| 64 | + parent=parent, |
| 65 | + document=folder, |
| 66 | + request_metadata=contentwarehouse.RequestMetadata( |
| 67 | + user_info=contentwarehouse.UserInfo(id=user_id) |
| 68 | + ), |
| 69 | + ) |
| 70 | + |
| 71 | + # Create a Folder for the given schema |
| 72 | + folder_response = folder_client.create_document(request=create_folder_request) |
| 73 | + |
| 74 | + # Read the output |
| 75 | + print(f"Rule Engine Output: {folder_response.rule_engine_output}") |
| 76 | + print(f"Folder Created: {folder_response.document}") |
| 77 | + |
| 78 | + return folder_response |
| 79 | + |
| 80 | + |
| 81 | +def create_document( |
| 82 | + project_number: str, location: str, user_id: str |
| 83 | +) -> contentwarehouse.Document: |
| 84 | + # Create a Schema Service client |
| 85 | + document_schema_client = contentwarehouse.DocumentSchemaServiceClient() |
| 86 | + |
| 87 | + # The full resource name of the location, e.g.: |
| 88 | + # projects/{project_number}/locations/{location} |
| 89 | + parent = document_schema_client.common_location_path( |
| 90 | + project=project_number, location=location |
| 91 | + ) |
| 92 | + |
| 93 | + # Define Schema Property of Text Type |
| 94 | + property_definition = contentwarehouse.PropertyDefinition( |
| 95 | + name="stock_symbol", # Must be unique within a document schema (case insensitive) |
| 96 | + display_name="Searchable text", |
| 97 | + is_searchable=True, |
| 98 | + text_type_options=contentwarehouse.TextTypeOptions(), |
| 99 | + ) |
| 100 | + |
| 101 | + # Define Document Schema Request |
| 102 | + create_document_schema_request = contentwarehouse.CreateDocumentSchemaRequest( |
| 103 | + parent=parent, |
| 104 | + document_schema=contentwarehouse.DocumentSchema( |
| 105 | + display_name="My Test Schema", |
| 106 | + property_definitions=[property_definition], |
| 107 | + ), |
| 108 | + ) |
| 109 | + |
| 110 | + # Create a Document schema |
| 111 | + document_schema = document_schema_client.create_document_schema( |
| 112 | + request=create_document_schema_request |
| 113 | + ) |
| 114 | + |
| 115 | + # Create a Document Service client |
| 116 | + document_client = contentwarehouse.DocumentServiceClient() |
| 117 | + |
| 118 | + # The full resource name of the location, e.g.: |
| 119 | + # projects/{project_number}/locations/{location} |
| 120 | + parent = document_client.common_location_path( |
| 121 | + project=project_number, location=location |
| 122 | + ) |
| 123 | + |
| 124 | + # Define Document Property Value |
| 125 | + document_property = contentwarehouse.Property( |
| 126 | + name=document_schema.property_definitions[0].name, |
| 127 | + text_values=contentwarehouse.TextArray(values=["GOOG"]), |
| 128 | + ) |
| 129 | + |
| 130 | + # Define Document |
| 131 | + document = contentwarehouse.Document( |
| 132 | + display_name="My Test Document", |
| 133 | + document_schema_name=document_schema.name, |
| 134 | + plain_text="This is a sample of a document's text.", |
| 135 | + properties=[document_property], |
| 136 | + ) |
| 137 | + |
| 138 | + # Define Request |
| 139 | + create_document_request = contentwarehouse.CreateDocumentRequest( |
| 140 | + parent=parent, |
| 141 | + document=document, |
| 142 | + request_metadata=contentwarehouse.RequestMetadata( |
| 143 | + user_info=contentwarehouse.UserInfo(id=user_id) |
| 144 | + ), |
| 145 | + ) |
| 146 | + |
| 147 | + # Create a Document for the given schema |
| 148 | + document_response = document_client.create_document(request=create_document_request) |
| 149 | + |
| 150 | + # Read the output |
| 151 | + print(f"Rule Engine Output: {document_response.rule_engine_output}") |
| 152 | + print(f"Document Created: {document_response.document}") |
| 153 | + |
| 154 | + return document_response |
| 155 | + |
| 156 | + |
| 157 | +def create_folder_link_document( |
| 158 | + project_number: str, location: str, user_id: str |
| 159 | +) -> None: |
| 160 | + # Function call to create a folder |
| 161 | + folder = create_folder(project_number, location, user_id) |
| 162 | + |
| 163 | + # Function call to create a Document |
| 164 | + document = create_document(project_number, location, user_id) |
| 165 | + |
| 166 | + # Create a Link Service client |
| 167 | + link_client = contentwarehouse.DocumentLinkServiceClient() |
| 168 | + |
| 169 | + # Initialize request argument(s) |
| 170 | + link = contentwarehouse.DocumentLink( |
| 171 | + source_document_reference=contentwarehouse.DocumentReference( |
| 172 | + document_name=folder.document.name, |
| 173 | + ), |
| 174 | + target_document_reference=contentwarehouse.DocumentReference( |
| 175 | + document_name=document.document.name, |
| 176 | + ), |
| 177 | + ) |
| 178 | + |
| 179 | + # Initialize document link request |
| 180 | + create_document_link_request = contentwarehouse.CreateDocumentLinkRequest( |
| 181 | + parent=folder.document.name, |
| 182 | + document_link=link, |
| 183 | + request_metadata=contentwarehouse.RequestMetadata( |
| 184 | + user_info=contentwarehouse.UserInfo(id=user_id) |
| 185 | + ), |
| 186 | + ) |
| 187 | + |
| 188 | + # Make the Document Link request |
| 189 | + create_link_response = link_client.create_document_link( |
| 190 | + request=create_document_link_request |
| 191 | + ) |
| 192 | + |
| 193 | + print(f"Link Created: {create_link_response}") |
| 194 | + |
| 195 | + # Initialize list linked targets request |
| 196 | + linked_targets_request = contentwarehouse.ListLinkedTargetsRequest( |
| 197 | + parent=folder.document.name, |
| 198 | + request_metadata=contentwarehouse.RequestMetadata( |
| 199 | + user_info=contentwarehouse.UserInfo(id=user_id) |
| 200 | + ), |
| 201 | + ) |
| 202 | + |
| 203 | + # Make the request |
| 204 | + linked_targets_response = link_client.list_linked_targets( |
| 205 | + request=linked_targets_request |
| 206 | + ) |
| 207 | + |
| 208 | + print(f"Validate Link Created: {linked_targets_response}") |
| 209 | + |
| 210 | + |
| 211 | +# [END contentwarehouse_create_folder_link_document] |
0 commit comments