From b936a7b9444593dc3fcd87d899d61a3af90ac4a9 Mon Sep 17 00:00:00 2001 From: Bo Shi Date: Wed, 7 Aug 2024 21:45:42 +0000 Subject: [PATCH] feat: Create sample for routeoptimization --- .github/CODEOWNERS | 1 + .github/blunderbuss.yml | 4 ++ routeoptimization/snippets/README.rst | 20 +++++++ routeoptimization/snippets/noxfile_config.py | 42 +++++++++++++ routeoptimization/snippets/optimize_tours.py | 59 +++++++++++++++++++ .../snippets/optimize_tours_test.py | 29 +++++++++ .../snippets/requirements-test.txt | 2 + routeoptimization/snippets/requirements.txt | 1 + 8 files changed, 158 insertions(+) create mode 100644 routeoptimization/snippets/README.rst create mode 100644 routeoptimization/snippets/noxfile_config.py create mode 100644 routeoptimization/snippets/optimize_tours.py create mode 100644 routeoptimization/snippets/optimize_tours_test.py create mode 100644 routeoptimization/snippets/requirements-test.txt create mode 100644 routeoptimization/snippets/requirements.txt diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index b40af77812f..00a74bb5675 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -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 diff --git a/.github/blunderbuss.yml b/.github/blunderbuss.yml index 7ef4ca61693..d25c4b3f0df 100644 --- a/.github/blunderbuss.yml +++ b/.github/blunderbuss.yml @@ -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" diff --git a/routeoptimization/snippets/README.rst b/routeoptimization/snippets/README.rst new file mode 100644 index 00000000000..ad7d27048bf --- /dev/null +++ b/routeoptimization/snippets/README.rst @@ -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/). diff --git a/routeoptimization/snippets/noxfile_config.py b/routeoptimization/snippets/noxfile_config.py new file mode 100644 index 00000000000..ae3ed14dc36 --- /dev/null +++ b/routeoptimization/snippets/noxfile_config.py @@ -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": {}, +} diff --git a/routeoptimization/snippets/optimize_tours.py b/routeoptimization/snippets/optimize_tours.py new file mode 100644 index 00000000000..6e2edabc680 --- /dev/null +++ b/routeoptimization/snippets/optimize_tours.py @@ -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: + # Use Default Application Credentials for the environment. + client = routeoptimization_v1.RouteOptimizationClient() + + response = client.optimize_tours( + request=routeoptimization_v1.OptimizeToursRequest( + parent="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] diff --git a/routeoptimization/snippets/optimize_tours_test.py b/routeoptimization/snippets/optimize_tours_test.py new file mode 100644 index 00000000000..64fff60eeef --- /dev/null +++ b/routeoptimization/snippets/optimize_tours_test.py @@ -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 +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 diff --git a/routeoptimization/snippets/requirements-test.txt b/routeoptimization/snippets/requirements-test.txt new file mode 100644 index 00000000000..24ad1f29818 --- /dev/null +++ b/routeoptimization/snippets/requirements-test.txt @@ -0,0 +1,2 @@ +pytest==8.2.0 + diff --git a/routeoptimization/snippets/requirements.txt b/routeoptimization/snippets/requirements.txt new file mode 100644 index 00000000000..7bfb3d0e194 --- /dev/null +++ b/routeoptimization/snippets/requirements.txt @@ -0,0 +1 @@ +google-maps-routeoptimization==0.1.2