Skip to content

Commit

Permalink
[FIX] Multiple parameters for route_v8
Browse files Browse the repository at this point in the history
  • Loading branch information
maciej-wichowski committed Oct 15, 2024
1 parent a8f1b58 commit eaeca38
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 20 deletions.
2 changes: 1 addition & 1 deletion herepy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
__email__ = "[email protected]"
__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"
Expand Down
18 changes: 3 additions & 15 deletions herepy/routing_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down
15 changes: 11 additions & 4 deletions herepy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
19 changes: 19 additions & 0 deletions tests/test_routing_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']}
)
8 changes: 8 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit eaeca38

Please sign in to comment.