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

Commit 9f23fe2

Browse files
docs: add get operation code snippet (#28)
* docs: add get operation code snippet * add batch import * fix int * fix lint * fix import * rename method * add operation import * fix symbol * add multiline * 🦉 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>
1 parent bf07a2f commit 9f23fe2

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-optimization/
8080
| Sample | Source Code | Try it |
8181
| --------------------------- | --------------------------------- | ------ |
8282
| Async Api | [source code](https://github.com/googleapis/java-optimization/blob/main/samples/snippets/src/main/java/com/example/optimizationai/AsyncApi.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-optimization&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/optimizationai/AsyncApi.java) |
83+
| Get Operation | [source code](https://github.com/googleapis/java-optimization/blob/main/samples/snippets/src/main/java/com/example/optimizationai/GetOperation.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-optimization&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/optimizationai/GetOperation.java) |
8384
| Sync Api | [source code](https://github.com/googleapis/java-optimization/blob/main/samples/snippets/src/main/java/com/example/optimizationai/SyncApi.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-optimization&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/optimizationai/SyncApi.java) |
8485
| Sync Api With Long Timeout | [source code](https://github.com/googleapis/java-optimization/blob/main/samples/snippets/src/main/java/com/example/optimizationai/SyncApiWithLongTimeout.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-optimization&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/optimizationai/SyncApiWithLongTimeout.java) |
8586

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2022 Google LLC
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.optimizationai;
18+
19+
// [START cloudoptimization_get_operation]
20+
import com.google.cloud.optimization.v1.FleetRoutingClient;
21+
import com.google.longrunning.Operation;
22+
import java.io.IOException;
23+
24+
class GetOperation {
25+
26+
static void getOperation() throws IOException {
27+
// TODO(developer): Replace these variables before running the sample.
28+
String operationFullId = "projects/[projectId]/operations/[operationId]";
29+
getOperation(operationFullId);
30+
}
31+
32+
// Get the status of an operation
33+
static void getOperation(String operationFullId) throws IOException {
34+
// Initialize client that will be used to send requests. This client only needs to be created
35+
// once, and can be reused for multiple requests. After completing all of your requests, call
36+
// the "close" method on the client to safely clean up any remaining background resources.
37+
try (FleetRoutingClient client = FleetRoutingClient.create()) {
38+
// Get the latest state of a long-running operation.
39+
Operation operation = client.getOperationsClient().getOperation(operationFullId);
40+
41+
// Display operation details.
42+
System.out.println("Operation details:");
43+
System.out.format("\tName: %s\n", operation.getName());
44+
System.out.format("\tMetadata Type Url: %s\n", operation.getMetadata().getTypeUrl());
45+
System.out.format("\tDone: %s\n", operation.getDone());
46+
if (operation.hasResponse()) {
47+
System.out.format("\tResponse Type Url: %s\n", operation.getResponse().getTypeUrl());
48+
}
49+
if (operation.hasError()) {
50+
System.out.println("\tResponse:");
51+
System.out.format("\t\tError code: %s\n", operation.getError().getCode());
52+
System.out.format("\t\tError message: %s\n", operation.getError().getMessage());
53+
}
54+
}
55+
}
56+
}
57+
// [END cloudoptimization_get_operation]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2022 Google LLC
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.optimizationai;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.api.gax.longrunning.OperationFuture;
22+
import com.google.cloud.optimization.v1.AsyncModelMetadata;
23+
import com.google.cloud.optimization.v1.BatchOptimizeToursRequest;
24+
import com.google.cloud.optimization.v1.BatchOptimizeToursResponse;
25+
import com.google.cloud.optimization.v1.FleetRoutingClient;
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.PrintStream;
28+
import org.junit.After;
29+
import org.junit.Before;
30+
import org.junit.Test;
31+
32+
/** Tests for GetOperation sample. */
33+
public class GetOperationTest {
34+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
35+
private static final String PROJECT_PARENT = String.format("projects/%s", PROJECT_ID);
36+
37+
private ByteArrayOutputStream bout;
38+
private PrintStream out;
39+
private PrintStream originalPrintStream;
40+
41+
@Before
42+
public void setUp() {
43+
bout = new ByteArrayOutputStream();
44+
out = new PrintStream(bout);
45+
originalPrintStream = System.out;
46+
System.setOut(out);
47+
}
48+
49+
@After
50+
public void tearDown() {
51+
System.out.flush();
52+
System.setOut(originalPrintStream);
53+
}
54+
55+
@Test
56+
public void testSyncApi() throws Exception {
57+
FleetRoutingClient fleetRoutingClient = FleetRoutingClient.create();
58+
BatchOptimizeToursRequest request =
59+
BatchOptimizeToursRequest.newBuilder().setParent(PROJECT_PARENT).build();
60+
OperationFuture<BatchOptimizeToursResponse, AsyncModelMetadata> response =
61+
fleetRoutingClient.batchOptimizeToursAsync(request);
62+
63+
GetOperation.getOperation(response.getInitialFuture().get().getName());
64+
String got = bout.toString();
65+
assertThat(got).contains("operations");
66+
}
67+
}

0 commit comments

Comments
 (0)