Skip to content

Commit

Permalink
docs(samples): add LRO code snippets (#209)
Browse files Browse the repository at this point in the history
* 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 <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and Ace Nassri committed Nov 17, 2022
1 parent 4992b1a commit 9921de2
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
54 changes: 54 additions & 0 deletions dialogflow-cx/long-running-operation.js
Original file line number Diff line number Diff line change
@@ -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));
62 changes: 62 additions & 0 deletions dialogflow-cx/test/long-running-operation.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});

0 comments on commit 9921de2

Please sign in to comment.