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

docs(samples): add try blocks to clean up resources in samples and sample tests #521

Merged
merged 27 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
380f5cb
add try block to clean up resources
aribray Jul 17, 2022
d336117
docs: add try block to clean up resources
aribray Jul 17, 2022
644d764
Merge branch 'aribray--close-resources' of github.com:aribray/java-di…
aribray Jul 17, 2022
48173c9
add try block to clean up resources
aribray Jul 17, 2022
247d799
add try block to clean up resources
aribray Jul 17, 2022
0377870
add Thread.sleep to try blocks, fix checkstyle
aribray Jul 17, 2022
330ad00
add Thread.sleep
aribray Jul 18, 2022
65152dd
add Thread.sleep
aribray Jul 18, 2022
f334e61
add try block to clean up resources
aribray Jul 18, 2022
07b5ace
change fulfillmentInfo tag
aribray Jul 18, 2022
3a63902
add try block to clean up resources
aribray Jul 18, 2022
3282ef2
add ;
aribray Jul 18, 2022
a5d2cac
remove try block
aribray Jul 18, 2022
dc15951
move try block
aribray Jul 18, 2022
339d56f
remove try block
aribray Jul 18, 2022
59479ed
change fulfillmentInfo tag
aribray Jul 18, 2022
a166911
checkstyle
aribray Jul 18, 2022
72c7080
add try block to clean up resources
aribray Jul 18, 2022
4d9513a
add catch
aribray Jul 18, 2022
f2936a8
remove println
aribray Jul 18, 2022
d9cbf0a
checkstyle
aribray Jul 18, 2022
d8fa631
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jul 18, 2022
3e2f984
add try-with-resources comment to client object creation lines in sni…
aribray Jul 19, 2022
3bc40dd
Merge branch 'aribray--close-resources' of github.com:aribray/java-di…
aribray Jul 19, 2022
2fbdbf5
add string.format
aribray Jul 19, 2022
84a71da
update parentPath
aribray Jul 19, 2022
58cdd11
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jul 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 21 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,27 @@ 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();

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);
}
}
}
// [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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we don't label things?

Copy link
Contributor Author

@aribray aribray Jul 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know the answer to this one, but I think it's a good question. I know that when I write snippets, I use a combination of the snippet style guide and the existing snippets for context. I know the overall style guide just got an update, but now might be a good time to have a language-specific refresh. @kweinmeister, I'll pass this off to you for your thoughts!

}
}
}
}
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