Skip to content

Commit

Permalink
samples: get operation (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
TrucHLe authored Sep 24, 2021
1 parent f90d2d9 commit b34509d
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
36 changes: 36 additions & 0 deletions contact-center-insights/snippets/get_operation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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.
#
# Get a long-running operation.
# [START contactcenterinsights_get_operation]
from google.cloud import contact_center_insights_v1
from google.longrunning import operations_pb2


def get_operation(operation_name: str) -> operations_pb2.Operation:
# Construct an Insights client that will authenticate via Application Default Credentials.
# See authentication details at https://cloud.google.com/docs/authentication/production.
insights_client = contact_center_insights_v1.ContactCenterInsightsClient()

# Call the Insights client to get the operation.
operation = insights_client.transport.operations_client.get_operation(
operation_name
)
if operation.done:
print("Operation is done")
else:
print("Operation is in progress")


# [END contactcenterinsights_get_operation]
80 changes: 80 additions & 0 deletions contact-center-insights/snippets/test_get_operation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# 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 google.auth

from google.cloud import contact_center_insights_v1

import pytest

import get_operation

TRANSCRIPT_URI = "gs://cloud-samples-data/ccai/chat_sample.json"
AUDIO_URI = "gs://cloud-samples-data/ccai/voice_6912.txt"


@pytest.fixture
def project_id():
_, project_id = google.auth.default()
return project_id


@pytest.fixture
def insights_client():
return contact_center_insights_v1.ContactCenterInsightsClient()


@pytest.fixture
def conversation_resource(project_id, insights_client):
# Create a conversation.
parent = contact_center_insights_v1.ContactCenterInsightsClient.common_location_path(
project_id, "us-central1"
)

conversation = contact_center_insights_v1.Conversation()
conversation.data_source.gcs_source.transcript_uri = TRANSCRIPT_URI
conversation.data_source.gcs_source.audio_uri = AUDIO_URI
conversation.medium = contact_center_insights_v1.Conversation.Medium.CHAT

conversation = insights_client.create_conversation(
parent=parent, conversation=conversation
)
yield conversation

# Delete the conversation.
delete_request = contact_center_insights_v1.DeleteConversationRequest()
delete_request.name = conversation.name
delete_request.force = True
insights_client.delete_conversation(request=delete_request)


@pytest.fixture
def analysis_operation(conversation_resource, insights_client):
# Create an analysis.
conversation_name = conversation_resource.name
analysis = contact_center_insights_v1.Analysis()
analysis_operation = insights_client.create_analysis(
parent=conversation_name, analysis=analysis
)

# Wait until the analysis operation is done and return the operation.
analysis_operation.result(timeout=600)
yield analysis_operation


def test_get_operation(capsys, analysis_operation):
operation_name = analysis_operation.operation.name
get_operation.get_operation(operation_name)
out, err = capsys.readouterr()
assert "Operation is done" in out

0 comments on commit b34509d

Please sign in to comment.