Concatenate url and arguments regardless of whether url has existing query parameters. ``args`` may be either a dictionary or a list of key-value pairs (the latter allows for multiple values with the same key. >>> url_concat("http://example.com/foo", dict(c="d")) 'http://exampl
(
url: str,
args: Union[
None, Dict[str, str], List[Tuple[str, str]], Tuple[Tuple[str, str], ...]
],
)
| 607 | |
| 608 | |
| 609 | def url_concat( |
| 610 | url: str, |
| 611 | args: Union[ |
| 612 | None, Dict[str, str], List[Tuple[str, str]], Tuple[Tuple[str, str], ...] |
| 613 | ], |
| 614 | ) -> str: |
| 615 | """Concatenate url and arguments regardless of whether |
| 616 | url has existing query parameters. |
| 617 | |
| 618 | ``args`` may be either a dictionary or a list of key-value pairs |
| 619 | (the latter allows for multiple values with the same key. |
| 620 | |
| 621 | >>> url_concat("http://example.com/foo", dict(c="d")) |
| 622 | 'http://example.com/foo?c=d' |
| 623 | >>> url_concat("http://example.com/foo?a=b", dict(c="d")) |
| 624 | 'http://example.com/foo?a=b&c=d' |
| 625 | >>> url_concat("http://example.com/foo?a=b", [("c", "d"), ("c", "d2")]) |
| 626 | 'http://example.com/foo?a=b&c=d&c=d2' |
| 627 | """ |
| 628 | if args is None: |
| 629 | return url |
| 630 | parsed_url = urlparse(url) |
| 631 | if isinstance(args, dict): |
| 632 | parsed_query = parse_qsl(parsed_url.query, keep_blank_values=True) |
| 633 | parsed_query.extend(args.items()) |
| 634 | elif isinstance(args, list) or isinstance(args, tuple): |
| 635 | parsed_query = parse_qsl(parsed_url.query, keep_blank_values=True) |
| 636 | parsed_query.extend(args) |
| 637 | else: |
| 638 | err = "'args' parameter should be dict, list or tuple. Not {0}".format( |
| 639 | type(args) |
| 640 | ) |
| 641 | raise TypeError(err) |
| 642 | final_query = urlencode(parsed_query) |
| 643 | url = urlunparse( |
| 644 | ( |
| 645 | parsed_url[0], |
| 646 | parsed_url[1], |
| 647 | parsed_url[2], |
| 648 | parsed_url[3], |
| 649 | final_query, |
| 650 | parsed_url[5], |
| 651 | ) |
| 652 | ) |
| 653 | return url |
| 654 | |
| 655 | |
| 656 | class HTTPFile(ObjectDict): |