WSRequest: (re-)introduce queryargs optional parameter to ease adding query params

This commit is contained in:
Laurent Monin
2023-06-04 18:10:22 +02:00
parent 76139d946d
commit d4e4641837
2 changed files with 20 additions and 0 deletions

View File

@@ -115,6 +115,7 @@ class WSRequest(QNetworkRequest):
important=False,
request_mimetype=None,
url=None,
queryargs=None,
):
"""
Args:
@@ -134,6 +135,7 @@ class WSRequest(QNetworkRequest):
important: Indicates that this is an important request.
request_mimetype: Set the Content-Type header.
url: URL passed as a string or as a QUrl to use for this request
queryargs: dictionary of keys and values to add to the url
"""
# mandatory parameters
self.method = method
@@ -149,6 +151,14 @@ class WSRequest(QNetworkRequest):
if not isinstance(url, QUrl):
url = QUrl(url)
if queryargs is not None:
query = QtCore.QUrlQuery(url)
for k, v in queryargs.items():
# FIXME: check if encoding is correct
query.addQueryItem(k, str(v))
url.setQuery(query)
super().__init__(url)
# optional parameters

View File

@@ -521,3 +521,13 @@ class WSRequestTest(PicardTestCase):
self.assertTrue(TEMP_ERRORS_RETRIES > 1)
self.assertEqual(request.mark_for_retry(), 1)
self.assertFalse(request.max_retries_reached())
def test_queryargs(self):
request = WSRequest(
url='http://example.org/path?a=1',
method='GET',
handler=dummy_handler,
queryargs={'a': 2, 'b': 'x%20x', 'c': '1+2', 'd': '&', 'e': '?'},
)
# FIXME: check encoding
self.assertEqual(request.url().toString(), 'http://example.org/path?a=1&a=2&b=x x&c=1+2&d=%26&e=?')