From 9921de2ec3608091ffb25bc6b9f629f578bda2f2 Mon Sep 17 00:00:00 2001 From: Gal Zahavi <38544478+galz10@users.noreply.github.com> Date: Thu, 6 Jan 2022 10:12:37 -0800 Subject: [PATCH] docs(samples): add LRO code snippets (#209) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * docs(samples): add LRO code snippets * lint fix * lint fix * lint fix * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot --- dialogflow-cx/long-running-operation.js | 54 ++++++++++++++++ .../test/long-running-operation.test.js | 62 +++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 dialogflow-cx/long-running-operation.js create mode 100644 dialogflow-cx/test/long-running-operation.test.js diff --git a/dialogflow-cx/long-running-operation.js b/dialogflow-cx/long-running-operation.js new file mode 100644 index 0000000000..020a7a2e25 --- /dev/null +++ b/dialogflow-cx/long-running-operation.js @@ -0,0 +1,54 @@ +// Copyright 2021 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'; + +async function main(projectId, agentId, location) { + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project'; + // const agentId = 'my-agent'; + // const location = 'global'; + + // [START dialogflow_cx_log_running_operation] + + const {AgentsClient, protos} = require('@google-cloud/dialogflow-cx'); + + const api_endpoint = `${location}-dialogflow.googleapis.com`; + + const client = new AgentsClient({apiEndpoint: api_endpoint}); + + const exportAgentRequest = + new protos.google.cloud.dialogflow.cx.v3.ExportAgentRequest(); + + exportAgentRequest.name = `projects/${projectId}/locations/${location}/agents/${agentId}`; + + // exportAgent call returns a promise to a long running operation + const [operation] = await client.exportAgent(exportAgentRequest); + + // Waiting for the long running opporation to finish + const [response] = await operation.promise(); + + // Prints the result of the operation when the operation is done + console.log(response); + + // [END dialogflow_cx_log_running_operation] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/dialogflow-cx/test/long-running-operation.test.js b/dialogflow-cx/test/long-running-operation.test.js new file mode 100644 index 0000000000..9c60c04faa --- /dev/null +++ b/dialogflow-cx/test/long-running-operation.test.js @@ -0,0 +1,62 @@ +// Copyright 2021 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 {assert} = require('chai'); +const {describe, before, it, after} = require('mocha'); +const execSync = require('child_process').execSync; +const uuid = require('uuid'); +const dialogflow = require('@google-cloud/dialogflow-cx'); +const exec = cmd => execSync(cmd, {encoding: 'utf8'}); +const location = 'global'; +let agentId = ''; +let agentPath = ''; + +describe('update intent', async () => { + const agentClient = new dialogflow.AgentsClient(); + const projectId = await agentClient.getProjectId(); + const agentDisplayName = `temp_agent_${uuid.v4().split('-')[0]}`; + const parent = 'projects/' + projectId + '/locations/' + location; + const cmd = 'node long-running-operation.js'; + + before('create an agent and get agent id', async () => { + // The path to identify the agent that owns the intents. + + const agent = { + displayName: agentDisplayName, + defaultLanguageCode: 'en', + timeZone: 'America/Los_Angeles', + }; + + const request = { + agent, + parent, + }; + + const [agentResponse] = await agentClient.createAgent(request); + + agentPath = agentResponse.name; + agentId = agentPath.split('/')[5]; + }); + + after('delete Agent', async () => { + agentClient.deleteAgent({name: agentPath}); + }); + + it('should export agent', async () => { + const output = exec(`${cmd} ${projectId} ${agentId} ${location}`); + assert.include(output, 'agentContent'); + }); +});