* chore: handling base path and urls * chore: uniformize urls in common settings * correct live url * chore: use url join to correctly join urls --------- Co-authored-by: sriram veeraghanta <veeraghanta.sriram@gmail.com>
54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
# Python imports
|
|
from typing import Optional
|
|
from urllib.parse import urlparse
|
|
|
|
|
|
def is_valid_url(url: str) -> bool:
|
|
"""
|
|
Validates whether the given string is a well-formed URL.
|
|
|
|
Args:
|
|
url (str): The URL string to validate.
|
|
|
|
Returns:
|
|
bool: True if the URL is valid, False otherwise.
|
|
|
|
Example:
|
|
>>> is_valid_url("https://example.com")
|
|
True
|
|
>>> is_valid_url("not a url")
|
|
False
|
|
"""
|
|
try:
|
|
result = urlparse(url)
|
|
# A valid URL should have at least scheme and netloc
|
|
return all([result.scheme, result.netloc])
|
|
except TypeError:
|
|
return False
|
|
|
|
|
|
def get_url_components(url: str) -> Optional[dict]:
|
|
"""
|
|
Parses the URL and returns its components if valid.
|
|
|
|
Args:
|
|
url (str): The URL string to parse.
|
|
|
|
Returns:
|
|
Optional[dict]: A dictionary with URL components if valid, None otherwise.
|
|
|
|
Example:
|
|
>>> get_url_components("https://example.com/path?query=1")
|
|
{'scheme': 'https', 'netloc': 'example.com', 'path': '/path', 'params': '', 'query': 'query=1', 'fragment': ''}
|
|
"""
|
|
if not is_valid_url(url):
|
|
return None
|
|
result = urlparse(url)
|
|
return {
|
|
"scheme": result.scheme,
|
|
"netloc": result.netloc,
|
|
"path": result.path,
|
|
"params": result.params,
|
|
"query": result.query,
|
|
"fragment": result.fragment,
|
|
}
|