diff --git a/google/cloud/optimization_v1/gapic_metadata.json b/google/cloud/optimization_v1/gapic_metadata.json index 2c7b228..c9c5016 100644 --- a/google/cloud/optimization_v1/gapic_metadata.json +++ b/google/cloud/optimization_v1/gapic_metadata.json @@ -36,6 +36,21 @@ ] } } + }, + "rest": { + "libraryClient": "FleetRoutingClient", + "rpcs": { + "BatchOptimizeTours": { + "methods": [ + "batch_optimize_tours" + ] + }, + "OptimizeTours": { + "methods": [ + "optimize_tours" + ] + } + } } } } diff --git a/google/cloud/optimization_v1/services/fleet_routing/client.py b/google/cloud/optimization_v1/services/fleet_routing/client.py index c25a91c..934defa 100644 --- a/google/cloud/optimization_v1/services/fleet_routing/client.py +++ b/google/cloud/optimization_v1/services/fleet_routing/client.py @@ -54,6 +54,7 @@ from .transports.base import FleetRoutingTransport, DEFAULT_CLIENT_INFO from .transports.grpc import FleetRoutingGrpcTransport from .transports.grpc_asyncio import FleetRoutingGrpcAsyncIOTransport +from .transports.rest import FleetRoutingRestTransport class FleetRoutingClientMeta(type): @@ -67,6 +68,7 @@ class FleetRoutingClientMeta(type): _transport_registry = OrderedDict() # type: Dict[str, Type[FleetRoutingTransport]] _transport_registry["grpc"] = FleetRoutingGrpcTransport _transport_registry["grpc_asyncio"] = FleetRoutingGrpcAsyncIOTransport + _transport_registry["rest"] = FleetRoutingRestTransport def get_transport_class( cls, diff --git a/google/cloud/optimization_v1/services/fleet_routing/transports/__init__.py b/google/cloud/optimization_v1/services/fleet_routing/transports/__init__.py index 9e17254..69798ab 100644 --- a/google/cloud/optimization_v1/services/fleet_routing/transports/__init__.py +++ b/google/cloud/optimization_v1/services/fleet_routing/transports/__init__.py @@ -19,15 +19,20 @@ from .base import FleetRoutingTransport from .grpc import FleetRoutingGrpcTransport from .grpc_asyncio import FleetRoutingGrpcAsyncIOTransport +from .rest import FleetRoutingRestTransport +from .rest import FleetRoutingRestInterceptor # Compile a registry of transports. _transport_registry = OrderedDict() # type: Dict[str, Type[FleetRoutingTransport]] _transport_registry["grpc"] = FleetRoutingGrpcTransport _transport_registry["grpc_asyncio"] = FleetRoutingGrpcAsyncIOTransport +_transport_registry["rest"] = FleetRoutingRestTransport __all__ = ( "FleetRoutingTransport", "FleetRoutingGrpcTransport", "FleetRoutingGrpcAsyncIOTransport", + "FleetRoutingRestTransport", + "FleetRoutingRestInterceptor", ) diff --git a/google/cloud/optimization_v1/services/fleet_routing/transports/rest.py b/google/cloud/optimization_v1/services/fleet_routing/transports/rest.py new file mode 100644 index 0000000..3753096 --- /dev/null +++ b/google/cloud/optimization_v1/services/fleet_routing/transports/rest.py @@ -0,0 +1,637 @@ +# -*- coding: utf-8 -*- +# 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. +# + +from google.auth.transport.requests import AuthorizedSession # type: ignore +import json # type: ignore +import grpc # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore +from google.auth import credentials as ga_credentials # type: ignore +from google.api_core import exceptions as core_exceptions +from google.api_core import retry as retries +from google.api_core import rest_helpers +from google.api_core import rest_streaming +from google.api_core import path_template +from google.api_core import gapic_v1 + +from google.protobuf import json_format +from google.api_core import operations_v1 +from google.longrunning import operations_pb2 +from requests import __version__ as requests_version +import dataclasses +import re +from typing import Callable, Dict, List, Optional, Sequence, Tuple, Union +import warnings + +try: + OptionalRetry = Union[retries.Retry, gapic_v1.method._MethodDefault] +except AttributeError: # pragma: NO COVER + OptionalRetry = Union[retries.Retry, object] # type: ignore + + +from google.cloud.optimization_v1.types import fleet_routing +from google.longrunning import operations_pb2 # type: ignore + +from .base import FleetRoutingTransport, DEFAULT_CLIENT_INFO as BASE_DEFAULT_CLIENT_INFO + + +DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=BASE_DEFAULT_CLIENT_INFO.gapic_version, + grpc_version=None, + rest_version=requests_version, +) + + +class FleetRoutingRestInterceptor: + """Interceptor for FleetRouting. + + Interceptors are used to manipulate requests, request metadata, and responses + in arbitrary ways. + Example use cases include: + * Logging + * Verifying requests according to service or custom semantics + * Stripping extraneous information from responses + + These use cases and more can be enabled by injecting an + instance of a custom subclass when constructing the FleetRoutingRestTransport. + + .. code-block:: python + class MyCustomFleetRoutingInterceptor(FleetRoutingRestInterceptor): + def pre_batch_optimize_tours(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_batch_optimize_tours(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_optimize_tours(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_optimize_tours(self, response): + logging.log(f"Received response: {response}") + return response + + transport = FleetRoutingRestTransport(interceptor=MyCustomFleetRoutingInterceptor()) + client = FleetRoutingClient(transport=transport) + + + """ + + def pre_batch_optimize_tours( + self, + request: fleet_routing.BatchOptimizeToursRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[fleet_routing.BatchOptimizeToursRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for batch_optimize_tours + + Override in a subclass to manipulate the request or metadata + before they are sent to the FleetRouting server. + """ + return request, metadata + + def post_batch_optimize_tours( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for batch_optimize_tours + + Override in a subclass to manipulate the response + after it is returned by the FleetRouting server but before + it is returned to user code. + """ + return response + + def pre_optimize_tours( + self, + request: fleet_routing.OptimizeToursRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[fleet_routing.OptimizeToursRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for optimize_tours + + Override in a subclass to manipulate the request or metadata + before they are sent to the FleetRouting server. + """ + return request, metadata + + def post_optimize_tours( + self, response: fleet_routing.OptimizeToursResponse + ) -> fleet_routing.OptimizeToursResponse: + """Post-rpc interceptor for optimize_tours + + Override in a subclass to manipulate the response + after it is returned by the FleetRouting server but before + it is returned to user code. + """ + return response + + def pre_get_operation( + self, + request: operations_pb2.GetOperationRequest, + metadata: Sequence[Tuple[str, str]], + ) -> operations_pb2.Operation: + """Pre-rpc interceptor for get_operation + + Override in a subclass to manipulate the request or metadata + before they are sent to the FleetRouting server. + """ + return request, metadata + + def post_get_operation( + self, response: operations_pb2.GetOperationRequest + ) -> operations_pb2.Operation: + """Post-rpc interceptor for get_operation + + Override in a subclass to manipulate the response + after it is returned by the FleetRouting server but before + it is returned to user code. + """ + return response + + +@dataclasses.dataclass +class FleetRoutingRestStub: + _session: AuthorizedSession + _host: str + _interceptor: FleetRoutingRestInterceptor + + +class FleetRoutingRestTransport(FleetRoutingTransport): + """REST backend transport for FleetRouting. + + A service for optimizing vehicle tours. + + Validity of certain types of fields: + + - ``google.protobuf.Timestamp`` + + - Times are in Unix time: seconds since + 1970-01-01T00:00:00+00:00. + - seconds must be in [0, 253402300799], i.e. in + [1970-01-01T00:00:00+00:00, 9999-12-31T23:59:59+00:00]. + - nanos must be unset or set to 0. + + - ``google.protobuf.Duration`` + + - seconds must be in [0, 253402300799], i.e. in + [1970-01-01T00:00:00+00:00, 9999-12-31T23:59:59+00:00]. + - nanos must be unset or set to 0. + + - ``google.type.LatLng`` + + - latitude must be in [-90.0, 90.0]. + - longitude must be in [-180.0, 180.0]. + - at least one of latitude and longitude must be non-zero. + + This class defines the same methods as the primary client, so the + primary client can load the underlying transport implementation + and call it. + + It sends JSON representations of protocol buffers over HTTP/1.1 + + """ + + def __init__( + self, + *, + host: str = "cloudoptimization.googleapis.com", + credentials: Optional[ga_credentials.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + client_cert_source_for_mtls: Optional[Callable[[], Tuple[bytes, bytes]]] = None, + quota_project_id: Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + always_use_jwt_access: Optional[bool] = False, + url_scheme: str = "https", + interceptor: Optional[FleetRoutingRestInterceptor] = None, + api_audience: Optional[str] = None, + ) -> None: + """Instantiate the transport. + + Args: + host (Optional[str]): + The hostname to connect to. + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is ignored if ``channel`` is provided. + scopes (Optional(Sequence[str])): A list of scopes. This argument is + ignored if ``channel`` is provided. + client_cert_source_for_mtls (Callable[[], Tuple[bytes, bytes]]): Client + certificate to configure mutual TLS HTTP channel. It is ignored + if ``channel`` is provided. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you are developing + your own client library. + always_use_jwt_access (Optional[bool]): Whether self signed JWT should + be used for service account credentials. + url_scheme: the protocol scheme for the API endpoint. Normally + "https", but for testing or local servers, + "http" can be specified. + """ + # Run the base constructor + # TODO(yon-mg): resolve other ctor params i.e. scopes, quota, etc. + # TODO: When custom host (api_endpoint) is set, `scopes` must *also* be set on the + # credentials object + maybe_url_match = re.match("^(?Phttp(?:s)?://)?(?P.*)$", host) + if maybe_url_match is None: + raise ValueError( + f"Unexpected hostname structure: {host}" + ) # pragma: NO COVER + + url_match_items = maybe_url_match.groupdict() + + host = f"{url_scheme}://{host}" if not url_match_items["scheme"] else host + + super().__init__( + host=host, + credentials=credentials, + client_info=client_info, + always_use_jwt_access=always_use_jwt_access, + api_audience=api_audience, + ) + self._session = AuthorizedSession( + self._credentials, default_host=self.DEFAULT_HOST + ) + self._operations_client: Optional[operations_v1.AbstractOperationsClient] = None + if client_cert_source_for_mtls: + self._session.configure_mtls_channel(client_cert_source_for_mtls) + self._interceptor = interceptor or FleetRoutingRestInterceptor() + self._prep_wrapped_messages(client_info) + + @property + def operations_client(self) -> operations_v1.AbstractOperationsClient: + """Create the client designed to process long-running operations. + + This property caches on the instance; repeated calls return the same + client. + """ + # Only create a new client if we do not already have one. + if self._operations_client is None: + http_options: Dict[str, List[Dict[str, str]]] = { + "google.longrunning.Operations.GetOperation": [ + { + "method": "get", + "uri": "/v1/{name=projects/*/operations/*}", + }, + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/operations/*}", + }, + ], + } + + rest_transport = operations_v1.OperationsRestTransport( + host=self._host, + # use the credentials which are saved + credentials=self._credentials, + scopes=self._scopes, + http_options=http_options, + path_prefix="v1", + ) + + self._operations_client = operations_v1.AbstractOperationsClient( + transport=rest_transport + ) + + # Return the client from cache. + return self._operations_client + + class _BatchOptimizeTours(FleetRoutingRestStub): + def __hash__(self): + return hash("BatchOptimizeTours") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: fleet_routing.BatchOptimizeToursRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the batch optimize tours method over HTTP. + + Args: + request (~.fleet_routing.BatchOptimizeToursRequest): + The request object. Request to batch optimize tours as an asynchronous + operation. Each input file should contain one + ``OptimizeToursRequest``, and each output file will + contain one ``OptimizeToursResponse``. The request + contains information to read/write and parse the files. + All the input and output files should be under the same + project. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{parent=projects/*/locations/*}:batchOptimizeTours", + "body": "*", + }, + { + "method": "post", + "uri": "/v1/{parent=projects/*}:batchOptimizeTours", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_batch_optimize_tours( + request, metadata + ) + pb_request = fleet_routing.BatchOptimizeToursRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_batch_optimize_tours(resp) + return resp + + class _OptimizeTours(FleetRoutingRestStub): + def __hash__(self): + return hash("OptimizeTours") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: fleet_routing.OptimizeToursRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> fleet_routing.OptimizeToursResponse: + r"""Call the optimize tours method over HTTP. + + Args: + request (~.fleet_routing.OptimizeToursRequest): + The request object. Request to be given to a tour + optimization solver which defines the + shipment model to solve as well as + optimization parameters. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.fleet_routing.OptimizeToursResponse: + Response after solving a tour + optimization problem containing the + routes followed by each vehicle, the + shipments which have been skipped and + the overall cost of the solution. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{parent=projects/*/locations/*}:optimizeTours", + "body": "*", + }, + { + "method": "post", + "uri": "/v1/{parent=projects/*}:optimizeTours", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_optimize_tours(request, metadata) + pb_request = fleet_routing.OptimizeToursRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = fleet_routing.OptimizeToursResponse() + pb_resp = fleet_routing.OptimizeToursResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_optimize_tours(resp) + return resp + + @property + def batch_optimize_tours( + self, + ) -> Callable[[fleet_routing.BatchOptimizeToursRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._BatchOptimizeTours(self._session, self._host, self._interceptor) # type: ignore + + @property + def optimize_tours( + self, + ) -> Callable[ + [fleet_routing.OptimizeToursRequest], fleet_routing.OptimizeToursResponse + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._OptimizeTours(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_operation(self): + return self._GetOperation(self._session, self._host, self._interceptor) # type: ignore + + class _GetOperation(FleetRoutingRestStub): + def __call__( + self, + request: operations_pb2.GetOperationRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + + r"""Call the get operation method over HTTP. + + Args: + request (operations_pb2.GetOperationRequest): + The request object for GetOperation method. + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + operations_pb2.Operation: Response from GetOperation method. + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/operations/*}", + }, + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/operations/*}", + }, + ] + + request, metadata = self._interceptor.pre_get_operation(request, metadata) + request_kwargs = json_format.MessageToDict(request) + transcoded_request = path_template.transcode(http_options, **request_kwargs) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads(json.dumps(transcoded_request["query_params"])) + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + resp = operations_pb2.Operation() + resp = json_format.Parse(response.content.decode("utf-8"), resp) + resp = self._interceptor.post_get_operation(resp) + return resp + + @property + def kind(self) -> str: + return "rest" + + def close(self): + self._session.close() + + +__all__ = ("FleetRoutingRestTransport",) diff --git a/tests/unit/gapic/optimization_v1/test_fleet_routing.py b/tests/unit/gapic/optimization_v1/test_fleet_routing.py index 648af90..77764ba 100644 --- a/tests/unit/gapic/optimization_v1/test_fleet_routing.py +++ b/tests/unit/gapic/optimization_v1/test_fleet_routing.py @@ -24,10 +24,17 @@ import grpc from grpc.experimental import aio +from collections.abc import Iterable +from google.protobuf import json_format +import json import math import pytest from proto.marshal.rules.dates import DurationRule, TimestampRule from proto.marshal.rules import wrappers +from requests import Response +from requests import Request, PreparedRequest +from requests.sessions import Session +from google.protobuf import json_format from google.api_core import client_options from google.api_core import exceptions as core_exceptions @@ -100,6 +107,7 @@ def test__get_default_mtls_endpoint(): [ (FleetRoutingClient, "grpc"), (FleetRoutingAsyncClient, "grpc_asyncio"), + (FleetRoutingClient, "rest"), ], ) def test_fleet_routing_client_from_service_account_info(client_class, transport_name): @@ -113,7 +121,11 @@ def test_fleet_routing_client_from_service_account_info(client_class, transport_ assert client.transport._credentials == creds assert isinstance(client, client_class) - assert client.transport._host == ("cloudoptimization.googleapis.com:443") + assert client.transport._host == ( + "cloudoptimization.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://cloudoptimization.googleapis.com" + ) @pytest.mark.parametrize( @@ -121,6 +133,7 @@ def test_fleet_routing_client_from_service_account_info(client_class, transport_ [ (transports.FleetRoutingGrpcTransport, "grpc"), (transports.FleetRoutingGrpcAsyncIOTransport, "grpc_asyncio"), + (transports.FleetRoutingRestTransport, "rest"), ], ) def test_fleet_routing_client_service_account_always_use_jwt( @@ -146,6 +159,7 @@ def test_fleet_routing_client_service_account_always_use_jwt( [ (FleetRoutingClient, "grpc"), (FleetRoutingAsyncClient, "grpc_asyncio"), + (FleetRoutingClient, "rest"), ], ) def test_fleet_routing_client_from_service_account_file(client_class, transport_name): @@ -166,13 +180,18 @@ def test_fleet_routing_client_from_service_account_file(client_class, transport_ assert client.transport._credentials == creds assert isinstance(client, client_class) - assert client.transport._host == ("cloudoptimization.googleapis.com:443") + assert client.transport._host == ( + "cloudoptimization.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://cloudoptimization.googleapis.com" + ) def test_fleet_routing_client_get_transport_class(): transport = FleetRoutingClient.get_transport_class() available_transports = [ transports.FleetRoutingGrpcTransport, + transports.FleetRoutingRestTransport, ] assert transport in available_transports @@ -189,6 +208,7 @@ def test_fleet_routing_client_get_transport_class(): transports.FleetRoutingGrpcAsyncIOTransport, "grpc_asyncio", ), + (FleetRoutingClient, transports.FleetRoutingRestTransport, "rest"), ], ) @mock.patch.object( @@ -332,6 +352,8 @@ def test_fleet_routing_client_client_options( "grpc_asyncio", "false", ), + (FleetRoutingClient, transports.FleetRoutingRestTransport, "rest", "true"), + (FleetRoutingClient, transports.FleetRoutingRestTransport, "rest", "false"), ], ) @mock.patch.object( @@ -525,6 +547,7 @@ def test_fleet_routing_client_get_mtls_endpoint_and_cert_source(client_class): transports.FleetRoutingGrpcAsyncIOTransport, "grpc_asyncio", ), + (FleetRoutingClient, transports.FleetRoutingRestTransport, "rest"), ], ) def test_fleet_routing_client_client_options_scopes( @@ -565,6 +588,7 @@ def test_fleet_routing_client_client_options_scopes( "grpc_asyncio", grpc_helpers_async, ), + (FleetRoutingClient, transports.FleetRoutingRestTransport, "rest", None), ], ) def test_fleet_routing_client_client_options_credentials_file( @@ -984,6 +1008,438 @@ async def test_batch_optimize_tours_field_headers_async(): ) in kw["metadata"] +@pytest.mark.parametrize( + "request_type", + [ + fleet_routing.OptimizeToursRequest, + dict, + ], +) +def test_optimize_tours_rest(request_type): + client = FleetRoutingClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = fleet_routing.OptimizeToursResponse( + request_label="request_label_value", + total_cost=0.10840000000000001, + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = fleet_routing.OptimizeToursResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.optimize_tours(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, fleet_routing.OptimizeToursResponse) + assert response.request_label == "request_label_value" + assert math.isclose(response.total_cost, 0.10840000000000001, rel_tol=1e-6) + + +def test_optimize_tours_rest_required_fields( + request_type=fleet_routing.OptimizeToursRequest, +): + transport_class = transports.FleetRoutingRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).optimize_tours._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).optimize_tours._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = FleetRoutingClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = fleet_routing.OptimizeToursResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = fleet_routing.OptimizeToursResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.optimize_tours(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_optimize_tours_rest_unset_required_fields(): + transport = transports.FleetRoutingRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.optimize_tours._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("parent",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_optimize_tours_rest_interceptors(null_interceptor): + transport = transports.FleetRoutingRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.FleetRoutingRestInterceptor(), + ) + client = FleetRoutingClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.FleetRoutingRestInterceptor, "post_optimize_tours" + ) as post, mock.patch.object( + transports.FleetRoutingRestInterceptor, "pre_optimize_tours" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = fleet_routing.OptimizeToursRequest.pb( + fleet_routing.OptimizeToursRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = fleet_routing.OptimizeToursResponse.to_json( + fleet_routing.OptimizeToursResponse() + ) + + request = fleet_routing.OptimizeToursRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = fleet_routing.OptimizeToursResponse() + + client.optimize_tours( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_optimize_tours_rest_bad_request( + transport: str = "rest", request_type=fleet_routing.OptimizeToursRequest +): + client = FleetRoutingClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.optimize_tours(request) + + +def test_optimize_tours_rest_error(): + client = FleetRoutingClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + fleet_routing.BatchOptimizeToursRequest, + dict, + ], +) +def test_batch_optimize_tours_rest(request_type): + client = FleetRoutingClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.batch_optimize_tours(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_batch_optimize_tours_rest_required_fields( + request_type=fleet_routing.BatchOptimizeToursRequest, +): + transport_class = transports.FleetRoutingRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).batch_optimize_tours._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).batch_optimize_tours._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = FleetRoutingClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.batch_optimize_tours(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_batch_optimize_tours_rest_unset_required_fields(): + transport = transports.FleetRoutingRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.batch_optimize_tours._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(()) + & set( + ( + "parent", + "modelConfigs", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_batch_optimize_tours_rest_interceptors(null_interceptor): + transport = transports.FleetRoutingRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.FleetRoutingRestInterceptor(), + ) + client = FleetRoutingClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.FleetRoutingRestInterceptor, "post_batch_optimize_tours" + ) as post, mock.patch.object( + transports.FleetRoutingRestInterceptor, "pre_batch_optimize_tours" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = fleet_routing.BatchOptimizeToursRequest.pb( + fleet_routing.BatchOptimizeToursRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = fleet_routing.BatchOptimizeToursRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.batch_optimize_tours( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_batch_optimize_tours_rest_bad_request( + transport: str = "rest", request_type=fleet_routing.BatchOptimizeToursRequest +): + client = FleetRoutingClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.batch_optimize_tours(request) + + +def test_batch_optimize_tours_rest_error(): + client = FleetRoutingClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.FleetRoutingGrpcTransport( @@ -1065,6 +1521,7 @@ def test_transport_get_channel(): [ transports.FleetRoutingGrpcTransport, transports.FleetRoutingGrpcAsyncIOTransport, + transports.FleetRoutingRestTransport, ], ) def test_transport_adc(transport_class): @@ -1079,6 +1536,7 @@ def test_transport_adc(transport_class): "transport_name", [ "grpc", + "rest", ], ) def test_transport_kind(transport_name): @@ -1215,6 +1673,7 @@ def test_fleet_routing_transport_auth_adc(transport_class): [ transports.FleetRoutingGrpcTransport, transports.FleetRoutingGrpcAsyncIOTransport, + transports.FleetRoutingRestTransport, ], ) def test_fleet_routing_transport_auth_gdch_credentials(transport_class): @@ -1309,11 +1768,40 @@ def test_fleet_routing_grpc_transport_client_cert_source_for_mtls(transport_clas ) +def test_fleet_routing_http_transport_client_cert_source_for_mtls(): + cred = ga_credentials.AnonymousCredentials() + with mock.patch( + "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" + ) as mock_configure_mtls_channel: + transports.FleetRoutingRestTransport( + credentials=cred, client_cert_source_for_mtls=client_cert_source_callback + ) + mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) + + +def test_fleet_routing_rest_lro_client(): + client = FleetRoutingClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + transport = client.transport + + # Ensure that we have a api-core operations client. + assert isinstance( + transport.operations_client, + operations_v1.AbstractOperationsClient, + ) + + # Ensure that subsequent calls to the property send the exact same object. + assert transport.operations_client is transport.operations_client + + @pytest.mark.parametrize( "transport_name", [ "grpc", "grpc_asyncio", + "rest", ], ) def test_fleet_routing_host_no_port(transport_name): @@ -1324,7 +1812,11 @@ def test_fleet_routing_host_no_port(transport_name): ), transport=transport_name, ) - assert client.transport._host == ("cloudoptimization.googleapis.com:443") + assert client.transport._host == ( + "cloudoptimization.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://cloudoptimization.googleapis.com" + ) @pytest.mark.parametrize( @@ -1332,6 +1824,7 @@ def test_fleet_routing_host_no_port(transport_name): [ "grpc", "grpc_asyncio", + "rest", ], ) def test_fleet_routing_host_with_port(transport_name): @@ -1342,7 +1835,36 @@ def test_fleet_routing_host_with_port(transport_name): ), transport=transport_name, ) - assert client.transport._host == ("cloudoptimization.googleapis.com:8000") + assert client.transport._host == ( + "cloudoptimization.googleapis.com:8000" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://cloudoptimization.googleapis.com:8000" + ) + + +@pytest.mark.parametrize( + "transport_name", + [ + "rest", + ], +) +def test_fleet_routing_client_transport_session_collision(transport_name): + creds1 = ga_credentials.AnonymousCredentials() + creds2 = ga_credentials.AnonymousCredentials() + client1 = FleetRoutingClient( + credentials=creds1, + transport=transport_name, + ) + client2 = FleetRoutingClient( + credentials=creds2, + transport=transport_name, + ) + session1 = client1.transport.optimize_tours._session + session2 = client2.transport.optimize_tours._session + assert session1 != session2 + session1 = client1.transport.batch_optimize_tours._session + session2 = client2.transport.batch_optimize_tours._session + assert session1 != session2 def test_fleet_routing_grpc_transport_channel(): @@ -1637,6 +2159,64 @@ async def test_transport_close_async(): close.assert_called_once() +def test_get_operation_rest_bad_request( + transport: str = "rest", request_type=operations_pb2.GetOperationRequest +): + client = FleetRoutingClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/operations/sample2"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.get_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.GetOperationRequest, + dict, + ], +) +def test_get_operation_rest(request_type): + client = FleetRoutingClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request_init = {"name": "projects/sample1/operations/sample2"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation() + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.get_operation(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) + + def test_get_operation(transport: str = "grpc"): client = FleetRoutingClient( credentials=ga_credentials.AnonymousCredentials(), @@ -1784,6 +2364,7 @@ async def test_get_operation_from_dict_async(): def test_transport_close(): transports = { + "rest": "_session", "grpc": "_grpc_channel", } @@ -1801,6 +2382,7 @@ def test_transport_close(): def test_client_ctx(): transports = [ + "rest", "grpc", ] for transport in transports: