From eaeca38a2af86172af0ec43ff8149b1174bf6943 Mon Sep 17 00:00:00 2001 From: maciej-wichowski Date: Tue, 15 Oct 2024 12:06:05 +0200 Subject: [PATCH] [FIX] Multiple parameters for route_v8 --- herepy/__init__.py | 2 +- herepy/routing_api.py | 18 +++--------------- herepy/utils.py | 15 +++++++++++---- tests/test_routing_api.py | 19 +++++++++++++++++++ tests/test_utils.py | 8 ++++++++ 5 files changed, 42 insertions(+), 20 deletions(-) diff --git a/herepy/__init__.py b/herepy/__init__.py index 5e5b261..75ffd5c 100644 --- a/herepy/__init__.py +++ b/herepy/__init__.py @@ -6,7 +6,7 @@ __email__ = "abdullahselek.os@gmail.com" __copyright__ = "Copyright (c) 2017 Abdullah Selek" __license__ = "MIT License" -__version__ = "3.6.4" +__version__ = "3.6.5" __url__ = "https://github.com/abdullahselek/HerePy" __download_url__ = "https://pypi.org/pypi/herepy" __description__ = "A library that provides a Python interface to the HERE APIs" diff --git a/herepy/routing_api.py b/herepy/routing_api.py index c2e2df2..2e037a3 100644 --- a/herepy/routing_api.py +++ b/herepy/routing_api.py @@ -457,17 +457,9 @@ def route_v8( if alternatives: data["alternatives"] = alternatives if avoid: - key = list(avoid.keys())[0] - values = list(avoid.values())[0] - data["avoid"] = { - key: ",".join(values), - } + data['avoid'] = {key: ",".join(vals) for key, vals in avoid.items()} if exclude: - key = list(exclude.keys())[0] - values = list(exclude.values())[0] - data["exclude"] = { - key: ",".join(values), - } + data['exclude'] = {key: ",".join(vals) for key, vals in exclude.items()} if units: data["units"] = units.__str__() if lang: @@ -477,11 +469,7 @@ def route_v8( if span_fields: data["spans"] = ",".join([field.__str__() for field in span_fields]) if truck: - key = list(truck.keys())[0] - values = list(truck.values())[0] - data["truck"] = { - key: ",".join(values), - } + data['truck'] = {key: ",".join(vals) for key, vals in truck.items()} if scooter: data["scooter"] = scooter diff --git a/herepy/utils.py b/herepy/utils.py index ac63dfc..1740010 100644 --- a/herepy/utils.py +++ b/herepy/utils.py @@ -18,16 +18,23 @@ def encode_parameters(parameters): parameters (dict): dictionary of query parameters to be converted. Returns: - A URL-encoded string in "key=value&key=value" form + A URL-encoded string in "key=value&key=value" form. If the parameter value is + a dict, the encoded string is converted to "main_key[sub_key]=sub_value". """ if parameters is None: return None if not isinstance(parameters, dict): raise HEREError("`parameters` must be a dict.") else: - return urlencode( - dict((k, v) for k, v in parameters.items() if v is not None) - ) + parameters = dict((k, v) for k, v in parameters.items() if v is not None) + res = dict() + for key, value in parameters.items(): + if isinstance(value, dict): + for key2, val2 in value.items(): + res[f"{key}[{key2}]"] = val2 + else: + res[key] = value + return urlencode(res) @staticmethod def build_url(url, extra_params=None): diff --git a/tests/test_routing_api.py b/tests/test_routing_api.py index 89b2169..36d7073 100644 --- a/tests/test_routing_api.py +++ b/tests/test_routing_api.py @@ -1215,3 +1215,22 @@ def test_route_v8_multiple_via_points(self): ) self.assertTrue(response) self.assertIsInstance(response, herepy.RoutingResponseV8) + + @responses.activate + def test_route_v8_url_parameters_multiple(self): + responses.add( + responses.GET, + "https://router.hereapi.com/v8/routes", + "{}", + status=200, + match=[ + responses.matchers.query_param_matcher( + {"truck[height]": "15000", "truck[width]": "3000"}, strict_match=False + ) + ], + ) + self._api.route_v8(transport_mode=herepy.RoutingTransportMode.truck, + origin=[41.9798, -87.8801], + destination=[41.9043, -87.9216], + truck={'height': ['15000'], 'width': ['3000']} + ) diff --git a/tests/test_utils.py b/tests/test_utils.py index c9684d5..8afc183 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -29,3 +29,11 @@ def test_buildurl(self): "https://geocoder.cit.api.here.com/6.2/geocode.json", data ) self.assertTrue(url) + + def test_build_url_sub_data(self): + data = { + "key1": "val", + "key2": {"sub_key": "sub_val", "sub_key2": "sub_val2"}, + } + url = Utils.build_url("https://router.hereapi.com/v8/routes", data) + self.assertEqual(url, "https://router.hereapi.com/v8/routes?key1=val&key2%5Bsub_key%5D=sub_val&key2%5Bsub_key2%5D=sub_val2")