-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3507 from GoogleCloudPlatform/create-doc
feat: Create document sample
- Loading branch information
Showing
4 changed files
with
167 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** Copyright 2023 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. | ||
*/ | ||
|
||
'use strict'; | ||
async function main( | ||
projectNumber = 'YOUR_PROJECT_NUMBER', | ||
location = 'YOUR_PROJECT_LOCATION', | ||
userId = 'user:xxx@example.com' | ||
) { | ||
// [START contentwarehouse_quickstart] | ||
|
||
/** | ||
* TODO(developer): Uncomment these variables before running the sample. | ||
* const projectNumber = 'YOUR_PROJECT_NUMBER'; | ||
* const location = 'YOUR_PROJECT_LOCATION'; // Format is 'us' or 'eu' | ||
* const userId = 'user:xxx@example.com'; // Format is "user:xxx@example.com" | ||
*/ | ||
|
||
// Import from google cloud | ||
const {DocumentSchemaServiceClient, DocumentServiceClient} = | ||
require('@google-cloud/contentwarehouse').v1; | ||
|
||
// Create service client | ||
const schemaClient = new DocumentSchemaServiceClient(); | ||
const serviceClient = new DocumentServiceClient(); | ||
|
||
// Get Document Schema | ||
async function quickstart() { | ||
// Initialize request argument(s) | ||
const schemaRequest = {}; | ||
const documentRequest = {}; | ||
|
||
// The full resource name of the location, e.g.: | ||
// projects/{project_number}/locations/{location} | ||
const parent = `projects/${projectNumber}/locations/${location}`; | ||
schemaRequest.parent = parent; | ||
documentRequest.parent = parent; | ||
|
||
// Property Definition | ||
const propertyDefinition = {}; | ||
propertyDefinition.name = 'testPropertyDefinitionName'; // Must be unique within a document schema (case insensitive) | ||
propertyDefinition.displayName = 'searchable text'; | ||
propertyDefinition.isSearchable = true; | ||
propertyDefinition.textTypeOptions = {}; | ||
|
||
// Document Schema | ||
const documentSchemaRequest = {}; | ||
documentSchemaRequest.displayName = 'My Test Schema'; | ||
documentSchemaRequest.propertyDefinitions = [propertyDefinition]; | ||
|
||
schemaRequest.documentSchema = documentSchemaRequest; | ||
|
||
// Create Document Schema | ||
const documentSchema = | ||
await schemaClient.createDocumentSchema(schemaRequest); | ||
|
||
// Property Value Definition | ||
const documentProperty = {}; | ||
documentProperty.name = propertyDefinition.name; | ||
documentProperty.textValues = {values: ['GOOG']}; | ||
|
||
// Document Definition | ||
const document = {}; | ||
document.displayName = 'My Test Document'; | ||
document.documentSchemaName = documentSchema[0].name; | ||
document.plainText = "This is a sample of a document's text."; | ||
document.properties = [documentProperty]; | ||
|
||
documentRequest.document = document; | ||
|
||
// Metadata Definition | ||
documentRequest.requestMetadata = {userInfo: {id: userId}}; | ||
|
||
// Make Request | ||
const response = serviceClient.createDocument(documentRequest); | ||
|
||
// Print out response | ||
response.then( | ||
result => console.log(`Document Created: ${JSON.stringify(result)}`), | ||
error => console.log(`${error}`) | ||
); | ||
} | ||
|
||
// [END contentwarehouse_quickstart] | ||
await quickstart(); | ||
} | ||
|
||
main(...process.argv.slice(2)).catch(err => { | ||
console.error(err); | ||
process.exitCode = 1; | ||
}); |
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,56 @@ | ||
// Copyright 2023 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 | ||
// | ||
// https://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. | ||
|
||
'use strict'; | ||
|
||
const {describe, it} = require('mocha'); | ||
const assert = require('assert'); | ||
const cp = require('child_process'); | ||
|
||
const {PoliciesClient} = require('@google-cloud/iam').v2; | ||
const {ProjectsClient} = require('@google-cloud/resource-manager').v3; | ||
const iamClient = new PoliciesClient(); | ||
const projectClient = new ProjectsClient(); | ||
|
||
const confirmationCreate = 'Document Created'; | ||
|
||
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); | ||
|
||
describe('Document tests', () => { | ||
let projectNumber; | ||
const location = 'us'; | ||
const userId = | ||
'user:kokoro-system-test@long-door-651.iam.gserviceaccount.com'; | ||
|
||
async function getProjectNumber() { | ||
const projectId = await iamClient.getProjectId(); | ||
const request = {name: `projects/${projectId}`}; | ||
const project = await projectClient.getProject(request); | ||
const resources = project[0].name.toString().split('/'); | ||
const projectNumber = resources[resources.length - 1]; | ||
return projectNumber; | ||
} | ||
|
||
before(async () => { | ||
projectNumber = await getProjectNumber(); | ||
}); | ||
|
||
it('should create a document', async () => { | ||
const output = execSync( | ||
`node create-document.js ${projectNumber} ${location} ${userId}` | ||
); | ||
|
||
assert(output.startsWith(confirmationCreate)); | ||
}); | ||
}); |