Fix APICallMixin url_args handling (#6468)

This commit is contained in:
Bobbe 2024-02-11 22:01:28 +01:00 committed by GitHub
parent 824aa8138b
commit b372db8960
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 7 deletions

View File

@ -2,6 +2,7 @@
import json as json_pkg
import logging
from collections.abc import Iterable
import requests
@ -107,7 +108,9 @@ class APICallMixin:
"""Returns an encoded path for the provided dict."""
groups = []
for key, val in arguments.items():
groups.append(f'{key}={",".join([str(a) for a in val])}')
if isinstance(val, Iterable) and not isinstance(val, str):
val = ','.join([str(a) for a in val])
groups.append(f'{key}={val}')
return f'?{"&".join(groups)}'
def api_call(

View File

@ -263,14 +263,17 @@ class APICallMixinTest(BaseMixinDefinition, TestCase):
"""Test that building up args work."""
# api_build_url_args
# 1 arg
result = self.mixin.api_build_url_args({'a': 'b'})
self.assertEqual(result, '?a=b')
result = self.mixin.api_build_url_args({'a': 'abc123'})
self.assertEqual(result, '?a=abc123')
# non string arg
result = self.mixin.api_build_url_args({'a': 1})
self.assertEqual(result, '?a=1')
# more args
result = self.mixin.api_build_url_args({'a': 'b', 'c': 'd'})
self.assertEqual(result, '?a=b&c=d')
result = self.mixin.api_build_url_args({'a': 'b', 'c': 42})
self.assertEqual(result, '?a=b&c=42')
# list args
result = self.mixin.api_build_url_args({'a': 'b', 'c': ['d', 'e', 'f']})
self.assertEqual(result, '?a=b&c=d,e,f')
result = self.mixin.api_build_url_args({'a': 'b', 'c': ['d', 'efgh', 1337]})
self.assertEqual(result, '?a=b&c=d,efgh,1337')
def test_api_call(self):
"""Test that api calls work."""