Skip to content

Commit b34509d

Browse files
authored
samples: get operation (#24)
1 parent f90d2d9 commit b34509d

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
# Get a long-running operation.
16+
# [START contactcenterinsights_get_operation]
17+
from google.cloud import contact_center_insights_v1
18+
from google.longrunning import operations_pb2
19+
20+
21+
def get_operation(operation_name: str) -> operations_pb2.Operation:
22+
# Construct an Insights client that will authenticate via Application Default Credentials.
23+
# See authentication details at https://cloud.google.com/docs/authentication/production.
24+
insights_client = contact_center_insights_v1.ContactCenterInsightsClient()
25+
26+
# Call the Insights client to get the operation.
27+
operation = insights_client.transport.operations_client.get_operation(
28+
operation_name
29+
)
30+
if operation.done:
31+
print("Operation is done")
32+
else:
33+
print("Operation is in progress")
34+
35+
36+
# [END contactcenterinsights_get_operation]
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
import google.auth
16+
17+
from google.cloud import contact_center_insights_v1
18+
19+
import pytest
20+
21+
import get_operation
22+
23+
TRANSCRIPT_URI = "gs://cloud-samples-data/ccai/chat_sample.json"
24+
AUDIO_URI = "gs://cloud-samples-data/ccai/voice_6912.txt"
25+
26+
27+
@pytest.fixture
28+
def project_id():
29+
_, project_id = google.auth.default()
30+
return project_id
31+
32+
33+
@pytest.fixture
34+
def insights_client():
35+
return contact_center_insights_v1.ContactCenterInsightsClient()
36+
37+
38+
@pytest.fixture
39+
def conversation_resource(project_id, insights_client):
40+
# Create a conversation.
41+
parent = contact_center_insights_v1.ContactCenterInsightsClient.common_location_path(
42+
project_id, "us-central1"
43+
)
44+
45+
conversation = contact_center_insights_v1.Conversation()
46+
conversation.data_source.gcs_source.transcript_uri = TRANSCRIPT_URI
47+
conversation.data_source.gcs_source.audio_uri = AUDIO_URI
48+
conversation.medium = contact_center_insights_v1.Conversation.Medium.CHAT
49+
50+
conversation = insights_client.create_conversation(
51+
parent=parent, conversation=conversation
52+
)
53+
yield conversation
54+
55+
# Delete the conversation.
56+
delete_request = contact_center_insights_v1.DeleteConversationRequest()
57+
delete_request.name = conversation.name
58+
delete_request.force = True
59+
insights_client.delete_conversation(request=delete_request)
60+
61+
62+
@pytest.fixture
63+
def analysis_operation(conversation_resource, insights_client):
64+
# Create an analysis.
65+
conversation_name = conversation_resource.name
66+
analysis = contact_center_insights_v1.Analysis()
67+
analysis_operation = insights_client.create_analysis(
68+
parent=conversation_name, analysis=analysis
69+
)
70+
71+
# Wait until the analysis operation is done and return the operation.
72+
analysis_operation.result(timeout=600)
73+
yield analysis_operation
74+
75+
76+
def test_get_operation(capsys, analysis_operation):
77+
operation_name = analysis_operation.operation.name
78+
get_operation.get_operation(operation_name)
79+
out, err = capsys.readouterr()
80+
assert "Operation is done" in out

0 commit comments

Comments
 (0)