Commit e1e01ff 1 parent a8f1b58 commit e1e01ff Copy full SHA for e1e01ff
File tree 4 files changed +41
-19
lines changed
4 files changed +41
-19
lines changed Original file line number Diff line number Diff line change @@ -457,17 +457,9 @@ def route_v8(
457
457
if alternatives :
458
458
data ["alternatives" ] = alternatives
459
459
if avoid :
460
- key = list (avoid .keys ())[0 ]
461
- values = list (avoid .values ())[0 ]
462
- data ["avoid" ] = {
463
- key : "," .join (values ),
464
- }
460
+ data ["avoid" ] = {key : "," .join (vals ) for key , vals in avoid .items ()}
465
461
if exclude :
466
- key = list (exclude .keys ())[0 ]
467
- values = list (exclude .values ())[0 ]
468
- data ["exclude" ] = {
469
- key : "," .join (values ),
470
- }
462
+ data ["exclude" ] = {key : "," .join (vals ) for key , vals in exclude .items ()}
471
463
if units :
472
464
data ["units" ] = units .__str__ ()
473
465
if lang :
@@ -477,11 +469,7 @@ def route_v8(
477
469
if span_fields :
478
470
data ["spans" ] = "," .join ([field .__str__ () for field in span_fields ])
479
471
if truck :
480
- key = list (truck .keys ())[0 ]
481
- values = list (truck .values ())[0 ]
482
- data ["truck" ] = {
483
- key : "," .join (values ),
484
- }
472
+ data ["truck" ] = {key : "," .join (vals ) for key , vals in truck .items ()}
485
473
if scooter :
486
474
data ["scooter" ] = scooter
487
475
Original file line number Diff line number Diff line change @@ -18,16 +18,23 @@ def encode_parameters(parameters):
18
18
parameters (dict):
19
19
dictionary of query parameters to be converted.
20
20
Returns:
21
- A URL-encoded string in "key=value&key=value" form
21
+ A URL-encoded string in "key=value&key=value" form. If the parameter value is
22
+ a dict, the encoded string is converted to "main_key[sub_key]=sub_value".
22
23
"""
23
24
if parameters is None :
24
25
return None
25
26
if not isinstance (parameters , dict ):
26
27
raise HEREError ("`parameters` must be a dict." )
27
28
else :
28
- return urlencode (
29
- dict ((k , v ) for k , v in parameters .items () if v is not None )
30
- )
29
+ parameters = dict ((k , v ) for k , v in parameters .items () if v is not None )
30
+ result = dict ()
31
+ for key , value in parameters .items ():
32
+ if isinstance (value , dict ):
33
+ for sub_key , sub_value in value .items ():
34
+ result [f"{ key } [{ sub_key } ]" ] = sub_value
35
+ else :
36
+ result [key ] = value
37
+ return urlencode (result )
31
38
32
39
@staticmethod
33
40
def build_url (url , extra_params = None ):
Original file line number Diff line number Diff line change @@ -1215,3 +1215,22 @@ def test_route_v8_multiple_via_points(self):
1215
1215
)
1216
1216
self .assertTrue (response )
1217
1217
self .assertIsInstance (response , herepy .RoutingResponseV8 )
1218
+
1219
+ @responses .activate
1220
+ def test_route_v8_url_parameters_multiple (self ):
1221
+ responses .add (
1222
+ responses .GET ,
1223
+ "https://router.hereapi.com/v8/routes" ,
1224
+ "{}" ,
1225
+ status = 200 ,
1226
+ match = [
1227
+ responses .matchers .query_param_matcher (
1228
+ {"truck[height]" : "15000" , "truck[width]" : "3000" }, strict_match = False
1229
+ )
1230
+ ],
1231
+ )
1232
+ self ._api .route_v8 (transport_mode = herepy .RoutingTransportMode .truck ,
1233
+ origin = [41.9798 , - 87.8801 ],
1234
+ destination = [41.9043 , - 87.9216 ],
1235
+ truck = {"height" : ["15000" ], "width" : ["3000" ]}
1236
+ )
Original file line number Diff line number Diff line change @@ -29,3 +29,11 @@ def test_buildurl(self):
29
29
"https://geocoder.cit.api.here.com/6.2/geocode.json" , data
30
30
)
31
31
self .assertTrue (url )
32
+
33
+ def test_build_url_sub_data (self ):
34
+ data = {
35
+ "key1" : "val" ,
36
+ "key2" : {"sub_key" : "sub_val" , "sub_key2" : "sub_val2" },
37
+ }
38
+ url = Utils .build_url ("https://router.hereapi.com/v8/routes" , data )
39
+ self .assertEqual (url , "https://router.hereapi.com/v8/routes?key1=val&key2%5Bsub_key%5D=sub_val&key2%5Bsub_key2%5D=sub_val2" )
You can’t perform that action at this time.
0 commit comments