Skip to content

Commit f7bf8eb

Browse files
authored
samples: set project-level TTL (#86)
1 parent a5ecd99 commit f7bf8eb

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2021 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.contactcenterinsights;
18+
19+
// [START contactcenterinsights_set_project_ttl]
20+
21+
import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient;
22+
import com.google.cloud.contactcenterinsights.v1.Settings;
23+
import com.google.cloud.contactcenterinsights.v1.SettingsName;
24+
import com.google.protobuf.Duration;
25+
import com.google.protobuf.FieldMask;
26+
import java.io.IOException;
27+
28+
public class SetProjectTtl {
29+
30+
public static void main(String[] args) throws IOException {
31+
// TODO(developer): Replace this variable before running the sample.
32+
String projectId = "my_project_id";
33+
34+
setProjectTtl(projectId);
35+
}
36+
37+
public static void setProjectTtl(String projectId) throws IOException {
38+
// Initialize client that will be used to send requests. This client only needs to be created
39+
// once, and can be reused for multiple requests. After completing all of your requests, call
40+
// the "close" method on the client to safely clean up any remaining background resources.
41+
try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) {
42+
// Construct a settings resource.
43+
SettingsName name = SettingsName.of(projectId, "us-central1");
44+
Settings settings =
45+
Settings.newBuilder()
46+
.setName(name.toString())
47+
.setConversationTtl(Duration.newBuilder().setSeconds(86400).build())
48+
.build();
49+
50+
// Construct an update mask.
51+
FieldMask updateMask = FieldMask.newBuilder().addPaths("conversation_ttl").build();
52+
53+
// Call the Insights client to set a project-level TTL.
54+
Settings response = client.updateSettings(settings, updateMask);
55+
System.out.printf("Set TTL for all incoming conversations to 1 day");
56+
}
57+
}
58+
}
59+
60+
// [END contactcenterinsights_set_project_ttl]
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2021 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.contactcenterinsights;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient;
23+
import com.google.cloud.contactcenterinsights.v1.Settings;
24+
import com.google.cloud.contactcenterinsights.v1.SettingsName;
25+
import com.google.protobuf.Duration;
26+
import com.google.protobuf.FieldMask;
27+
import java.io.ByteArrayOutputStream;
28+
import java.io.IOException;
29+
import java.io.PrintStream;
30+
import org.junit.After;
31+
import org.junit.Before;
32+
import org.junit.BeforeClass;
33+
import org.junit.Test;
34+
import org.junit.runner.RunWith;
35+
import org.junit.runners.JUnit4;
36+
37+
@RunWith(JUnit4.class)
38+
public class SetProjectTtlIT {
39+
40+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
41+
private ByteArrayOutputStream bout;
42+
private PrintStream out;
43+
44+
private static void requireEnvVar(String varName) {
45+
assertNotNull(String.format(varName), String.format(varName));
46+
}
47+
48+
@BeforeClass
49+
public static void checkRequirements() {
50+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
51+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
52+
}
53+
54+
@Before
55+
public void setUp() {
56+
bout = new ByteArrayOutputStream();
57+
out = new PrintStream(bout);
58+
System.setOut(out);
59+
}
60+
61+
@After
62+
public void tearDown() throws IOException {
63+
try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) {
64+
// Clear project-level TTL.
65+
SettingsName name = SettingsName.of(PROJECT_ID, "us-central1");
66+
Settings settings =
67+
Settings.newBuilder()
68+
.setName(name.toString())
69+
.setConversationTtl(Duration.newBuilder().build())
70+
.build();
71+
72+
FieldMask updateMask = FieldMask.newBuilder().addPaths("conversation_ttl").build();
73+
74+
Settings response = client.updateSettings(settings, updateMask);
75+
}
76+
System.setOut(null);
77+
}
78+
79+
@Test
80+
public void testSetProjecTtl() throws IOException {
81+
SetProjectTtl.setProjectTtl(PROJECT_ID);
82+
assertThat(bout.toString()).contains("Set TTL for all incoming conversations to 1 day");
83+
}
84+
}

0 commit comments

Comments
 (0)