Coverage for jutil/urls.py: 88%

# Coverage for jutil/urls.py : 88%

## 13 statements

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 import logging

2 from urllib.parse import urlparse, parse_qsl, urlunparse, urlencode

3

4 logger = logging.getLogger(__name__)

5

6

7 def modify_url(url: str, new_params: dict) -> str:

8 """

9 Modifies existing URL by setting/overriding specified query string parameters.

10 This can be useful for example if you need to modify user-provided callback URL

11 for extra arguments. Note: Does not support multiple querystring parameters with identical name.

12 :param url: Base URL/path to modify

13 :param new_params: Querystring parameters to set/override (dict)

14 :return: New URL/path

15 """

16 res = urlparse(url)

17 query_params = dict(parse_qsl(res.query))

18 for k, v in new_params.items():

19 if v is None: 19 ↛ 20line 19 didn't jump to line 20, because the condition on line 19 was never true

20 query_params[str(k)] = ''

21 else:

22 query_params[str(k)] = str(v)

23 parts = list(res)

24 parts[4] = urlencode(query_params)

25 return urlunparse(parts)