Skip to content

Commit 50fb4e9

Browse files
authored
samples: set project-level TTL (#23)
1 parent fbe592f commit 50fb4e9

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
google-api-core==2.0.1
22
google-cloud-bigquery==2.26.0
3-
google-cloud-contact-center-insights==0.2.0
3+
google-cloud-contact-center-insights==0.2.0
4+
protobuf==3.17.3
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
# Set a project-level TTL for all incoming conversations.
16+
# [START contactcenterinsights_set_project_ttl]
17+
from google.api_core import protobuf_helpers
18+
from google.cloud import contact_center_insights_v1
19+
from google.protobuf import duration_pb2
20+
21+
22+
def set_project_ttl(project_id: str) -> None:
23+
# Construct a settings resource.
24+
settings = contact_center_insights_v1.Settings()
25+
settings.name = contact_center_insights_v1.ContactCenterInsightsClient.settings_path(
26+
project_id, "us-central1"
27+
)
28+
29+
conversation_ttl = duration_pb2.Duration()
30+
conversation_ttl.seconds = 86400
31+
settings.conversation_ttl = conversation_ttl
32+
33+
# Construct an update mask to only update the fields that are set on the settings resource.
34+
update_mask = protobuf_helpers.field_mask(None, type(settings).pb(settings))
35+
36+
# Construct an Insights client that will authenticate via Application Default Credentials.
37+
# See authentication details at https://cloud.google.com/docs/authentication/production.
38+
insights_client = contact_center_insights_v1.ContactCenterInsightsClient()
39+
40+
# Call the Insights client to set a project-level TTL.
41+
insights_client.update_settings(settings=settings, update_mask=update_mask)
42+
43+
# Call the Insights client to get the project-level TTL to confirm that it was set.
44+
new_conversation_ttl = insights_client.get_settings(
45+
name=settings.name
46+
).conversation_ttl
47+
print(
48+
"Set TTL for all incoming conversations to {} day".format(
49+
new_conversation_ttl.days
50+
)
51+
)
52+
53+
# [END contactcenterinsights_set_project_ttl]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
from google.protobuf import field_mask_pb2
19+
20+
import pytest
21+
22+
import set_project_ttl
23+
24+
25+
@pytest.fixture
26+
def project_id():
27+
_, project_id = google.auth.default()
28+
return project_id
29+
30+
31+
@pytest.fixture
32+
def clear_project_ttl(project_id):
33+
yield
34+
settings = contact_center_insights_v1.Settings()
35+
settings.name = contact_center_insights_v1.ContactCenterInsightsClient.settings_path(
36+
project_id, "us-central1"
37+
)
38+
settings.conversation_ttl = None
39+
update_mask = field_mask_pb2.FieldMask()
40+
update_mask.paths.append("conversation_ttl")
41+
42+
insights_client = contact_center_insights_v1.ContactCenterInsightsClient()
43+
insights_client.update_settings(settings=settings, update_mask=update_mask)
44+
45+
46+
def test_set_project_ttl(capsys, project_id, clear_project_ttl):
47+
set_project_ttl.set_project_ttl(project_id)
48+
out, err = capsys.readouterr()
49+
assert "Set TTL for all incoming conversations to 1 day" in out

0 commit comments

Comments
 (0)