-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add a code snippet for the sync api (#24)
* Added a sync api code snippet
- Loading branch information
1 parent
715bbb3
commit c75d5f0
Showing
4 changed files
with
179 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
optimization/snippets/src/main/java/com/example/optimizationai/SyncApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
57 changes: 57 additions & 0 deletions
57
optimization/snippets/src/test/java/com/example/optimizationai/SyncApiTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} |