Skip to content
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

[FIX] Multiple parameters for route_v8 #84

Merged
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
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)
result = dict()
for key, value in parameters.items():
if isinstance(value, dict):
for sub_key, sub_value in value.items():
result[f"{key}[{sub_key}]"] = sub_value
else:
result[key] = value
return urlencode(result)

@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")
Loading