From c75d5f0a21586dd3c499b76e2b51926b40a35079 Mon Sep 17 00:00:00 2001
From: changsongd <101151583+changsongd@users.noreply.github.com>
Date: Fri, 8 Apr 2022 10:30:12 -0700
Subject: [PATCH] docs: add a code snippet for the sync api (#24)
* Added a sync api code snippet
---
optimization/snippets/pom.xml | 3 +-
.../snippets/resources/sync_request.textproto | 64 +++++++++++++++++++
.../com/example/optimizationai/SyncApi.java | 57 +++++++++++++++++
.../example/optimizationai/SyncApiTest.java | 57 +++++++++++++++++
4 files changed, 179 insertions(+), 2 deletions(-)
create mode 100644 optimization/snippets/resources/sync_request.textproto
create mode 100644 optimization/snippets/src/main/java/com/example/optimizationai/SyncApi.java
create mode 100644 optimization/snippets/src/test/java/com/example/optimizationai/SyncApiTest.java
diff --git a/optimization/snippets/pom.xml b/optimization/snippets/pom.xml
index b843131e1f7..06cfa0fcec3 100644
--- a/optimization/snippets/pom.xml
+++ b/optimization/snippets/pom.xml
@@ -26,7 +26,7 @@
UTF-8
-
+
@@ -45,7 +45,6 @@
google-cloud-optimization
0.1.1
-
com.google.cloud
google-cloud-storage
diff --git a/optimization/snippets/resources/sync_request.textproto b/optimization/snippets/resources/sync_request.textproto
new file mode 100644
index 00000000000..d1026f95eb0
--- /dev/null
+++ b/optimization/snippets/resources/sync_request.textproto
@@ -0,0 +1,64 @@
+# proto-file: google3/google/cloud/optimization/v1/fleet_routing.proto
+# proto-message: OptimizeToursRequest
+model {
+ shipments {
+ pickups {
+ arrival_location { latitude: 48.874507 longitude: 2.30361 }
+ time_windows {
+ start_time { seconds: 1000 }
+ end_time { seconds: 2000 }
+ }
+ duration { seconds: 150 }
+ }
+ deliveries {
+ arrival_location { latitude: 48.880942 longitude: 2.323866 }
+ time_windows {
+ start_time { seconds: 3000 }
+ end_time { seconds: 4000 }
+ }
+ duration: { seconds: 250 }
+ }
+ load_demands {
+ key: "weight"
+ value: { amount: 10 }
+ }
+ }
+ shipments {
+ pickups {
+ arrival_location { latitude: 48.880943 longitude: 2.323867 }
+ time_windows {
+ start_time { seconds: 1001 }
+ end_time { seconds: 2001 }
+ }
+ duration { seconds: 151 }
+ }
+ deliveries {
+ arrival_location { latitude: 48.880940 longitude: 2.323844 }
+ time_windows {
+ start_time { seconds: 3001 }
+ end_time { seconds: 4001 }
+ }
+ duration { seconds: 251 }
+ }
+ load_demands {
+ key: "weight"
+ value: { amount: 20 }
+ }
+ }
+ vehicles {
+ start_location { latitude: 48.863102 longitude: 2.341204 }
+ end_location { latitude: 48.863110 longitude: 2.341205 }
+ load_limits {
+ key: "weight"
+ value: { max_load: 50 }
+ }
+ }
+ vehicles {
+ start_location { latitude: 48.863112 longitude: 2.341214 }
+ end_location { latitude: 48.863120 longitude: 2.341215 }
+ load_limits {
+ key: "weight"
+ value: { max_load: 60 }
+ }
+ }
+ }
\ No newline at end of file
diff --git a/optimization/snippets/src/main/java/com/example/optimizationai/SyncApi.java b/optimization/snippets/src/main/java/com/example/optimizationai/SyncApi.java
new file mode 100644
index 00000000000..5e73344b17b
--- /dev/null
+++ b/optimization/snippets/src/main/java/com/example/optimizationai/SyncApi.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2022 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.optimizationai;
+
+// [START cloudoptimization_sync_api]
+
+import com.google.cloud.optimization.v1.FleetRoutingClient;
+import com.google.cloud.optimization.v1.OptimizeToursRequest;
+import com.google.cloud.optimization.v1.OptimizeToursResponse;
+import com.google.protobuf.Duration;
+import com.google.protobuf.TextFormat;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+
+/**
+ * This is an example to send a request to Cloud Fleet Routing synchronous API via Java API Client.
+ * A sample sync_request.textproto file can be found in the resources folder.
+ */
+public class SyncApi {
+ public static void callSyncApi() throws Exception {
+ // TODO(developer): Replace these variables before running the sample.
+ String projectParent = "projects/{YOUR_GCP_PROJECT_ID}";
+ String modelPath = "YOUR_MODEL_PATH";
+ callSyncApi(projectParent, modelPath);
+ }
+
+ public static void callSyncApi(String projectParent, String modelPath) throws Exception {
+ int timeoutSeconds = 100;
+ InputStream modelInputstream = new FileInputStream(modelPath);
+ Reader modelInputStreamReader = new InputStreamReader(modelInputstream);
+ OptimizeToursRequest.Builder requestBuilder =
+ OptimizeToursRequest.newBuilder()
+ .setTimeout(Duration.newBuilder().setSeconds(timeoutSeconds).build())
+ .setParent(projectParent);
+ TextFormat.getParser().merge(modelInputStreamReader, requestBuilder);
+ FleetRoutingClient fleetRoutingClient = FleetRoutingClient.create();
+ OptimizeToursResponse response = fleetRoutingClient.optimizeTours(requestBuilder.build());
+ System.out.println(response.toString());
+ }
+}
+// [END cloudoptimization_sync_api]
diff --git a/optimization/snippets/src/test/java/com/example/optimizationai/SyncApiTest.java b/optimization/snippets/src/test/java/com/example/optimizationai/SyncApiTest.java
new file mode 100644
index 00000000000..e663a924584
--- /dev/null
+++ b/optimization/snippets/src/test/java/com/example/optimizationai/SyncApiTest.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2022 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.optimizationai;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/** Tests for SyncApi sample. */
+public class SyncApiTest {
+ private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
+ private static final String PROJECT_PARENT = String.format("projects/%s", PROJECT_ID);
+ private static final String MODEL_PATH = "resources/sync_request.textproto";
+
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+ private PrintStream originalPrintStream;
+
+ @Before
+ public void setUp() {
+ bout = new ByteArrayOutputStream();
+ out = new PrintStream(bout);
+ originalPrintStream = System.out;
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() {
+ System.out.flush();
+ System.setOut(originalPrintStream);
+ }
+
+ @Test
+ public void testSyncApi() throws Exception {
+ SyncApi.callSyncApi(PROJECT_PARENT, MODEL_PATH);
+ String got = bout.toString();
+ assertThat(got).contains("routes");
+ }
+}