Skip to content

Commit ec75597

Browse files
galz10dandhlee
andauthored
docs(samples): Added LRO code snippet (#220)
* docs(samples): Added LRO code snippet * lint fix * converted export_agent response to string in test * Update samples/snippets/long_running_operation.py Co-authored-by: Dan Lee <[email protected]> * changed name of function Co-authored-by: Dan Lee <[email protected]>
1 parent 612b7eb commit ec75597

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2021 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+
""" DialogFlow CX long running operation code snippet """
17+
18+
## [START dialogflow_cx_long_running_snippet]
19+
from google.cloud.dialogflowcx_v3.services.agents.client import AgentsClient
20+
from google.cloud.dialogflowcx_v3.types.agent import ExportAgentRequest
21+
22+
23+
def export_long_running_agent(project_id, agent_id, location):
24+
25+
api_endpoint = f'{location}-dialogflow.googleapis.com:443'
26+
client_options = {"api_endpoint": api_endpoint}
27+
28+
agents_client = AgentsClient(client_options=client_options)
29+
30+
export_request = ExportAgentRequest()
31+
32+
export_request.name = f'projects/{project_id}/locations/{location}/agents/{agent_id}'
33+
34+
# export_agent returns a long running operation
35+
operation = agents_client.export_agent(request=export_request)
36+
37+
# Returns the result of the operation when the operation is done
38+
return operation.result()
39+
## [END dialogflow_cx_long_running_snippet]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Copyright 2021, Google LLC
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
import os
15+
import uuid
16+
17+
from google.cloud.dialogflowcx_v3.services.agents.client import AgentsClient
18+
from google.cloud.dialogflowcx_v3.types.agent import Agent, DeleteAgentRequest
19+
20+
import pytest
21+
22+
from long_running_operation import export_long_running_agent
23+
24+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
25+
pytest.AGENT_ID = None
26+
pytest.PARENT = None
27+
28+
29+
def create_agent(project_id, display_name):
30+
parent = "projects/" + project_id + "/locations/global"
31+
32+
agents_client = AgentsClient()
33+
34+
agent = Agent(
35+
display_name=display_name,
36+
default_language_code="en",
37+
time_zone="America/Los_Angeles",
38+
)
39+
40+
response = agents_client.create_agent(request={"agent": agent, "parent": parent})
41+
42+
return response
43+
44+
45+
def delete_agent(name):
46+
agents_client = AgentsClient()
47+
agent = DeleteAgentRequest(name=name)
48+
agents_client.delete_agent(request=agent)
49+
50+
51+
@pytest.fixture(scope="function", autouse=True)
52+
def setup_teardown():
53+
agentName = "temp_agent_" + str(uuid.uuid4())
54+
pytest.PARENT = create_agent(PROJECT_ID, agentName).name
55+
pytest.AGENT_ID = pytest.PARENT.split("/")[5]
56+
print("Created Agent in setUp")
57+
58+
yield
59+
60+
delete_agent(pytest.PARENT)
61+
62+
63+
def test_export_agent():
64+
actualResponse = export_long_running_agent(PROJECT_ID, pytest.AGENT_ID, "global")
65+
66+
assert pytest.AGENT_ID in str(actualResponse)

0 commit comments

Comments
 (0)