Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

docs(samples): added page management samples #152

Merged
merged 33 commits into from Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
6b7410e
docs(samples): added page management samples
galz10 Aug 23, 2021
cb2c864
Merge branch 'master' into page-management
galz10 Aug 23, 2021
99b596d
fixed failing test
galz10 Aug 23, 2021
01fb5ba
Merge branch 'page-management' of github.com:galz10/python-dialogflow…
galz10 Aug 23, 2021
5d09ec6
Remvoe await
galz10 Aug 23, 2021
11c8fcf
added await
galz10 Aug 23, 2021
e3329be
added pytest async
galz10 Aug 23, 2021
9fda01d
Lint fix
galz10 Aug 23, 2021
c264d63
Lint and test case fix
galz10 Aug 24, 2021
4a0f012
Merge branch 'main' into page-management
galz10 Aug 25, 2021
15a22ef
removed pytest.async
galz10 Aug 25, 2021
ffd4dec
Merge branch 'page-management' of github.com:galz10/python-dialogflow…
galz10 Aug 25, 2021
8aff217
test fix
galz10 Aug 25, 2021
d49bec9
update tests
galz10 Aug 26, 2021
44bb451
fix uuid
galz10 Aug 26, 2021
a0d5165
added asyncio
galz10 Aug 26, 2021
17d6b60
added loop
galz10 Aug 26, 2021
790e55c
removed async
galz10 Aug 26, 2021
00bce98
test fix
galz10 Aug 26, 2021
a7ef32a
changed pytest scope to session
galz10 Aug 26, 2021
83fbed1
changes fixture scope
galz10 Aug 26, 2021
d263f88
test change
galz10 Aug 26, 2021
ef22679
test fix
galz10 Aug 26, 2021
65fef0b
changed response type
galz10 Aug 26, 2021
cde39b6
fixed list page test
galz10 Aug 26, 2021
27512fc
lint fix
galz10 Aug 26, 2021
b157b62
Merge branch 'main' into page-management
galz10 Aug 31, 2021
ebf75a5
Merge branch 'main' into page-management
parthea Sep 1, 2021
08b4645
failing test fix
galz10 Sep 1, 2021
3f957d2
Merge branch 'page-management' of github.com:galz10/python-dialogflow…
galz10 Sep 1, 2021
a8f662f
revised code per comments
galz10 Sep 1, 2021
621c60a
converted object to string
galz10 Sep 1, 2021
cf99bc8
Merge branch 'main' into page-management
galz10 Sep 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions samples/snippets/create_agent_test.py
Expand Up @@ -13,8 +13,8 @@

"""Test for create_agent"""

from datetime import date
import os
import uuid

from google.cloud.dialogflowcx_v3.services.agents.client import AgentsClient
from google.cloud.dialogflowcx_v3.types.agent import DeleteAgentRequest
Expand All @@ -34,8 +34,7 @@ def delete_agent(name):


def test_create_agent():
today = date.today()
agentName = "tempAgent." + today.strftime("%d.%m.%Y")
agentName = f"fake_agent_{uuid.uuid4()}"
response = create_agent(PROJECT_ID, agentName)
delete_agent(response.name)

Expand Down
96 changes: 96 additions & 0 deletions samples/snippets/page_management.py
@@ -0,0 +1,96 @@
# 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
#
# 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.

from google.cloud.dialogflowcx_v3 import PagesAsyncClient
from google.cloud.dialogflowcx_v3.types.page import (
CreatePageRequest,
DeletePageRequest,
ListPagesRequest,
Page,
)


# [START dialogflow_cx_create_page]
async def create_page(project_id, agent_id, flow_id, location, displayName):
pages_client = PagesAsyncClient()

page = Page()
page.display_name = displayName

request = CreatePageRequest()
request.parent = (
"projects/"
galz10 marked this conversation as resolved.
Show resolved Hide resolved
+ project_id
+ "/locations/"
+ location
+ "/agents/"
+ agent_id
+ "/flows/"
+ flow_id
)
request.page = page

response = await pages_client.create_page(request=request)
return response


# [END dialogflow_cx_create_page]


# [START dialogflow_cx_list_page]
async def list_page(project_id, agent_id, flow_id, location):
pages_client = PagesAsyncClient()

request = ListPagesRequest()
request.parent = (
"projects/"
+ project_id
+ "/locations/"
+ location
+ "/agents/"
+ agent_id
+ "/flows/"
+ flow_id
)
request.language_code = "en"

response = await pages_client.list_pages(request=request)
return response


# [END dialogflow_cx_list_page]


# [START dialogflow_cx_delete_page]
async def delete_page(project_id, agent_id, flow_id, page_id, location):
pages_client = PagesAsyncClient()

request = DeletePageRequest()
request.name = (
"projects/"
+ project_id
+ "/locations/"
+ location
+ "/agents/"
+ agent_id
+ "/flows/"
+ flow_id
+ "/pages/"
+ page_id
)

response = await pages_client.delete_page(request=request)
return response


# [END dialogflow_cx_delete_page]
114 changes: 114 additions & 0 deletions samples/snippets/page_management_test.py
@@ -0,0 +1,114 @@
# 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
#
# 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.

import asyncio
from copy import Error
import os
import uuid

from google.cloud.dialogflowcx_v3.services.agents.client import AgentsClient
from google.cloud.dialogflowcx_v3.types.agent import Agent, DeleteAgentRequest

import pytest

from page_management import create_page, delete_page, list_page

PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
pytest.AGENT_ID = None
pytest.PARENT = None
pytest.CREATED_PAGE = None
pytest.PAGE_ID = None


def delete_agent(name):
agents_client = AgentsClient()
agent = DeleteAgentRequest(name=name)
agents_client.delete_agent(request=agent)


@pytest.fixture
def loop():
loop = asyncio.new_event_loop()
yield loop
loop.close()


@pytest.fixture(scope="module", autouse=True)
def setup_teardown():
loop = asyncio.new_event_loop()
agentName = "temp_agent_" + str(uuid.uuid4())

parent = "projects/" + PROJECT_ID + "/locations/global"

agents_client = AgentsClient()

agent = Agent(
display_name=agentName,
default_language_code="en",
time_zone="America/Los_Angeles",
)

response = agents_client.create_agent(request={"agent": agent, "parent": parent})
pytest.PARENT = response.name

pytest.AGENT_ID = pytest.PARENT.split("/")[5]
print("Created Agent in setUp")

yield

delete_agent(pytest.PARENT)
loop.close()


def test_create_page(loop: asyncio.AbstractEventLoop):
pytest.CREATED_PAGE = f"fake_page_{uuid.uuid4()}"
actualResponse = loop.run_until_complete(
create_page(
PROJECT_ID,
pytest.AGENT_ID,
"00000000-0000-0000-0000-000000000000",
"global",
pytest.CREATED_PAGE,
)
)

pytest.PAGE_ID = actualResponse.name.split("/")[9]
assert actualResponse.display_name == pytest.CREATED_PAGE


def test_list_page(loop: asyncio.AbstractEventLoop):
actualResponse = loop.run_until_complete(
list_page(
PROJECT_ID,
pytest.AGENT_ID,
"00000000-0000-0000-0000-000000000000",
"global",
)
)

assert pytest.PAGE_ID in str(actualResponse)


def test_delete_page(loop: asyncio.AbstractEventLoop):
try:
loop.run_until_complete(
delete_page(
PROJECT_ID,
pytest.AGENT_ID,
"00000000-0000-0000-0000-000000000000",
pytest.PAGE_ID,
"global",
)
)
except Error:
pytest.fail("Unexpected MyError ..")