-
Notifications
You must be signed in to change notification settings - Fork 6.6k
feat: Create sample for routeoptimization #12118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Google Maps Platform: Route Optimization API Examples | ||
|
||
## Prerequisites to run locally: | ||
|
||
* [pip](https://pypi.python.org/pypi/pip) | ||
|
||
Go to the [Google Cloud Console](https://console.cloud.google.com). | ||
|
||
Under API Manager, search for the Route Optimization API and enable it. | ||
|
||
## Set Up Your Local Dev Environment | ||
|
||
To install, run the following commands. If you want to use [virtualenv](https://virtualenv.readthedocs.org/en/latest/) | ||
(recommended), run the commands within a virtualenv. | ||
|
||
* pip install -r requirements.txt | ||
|
||
## Authentication | ||
|
||
Please see the [Google cloud authentication guide](https://cloud.google.com/docs/authentication/). |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Copyright 2021 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. | ||
|
||
# Default TEST_CONFIG_OVERRIDE for python repos. | ||
|
||
# You can copy this file into your directory, then it will be imported from | ||
# the noxfile.py. | ||
|
||
# The source of truth: | ||
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py | ||
|
||
TEST_CONFIG_OVERRIDE = { | ||
# You can opt out from the test for specific Python versions. | ||
"ignored_versions": ["2.7", "3.7"], | ||
# Old samples are opted out of enforcing Python type hints | ||
# All new samples should feature them | ||
"enforce_type_hints": True, | ||
# An envvar key for determining the project id to use. Change it | ||
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a | ||
# build specific Cloud project. You can also use your own string | ||
# to use your own Cloud project. | ||
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT", | ||
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', | ||
# If you need to use a specific version of pip, | ||
# change pip_version_override to the string representation | ||
# of the version number, for example, "20.2.4" | ||
"pip_version_override": None, | ||
# A dictionary you want to inject into your test. Don't put any | ||
# secrets here. These values will override predefined values. | ||
"envs": {}, | ||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,59 @@ | ||||||||
# Copyright 2024 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 routeoptimization_optimize_tours_basic] | ||||||||
|
||||||||
from google.maps import routeoptimization_v1 | ||||||||
|
||||||||
|
||||||||
def call_optimize_tours(project_id: str) -> routeoptimization_v1.OptimizeToursResponse: | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a good practice to include a docstring briefly explaining what the function does.
Suggested change
|
||||||||
# Use Default Application Credentials for the environment. | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a good practice to explicitly set the
Suggested change
|
||||||||
client = routeoptimization_v1.RouteOptimizationClient() | ||||||||
|
||||||||
response = client.optimize_tours( | ||||||||
request=routeoptimization_v1.OptimizeToursRequest( | ||||||||
parent="projects/" + project_id, | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||
model={ | ||||||||
"shipments": [ | ||||||||
{ | ||||||||
"deliveries": [ | ||||||||
{ | ||||||||
"arrival_location": { | ||||||||
"latitude": 48.880942, | ||||||||
"longitude": 2.323866, | ||||||||
} | ||||||||
} | ||||||||
] | ||||||||
} | ||||||||
], | ||||||||
"vehicles": [ | ||||||||
{ | ||||||||
"end_location": { | ||||||||
"latitude": 48.86311, | ||||||||
"longitude": 2.341205, | ||||||||
}, | ||||||||
"start_location": { | ||||||||
"latitude": 48.863102, | ||||||||
"longitude": 2.341204, | ||||||||
}, | ||||||||
} | ||||||||
], | ||||||||
}, | ||||||||
) | ||||||||
) | ||||||||
|
||||||||
return response | ||||||||
|
||||||||
|
||||||||
# [END routeoptimization_optimize_tours_basic] |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,29 @@ | ||||||||
# Copyright 2024 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. | ||||||||
|
||||||||
from google.api_core.retry import Retry | ||||||||
import google.auth | ||||||||
|
||||||||
|
||||||||
import optimize_tours | ||||||||
|
||||||||
|
||||||||
@Retry | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||
def test_call_sync_api() -> None: | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a good practice to explicitly set the
Suggested change
|
||||||||
_, project_id = google.auth.default() | ||||||||
got = optimize_tours.call_optimize_tours(project_id) | ||||||||
|
||||||||
assert len(got.routes) > 0 | ||||||||
assert len(got.routes[0].visits) > 0 | ||||||||
assert got.metrics is not None |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pytest==8.2.0 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
google-maps-routeoptimization==0.1.2 |
Uh oh!
There was an error while loading. Please reload this page.