MCPcopy
hub / github.com/tornadoweb/tornado / url_concat

Function url_concat

tornado/httputil.py:781–825  ·  view source on GitHub ↗

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], ...]
    ],
)

Source from the content-addressed store, hash-verified

779
780
781def url_concat(
782 url: str,
783 args: Union[
784 None, Dict[str, str], List[Tuple[str, str]], Tuple[Tuple[str, str], ...]
785 ],
786) -> str:
787 """Concatenate url and arguments regardless of whether
788 url has existing query parameters.
789
790 ``args`` may be either a dictionary or a list of key-value pairs
791 (the latter allows for multiple values with the same key.
792
793 >>> url_concat("http://example.com/foo", dict(c="d"))
794 'http://example.com/foo?c=d'
795 >>> url_concat("http://example.com/foo?a=b", dict(c="d"))
796 'http://example.com/foo?a=b&c=d'
797 >>> url_concat("http://example.com/foo?a=b", [("c", "d"), ("c", "d2")])
798 'http://example.com/foo?a=b&c=d&c=d2'
799 """
800 if args is None:
801 return url
802 parsed_url = urlparse(url)
803 if isinstance(args, dict):
804 parsed_query = parse_qsl(parsed_url.query, keep_blank_values=True)
805 parsed_query.extend(args.items())
806 elif isinstance(args, list) or isinstance(args, tuple):
807 parsed_query = parse_qsl(parsed_url.query, keep_blank_values=True)
808 parsed_query.extend(args)
809 else:
810 err = "'args' parameter should be dict, list or tuple. Not {0}".format(
811 type(args)
812 )
813 raise TypeError(err)
814 final_query = urlencode(parsed_query)
815 url = urlunparse(
816 (
817 parsed_url[0],
818 parsed_url[1],
819 parsed_url[2],
820 parsed_url[3],
821 final_query,
822 parsed_url[5],
823 )
824 )
825 return url
826
827
828class HTTPFile(ObjectDict):

Calls 2

itemsMethod · 0.80
formatMethod · 0.80