-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(samples): added page management samples (#152)
* docs(samples): added page management samples * fixed failing test * Remvoe await * added await * added pytest async * Lint fix * Lint and test case fix * removed pytest.async * test fix * update tests * fix uuid * added asyncio * added loop * removed async * test fix * changed pytest scope to session * changes fixture scope * test change * test fix * changed response type * fixed list page test * lint fix * failing test fix * revised code per comments * converted object to string Co-authored-by: Anthonios Partheniou <partheniou@google.com>
- Loading branch information
Showing
3 changed files
with
195 additions
and
3 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,79 @@ | ||
# 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/" | ||
+ 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 = ( | ||
f"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 = f"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] |
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,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 ..") |