|
| 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 | +'use strict'; |
| 17 | +async function main( |
| 18 | + projectNumber = 'YOUR_PROJECT_NUMBER', |
| 19 | + location = 'YOUR_PROJECT_LOCATION', |
| 20 | + userId = 'user:xxx@example.com', |
| 21 | +) { |
| 22 | + // [START contentwarehouse_quickstart] |
| 23 | + |
| 24 | + /** |
| 25 | + * TODO(developer): Uncomment these variables before running the sample. |
| 26 | + * const projectNumber = 'YOUR_PROJECT_NUMBER'; |
| 27 | + * const location = 'YOUR_PROJECT_LOCATION'; // Format is 'us' or 'eu' |
| 28 | + * const userId = 'user:xxx@example.com'; // Format is "user:xxx@example.com" |
| 29 | + */ |
| 30 | + |
| 31 | + // Import from google cloud |
| 32 | + const {DocumentSchemaServiceClient, DocumentServiceClient} = |
| 33 | + require('@google-cloud/contentwarehouse').v1; |
| 34 | + |
| 35 | + // Create service client |
| 36 | + const schemaClient = new DocumentSchemaServiceClient(); |
| 37 | + const serviceClient = new DocumentServiceClient(); |
| 38 | + |
| 39 | + // Get Document Schema |
| 40 | + async function quickstart() { |
| 41 | + // Initialize request argument(s) |
| 42 | + const schemaRequest = {}; |
| 43 | + const documentRequest = {}; |
| 44 | + |
| 45 | + // The full resource name of the location, e.g.: |
| 46 | + // projects/{project_number}/locations/{location} |
| 47 | + const parent = `projects/${projectNumber}/locations/${location}`; |
| 48 | + schemaRequest.parent = parent; |
| 49 | + documentRequest.parent = parent; |
| 50 | + |
| 51 | + // Property Definition |
| 52 | + const propertyDefinition = {}; |
| 53 | + propertyDefinition.name = 'schema property 1'; // Must be unique within a document schema (case insensitive) |
| 54 | + propertyDefinition.displayName = 'searchable text'; |
| 55 | + propertyDefinition.isSearchable = true; |
| 56 | + propertyDefinition.textTypeOptions = {}; |
| 57 | + |
| 58 | + // Document Schema |
| 59 | + const documentSchemaRequest = {}; |
| 60 | + documentSchemaRequest.displayName = 'My Test Schema'; |
| 61 | + documentSchemaRequest.propertyDefinitions = []; |
| 62 | + |
| 63 | + schemaRequest.documentSchema = documentSchemaRequest; |
| 64 | + |
| 65 | + // Create Document Schema |
| 66 | + const documentSchema = await schemaClient.createDocumentSchema(schemaRequest); |
| 67 | + |
| 68 | + // Property Value Definition |
| 69 | + const documentProperty = {}; |
| 70 | + documentProperty.name = propertyDefinition.name; |
| 71 | + documentProperty.values = {textValues: ["GOOG"]}; //TODO: how to get the right object type for this? |
| 72 | + |
| 73 | + // Document Definition |
| 74 | + const document = {}; |
| 75 | + document.displayName = 'My Test Document'; |
| 76 | + document.documentSchemaName = documentSchema[0].name; |
| 77 | + document.plainText = "This is a sample of a document's text."; |
| 78 | + document.properties = []; |
| 79 | + |
| 80 | + documentRequest.document = document; |
| 81 | + |
| 82 | + // Metadata Definition |
| 83 | + documentRequest.requestMetadata = {userInfo: {id: userId}}; |
| 84 | + |
| 85 | + // Make Request |
| 86 | + const response = serviceClient.createDocument(documentRequest); |
| 87 | + |
| 88 | + // Print out response |
| 89 | + response.then( |
| 90 | + result => console.log(`Document Created: ${JSON.stringify(result)}`), |
| 91 | + error => console.log(`${error}`) |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + // [END contentwarehouse_quickstart] |
| 96 | + await quickstart(); |
| 97 | +} |
| 98 | + |
| 99 | +main(...process.argv.slice(2)).catch(err => { |
| 100 | + console.error(err); |
| 101 | + process.exitCode = 1; |
| 102 | +}); |
0 commit comments