Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
/video/live-stream/* @GoogleCloudPlatform/cloud-media-team @GoogleCloudPlatform/python-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
/video/stitcher/* @GoogleCloudPlatform/cloud-media-team @GoogleCloudPlatform/python-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
/translate @GoogleCloudPlatform/python-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/cloud-ml-translate-dev
/routeoptimization/**/* @GoogleCloudPlatform/geo-routeoptimization @GoogleCloudPlatform/python-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers

# BEGIN - pending clarification
/memorystore/**/* @GoogleCloudPlatform/python-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
Expand Down
4 changes: 4 additions & 0 deletions .github/blunderbuss.yml
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ assign_prs_by:
- "api: dataplex"
to:
- GoogleCloudPlatform/googleapi-dataplex
- labels:
- "api: routeoptimization"
to:
- GoogleCloudPlatform/geo-routeoptimization
# Self-service individuals
- labels:
- "api: auth"
Expand Down
20 changes: 20 additions & 0 deletions routeoptimization/snippets/README.rst
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/).
42 changes: 42 additions & 0 deletions routeoptimization/snippets/noxfile_config.py
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": {},
}
59 changes: 59 additions & 0 deletions routeoptimization/snippets/optimize_tours.py
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:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

It's a good practice to include a docstring briefly explaining what the function does.

Suggested change
def call_optimize_tours(project_id: str) -> routeoptimization_v1.OptimizeToursResponse:
def call_optimize_tours(project_id: str) -> routeoptimization_v1.OptimizeToursResponse:
"""Calls the optimize tours method."""

# Use Default Application Credentials for the environment.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

It's a good practice to explicitly set the retry argument. Refer to this documentation for more details: https://googleapis.dev/python/google-api-core/latest/retry.html

Suggested change
# Use Default Application Credentials for the environment.
client = routeoptimization_v1.RouteOptimizationClient(client_options={'api_endpoint': 'us-central1-routeoptimization.googleapis.com:443'})

client = routeoptimization_v1.RouteOptimizationClient()

response = client.optimize_tours(
request=routeoptimization_v1.OptimizeToursRequest(
parent="projects/" + project_id,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

Use f-strings for better readability.

Suggested change
parent="projects/" + project_id,
parent=f"projects/{project_id}",

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]
29 changes: 29 additions & 0 deletions routeoptimization/snippets/optimize_tours_test.py
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

It is a good practice to include a docstring briefly explaining what the test does.

Suggested change
@Retry
@Retry
def test_call_sync_api() -> None:
"""Tests optimize tours method."""

def test_call_sync_api() -> None:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

It's a good practice to explicitly set the retry argument. Refer to this documentation for more details: https://googleapis.dev/python/google-api-core/latest/retry.html

Suggested change
def test_call_sync_api() -> None:
@Retry(deadline=600)
def test_call_sync_api() -> None:

_, 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
2 changes: 2 additions & 0 deletions routeoptimization/snippets/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pytest==8.2.0

1 change: 1 addition & 0 deletions routeoptimization/snippets/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
google-maps-routeoptimization==0.1.2