def_convert_url_to_string(url:Union[httpcore.URL,str,bytes])->str:"""Convert httpcore.URL, str, or bytes to a string URL. Migrated from hishel._utils.normalized_url which was removed in 1.0. """ifisinstance(url,httpcore.URL):# Convert httpcore.URL to string: scheme://host[:port]/path[?query][#fragment]url_str=f"{url.scheme.decode('ascii')}://{url.host.decode('ascii')}"ifurl.portisnotNone:url_str+=f":{url.port}"ifurl.target:url_str+=url.target.decode("ascii")returnurl_strelifisinstance(url,bytes):returnurl.decode("ascii")returnurl
[docs]defnormalize_url(url:Union[httpcore.URL,str,bytes])->str:url=_convert_url_to_string(url)parsed_url=urlparse(url)ifnotparsed_url.schemeornotparsed_url.netloc:raiseValueError("Invalid URL: Missing scheme or netloc.")returnurlunparse(parsed_url._replace(scheme=parsed_url.scheme.lower(),netloc=parsed_url.netloc.lower(),path=parsed_url.pathor"/",))