Skip to content

Commit 6484c75

Browse files
authored
Fix url query generation when value is a list
1 parent 54db40e commit 6484c75

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

apypie/api.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ def _qs_param(param):
3939
return param
4040

4141

42+
def _qs_key(k, v):
43+
if isinstance(v, (list, tuple)):
44+
return f"{k}[]"
45+
return k
46+
47+
4248
class Api(object):
4349
"""
4450
Apipie API bindings
@@ -305,7 +311,7 @@ def http_call(self, http_method, path, params=None, headers=None, data=None, fil
305311

306312
if params:
307313
if http_method in ['get', 'head']:
308-
kwargs['params'] = {k: _qs_param(v) for k, v in params.items()}
314+
kwargs['params'] = {_qs_key(k, v): _qs_param(v) for k, v in params.items()}
309315
else:
310316
kwargs['json'] = params
311317
elif http_method in ['post', 'put', 'patch'] and not data and not files:

tests/test_apypie.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,16 @@ def test_http_call_get_with_params(api, requests_mock):
240240
api.http_call('get', '/', {'test': 'all the things'})
241241

242242

243+
def test_http_call_get_with_list_params(api, requests_mock):
244+
requests_mock.get('https://api.example.com/?test[]=all&test[]=the&test[]=list', text='{}')
245+
api.http_call('get', '/', {'test': ['all', 'the', 'list']})
246+
247+
248+
def test_http_call_get_with_tuple_params(api, requests_mock):
249+
requests_mock.get('https://api.example.com/?test[]=all&test[]=the&test[]=tuple', text='{}')
250+
api.http_call('get', '/', {'test': ('all', 'the', 'tuple')})
251+
252+
243253
def test_http_call_post(api, requests_mock):
244254
expected_headers = {
245255
'Accept': 'application/json;version=1',

0 commit comments

Comments
 (0)