Skip to content
This repository has been archived by the owner on Sep 16, 2023. It is now read-only.

Commit

Permalink
docs(samples): add try blocks to clean up resources in samples and sa…
Browse files Browse the repository at this point in the history
…mple tests (#521)

* add try block to clean up resources

Change-Id: Ie68f4715646942d2e78189b3477cb9a5a31713b8

* docs: add try block to clean up resources

Change-Id: Ibaa2bd6a9f687b67af61a5f958c37e2faf087434

* add try block to clean up resources

Change-Id: I60fff4d6c83d42b8921b4ed4ce6c0eea76a952c6

* add try block to clean up resources

Change-Id: I2facb88a81c1d76d5b2316da04cebdffba6634b9

* add Thread.sleep to try blocks, fix checkstyle

Change-Id: I0eab8286ac21a8becf64b541100050581016ca5d

* add Thread.sleep

Change-Id: I94553a4501a6bdfbdcad1360833cd5e4352d9a77

* add Thread.sleep

Change-Id: I123c577193306dc4625d2d56637e71e13cd06f70

* add try block to clean up resources

Change-Id: I38d51da44e19b7c50d09d3e32aa6b5378c8fc0e2

* change fulfillmentInfo tag

Change-Id: I8f0b7e31a2283c5dae7c6b377861ee03e8cbe621

* add try block to clean up resources

Change-Id: I3ee67b4c3ce45b18138d9382fd18f5f4646372db

* add ;

Change-Id: I10cb37ff93f4c28c1f94d8e221c893fe4b74835a

* remove try block

Change-Id: I9b5f96d7c0ee75745084ed2a277fb9dc8b76a98f

* move try block

Change-Id: I17eeaf75673cb24682552dd59916c0ad5d68b89a

* remove try block

Change-Id: I001e1bd68b5c0953d2c634239828d81bf2576a25

* change fulfillmentInfo tag

Change-Id: I1454736df9212b36afcd322ef3ce8c677b3500e8

* checkstyle

Change-Id: I10d3750668a4acb9ff18f58fdb4f1a19faf6eacc

* add try block to clean up resources

Change-Id: I2a56b4f9b558fbebb816fb409fdabff4ee194f2f

* add catch

Change-Id: I7dd46f9543256427e9d86ab4f60b4806bdb8bd9a

* remove println

Change-Id: Idb0f4d75d0e7902a666248c48ed4fc54aeef5e59

* checkstyle

Change-Id: Ie28023c2bf6422a50dc5e09128ede1e49679c95d

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* add try-with-resources comment to client object creation lines in snippets

Change-Id: Ic1acaa6097e972b2db35cca2f5928e826ef08964

* add string.format

Change-Id: I25e7e803c8d0b40e6503b6ed8c5fd1c872c4506b

* update parentPath

Change-Id: Id387695b0d86c30c74f796e7f57db6e1f8be43bd

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
aribray and gcf-owl-bot[bot] authored Jul 19, 2022
1 parent d73cf1d commit d07165a
Show file tree
Hide file tree
Showing 29 changed files with 157 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ public void service(HttpRequest request, HttpResponse response) throws Exception
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonResponseObject = gson.toJson(webhookResponse);

System.out.println("Response Object: \n");
System.out.println(jsonResponseObject.toString());

/* {
* "page_info": {
* "form_info": {
Expand Down
3 changes: 3 additions & 0 deletions samples/snippets/src/main/java/dialogflow/cx/CreateAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public static Agent createAgent(String parent, String displayName) throws IOExce
String apiEndpoint = "global-dialogflow.googleapis.com:443";

AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build();
// Note: close() needs to be called on the AgentsClient object to clean up resources
// such as threads. In the example below, try-with-resources is used,
// which automatically calls close().
try (AgentsClient client = AgentsClient.create(agentsSettings)) {
// Set the details of the Agent to create
Builder build = Agent.newBuilder();
Expand Down
5 changes: 4 additions & 1 deletion samples/snippets/src/main/java/dialogflow/cx/CreateFlow.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ public static Flow createFlow(
}
FlowsSettings flowsSettings = flowsSettingsBuilder.build();

// Instantiates a client
// Instantiates a client.
// Note: close() needs to be called on the FlowsClient object to clean up resources
// such as threads. In the example below, try-with-resources is used,
// which automatically calls close().
try (FlowsClient flowsClient = FlowsClient.create(flowsSettings)) {
// Set the project agent name using the projectID (my-project-id), locationID (global), and
// agentID (UUID).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public static Intent createIntent(
IntentsSettings intentsSettings = intentsSettingsBuilder.build();

// Instantiates a client
// Note: close() needs to be called on the IntentsClient object to clean up resources
// such as threads. In the example below, try-with-resources is used,
// which automatically calls close().
try (IntentsClient intentsClient = IntentsClient.create(intentsSettings)) {
// Set the project agent name using the projectID (my-project-id), locationID (global), and
// agentID (UUID).
Expand Down
3 changes: 3 additions & 0 deletions samples/snippets/src/main/java/dialogflow/cx/CreatePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public static Page createPage(
PagesSettings pagesSettings = pagesSettingsBuilder.build();

// Instantiates a client
// Note: close() needs to be called on the PagesClient object to clean up resources
// such as threads. In the example below, try-with-resources is used,
// which automatically calls close().
try (PagesClient pagesClient = PagesClient.create(pagesSettings)) {
// Set the flow name using the projectID (my-project-id), locationID (global), agentID (UUID)
// and flowID (UUID).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public static Page createPage(
.setPage(pageBuilder);

// Make API request to create page
// Note: close() needs to be called on the PagesClient object to clean up resources
// such as threads. In the example below, try-with-resources is used,
// which automatically calls close().
try (PagesClient client = PagesClient.create()) {
response = client.createPage(createRequestBuilder.build());
System.out.println("Successfully created page!");
Expand Down
4 changes: 4 additions & 0 deletions samples/snippets/src/main/java/dialogflow/cx/DeletePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public static void main(String[] args) throws IOException {
public static void deletePage(
String projectId, String agentId, String flowId, String pageId, String location)
throws IOException {

// Note: close() needs to be called on the PagesClient object to clean up resources
// such as threads. In the example below, try-with-resources is used,
// which automatically calls close().
try (PagesClient client = PagesClient.create()) {
Builder deleteRequestBuilder = DeletePageRequest.newBuilder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ public static Map<String, QueryResult> detectIntent(
SessionsSettings sessionsSettings = sessionsSettingsBuilder.build();

Map<String, QueryResult> queryResults = Maps.newHashMap();
// Instantiates a client
// Instantiates a client.

// Note: close() needs to be called on the SessionsClient object to clean up resources
// such as threads. In the example below, try-with-resources is used,
// which automatically calls close().
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
// Set the session name using the projectID (my-project-id), locationID (global), agentID
// (UUID), and sessionId (UUID).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public static void detectIntent(

// Instantiates a client by setting the session name.
// Format:`projects/<ProjectID>/locations/<LocationID>/agents/<AgentID>/sessions/<SessionID>`

// Note: close() needs to be called on the SessionsClient object to clean up resources
// such as threads. In the example below, try-with-resources is used,
// which automatically calls close().
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
SessionName session =
SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public static Map<String, QueryResult> detectIntent(

// Instantiates a client by setting the session name.
// Format:`projects/<ProjectID>/locations/<LocationID>/agents/<AgentID>/sessions/<SessionID>`

// Note: close() needs to be called on the SessionsClient object to clean up resources
// such as threads. In the example below, try-with-resources is used,
// which automatically calls close().
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
SessionName session =
SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public static void detectIntent(

// Instantiates a client by setting the session name.
// Format:`projects/<ProjectID>/locations/<LocationID>/agents/<AgentID>/sessions/<SessionID>`

// Note: close() needs to be called on the SessionsClient object to clean up resources
// such as threads. In the example below, try-with-resources is used,
// which automatically calls close().
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
SessionName session =
SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public static void detectIntent(

// Instantiates a client by setting the session name.
// Format:`projects/<ProjectID>/locations/<LocationID>/agents/<AgentID>/sessions/<SessionID>`

// Note: close() needs to be called on the SessionsClient object to clean up resources
// such as threads. In the example below, try-with-resources is used,
// which automatically calls close().
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
SessionName session =
SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public static Map<String, QueryResult> detectIntent(

// Instantiates a client by setting the session name.
// Format:`projects/<ProjectID>/locations/<LocationID>/agents/<AgentID>/sessions/<SessionID>`

// Note: close() needs to be called on the SessionsClient object to clean up resources
// such as threads. In the example below, try-with-resources is used,
// which automatically calls close().
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
SessionName session =
SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public static void detectIntentStream(
// Instantiates a client by setting the session name.
// Format: `projects/<ProjectID>/locations/<LocationID>/agents/<AgentID>/sessions/<SessionID>`
// Using the same `sessionId` between requests allows continuation of the conversation.

// Note: close() needs to be called on the SessionsClient object to clean up resources
// such as threads. In the example below, try-with-resources is used,
// which automatically calls close().
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
SessionName session = SessionName.of(projectId, locationId, agentId, sessionId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public static void detectIntentStreamingPartialResponse(
// Instantiates a client by setting the session name.
// Format:`projects/<ProjectID>/locations/<LocationID>/agents/<AgentID>/sessions/<SessionID>`
// Using the same `sessionId` between requests allows continuation of the conversation.

// Note: close() needs to be called on the SessionsClient object to clean up resources
// such as threads. In the example below, try-with-resources is used,
// which automatically calls close().
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
SessionName session = SessionName.of(projectId, locationId, agentId, sessionId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public static void detectIntent(

// Instantiates a client by setting the session name.
// Format:`projects/<ProjectID>/locations/<LocationID>/agents/<AgentID>/sessions/<SessionID>`

// Note: close() needs to be called on the SessionsClient object to clean up resources
// such as threads. In the example below, try-with-resources is used,
// which automatically calls close().
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
SessionName session =
SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId);
Expand Down
3 changes: 3 additions & 0 deletions samples/snippets/src/main/java/dialogflow/cx/ExportAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public static void exportAgent(String projectId, String agentId, String location
String apiEndpoint = String.format("%s-dialogflow.googleapis.com:443", location);

AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build();
// Note: close() needs to be called on the AgentsClient object to clean up resources
// such as threads. In the example below, try-with-resources is used,
// which automatically calls close().
try (AgentsClient agentsClient = AgentsClient.create(agentsSettings)) {
ExportAgentRequest request =
ExportAgentRequest.newBuilder()
Expand Down
33 changes: 16 additions & 17 deletions samples/snippets/src/main/java/dialogflow/cx/ListPages.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,22 @@ public static void main(String[] args) throws IOException {
// Lists all pages from the provided parameters
public static void listPages(String projectId, String agentId, String flowId, String location)
throws IOException {
PagesClient client = PagesClient.create();
Builder listRequestBuilder = ListPagesRequest.newBuilder();

listRequestBuilder.setParent(
"projects/"
+ projectId
+ "/locations/"
+ location
+ "/agents/"
+ agentId
+ "/flows/"
+ flowId);
listRequestBuilder.setLanguageCode("en");

// Make API request to list all pages in the project
for (Page element : client.listPages(listRequestBuilder.build()).iterateAll()) {
System.out.println(element);
// Note: close() needs to be called on the PagesClient object to clean up resources
// such as threads. In the example below, try-with-resources is used,
// which automatically calls close().
try (PagesClient client = PagesClient.create()) {
Builder listRequestBuilder = ListPagesRequest.newBuilder();

String parentPath =
String.format(
"projects/%s/locations/%s/agents/%s/flows/%s", projectId, location, agentId, flowId);
listRequestBuilder.setParent(parentPath);
listRequestBuilder.setLanguageCode("en");

// Make API request to list all pages in the project
for (Page element : client.listPages(listRequestBuilder.build()).iterateAll()) {
System.out.println(element);
}
}
}
// [END dialogflow_cx_list_pages]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@ public static void listTestCaseResults(
TestCasesSettings.newBuilder()
.setEndpoint(location + "-dialogflow.googleapis.com:443")
.build();
TestCasesClient client = TestCasesClient.create(testCasesSettings);

for (TestCaseResult element : client.listTestCaseResults(req.build()).iterateAll()) {
System.out.println(element);
// Note: close() needs to be called on the TestCasesClient object to clean up resources
// such as threads. In the example below, try-with-resources is used,
// which automatically calls close().
try (TestCasesClient client = TestCasesClient.create(testCasesSettings)) {
for (TestCaseResult element : client.listTestCaseResults(req.build()).iterateAll()) {
System.out.println(element);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public static void main(String[] args) throws IOException {
// DialogFlow API List Training Phrases sample.
public static void listTrainingPhrases(
String projectId, String location, String agentId, String intentId) throws IOException {

// Note: close() needs to be called on the IntentsClient object to clean up resources
// such as threads. In the example below, try-with-resources is used,
// which automatically calls close().
try (IntentsClient client = IntentsClient.create()) {
// Set the intent name
IntentName name = IntentName.of(projectId, location, agentId, intentId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public static void main(String[] args) throws IOException {
public static void updateIntent(
String projectId, String agentId, String intentId, String location, String displayName)
throws IOException {

// Note: close() needs to be called on the IntentsClient object to clean up resources
// such as threads. In the example below, try-with-resources is used,
// which automatically calls close().
try (IntentsClient client = IntentsClient.create()) {
String intentPath =
"projects/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ public void service(HttpRequest request, HttpResponse response) throws Exception
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonResponseObject = gson.toJson(webhookResponse);

System.out.println("Session Parameter Info: \n");
System.out.println(jsonResponseObject.toString());

/** { "session_info": { "parameters": { "order_number": "12345" } } } */
BufferedWriter writer = response.getWriter();
// Sends the webhookResponseObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ public void service(HttpRequest request, HttpResponse response) throws Exception
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonResponseObject = gson.toJson(webhookResponse);

System.out.println("Response Object: \n");
System.out.println(jsonResponseObject.toString());
/**
* { "page_info": { "form_info": { "parameter_info": [ { "display_name": "order_number",
* "required": "true", "state": "INVALID", "value": "123" } ] } }, "session_info": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public void beforeTest() throws IOException {
public void helloHttp_bodyParamsPost() throws IOException, Exception {

FulfillmentInfo fulfillmentInfo =
FulfillmentInfo.newBuilder().setTag("configure-session-parameters").build();
FulfillmentInfo.newBuilder()
.setTag("configure-form-parameters-optional-or-parameter")
.build();

WebhookRequest webhookRequest =
WebhookRequest.newBuilder().setFulfillmentInfo(fulfillmentInfo).build();
Expand Down Expand Up @@ -94,5 +96,6 @@ public void helloHttp_bodyParamsPost() throws IOException, Exception {
String expectedResponse = gson.toJson(webhookResponse);

assertThat(responseOut.toString()).isEqualTo(expectedResponse);
Thread.sleep(200);
}
}
10 changes: 5 additions & 5 deletions samples/snippets/src/test/java/dialogflow/cx/CreateAgentIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public void tearDown() throws IOException, InterruptedException {
String apiEndpoint = "global-dialogflow.googleapis.com:443";

AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build();
AgentsClient client = AgentsClient.create(agentsSettings);
try (AgentsClient client = AgentsClient.create(agentsSettings)) {
client.deleteAgent(CreateAgentIT.agentPath);

client.deleteAgent(CreateAgentIT.agentPath);

// Small delay to prevent reaching quota limit of requests per minute
Thread.sleep(250);
// Small delay to prevent reaching quota limit of requests per minute
Thread.sleep(250);
}
}

@Test
Expand Down
Loading

0 comments on commit d07165a

Please sign in to comment.