forked from googleapis/python-optimization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_api.py
55 lines (45 loc) · 2.38 KB
/
async_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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.
# [START cloudoptimization_async_api]
from google.api_core.exceptions import GoogleAPICallError
from google.cloud import optimization_v1
# TODO(developer): Uncomment these variables before running the sample.
# project_id= 'YOUR_PROJECT_ID'
# request_file_name = 'YOUR_REQUEST_FILE_NAME'
# request_model_gcs_path = 'gs://YOUR_PROJECT/YOUR_BUCKET/YOUR_REQUEST_MODEL_PATH'
# model_solution_gcs_path = 'gs://YOUR_PROJECT/YOUR_BUCKET/YOUR_SOLUCTION_PATH'
def call_async_api(project_id: str, request_model_gcs_path: str, model_solution_gcs_path_prefix: str) -> None:
"""Call the async api for fleet routing."""
# Use the default credentials for the environment to authenticate the client.
fleet_routing_client = optimization_v1.FleetRoutingClient()
request_file_name = "resources/async_request.json"
with open(request_file_name, 'r') as f:
fleet_routing_request = optimization_v1.BatchOptimizeToursRequest.from_json(f.read())
fleet_routing_request.parent = f"projects/{project_id}"
for idx, mc in enumerate(fleet_routing_request.model_configs):
mc.input_config.gcs_source.uri = request_model_gcs_path
model_solution_gcs_path = f'{model_solution_gcs_path_prefix}_{idx}'
mc.output_config.gcs_destination.uri = model_solution_gcs_path
# The timeout argument for the gRPC call is independent from the `timeout`
# field in the request's OptimizeToursRequest message(s).
operation = fleet_routing_client.batch_optimize_tours(fleet_routing_request)
print(operation.operation.name)
try:
# Block to wait for the job to finish.
result = operation.result()
print(result)
# Do you stuff.
except GoogleAPICallError:
print(operation.operation.error)
# [END cloudoptimization_async_api]