in src/dubbo/url.py [0:0]
def create_url(url: str, encoded: bool = False) -> "URL":
"""
Creates a URL object from a URL string.
This function takes a URL string and converts it into a URL object.
If the 'encoded' parameter is set to True, the URL string will be decoded before being converted.
:param url: The URL string to be converted into a URL object.
:type url: str
:param encoded: Determines if the URL string should be decoded before being converted. Defaults to False.
:type encoded: bool
:return: A URL object.
:rtype: URL
:raises ValueError: If the URL format is invalid.
"""
# If the URL is encoded, decode it
if encoded:
url = parse.unquote(url)
if common_constants.PROTOCOL_SEPARATOR not in url:
raise ValueError("Invalid URL format: missing protocol")
parsed_url = parse.urlparse(url)
if not parsed_url.scheme:
raise ValueError("Invalid URL format: missing scheme.")
return URL(
parsed_url.scheme,
parsed_url.hostname or "",
parsed_url.port,
parsed_url.username or "",
parsed_url.password or "",
parsed_url.path.lstrip("/"),
{k: v[0] for k, v in parse.parse_qs(parsed_url.query).items()},
)