GitHub

API reference for Url.

A URL, as new URL(text, base) parses it; raises ValueError when text is not a URL. str(url) is its href, so f"{url}/items" builds a URL text. Two URLs are equal when their href is, as Effect's Equal compares URLs.

http.url has Effect's functions over it (set_hash, mutate, ...).

Attributes

AttributeTypeDescription
recordUrlRecord
hrefstr
protocolstr

The scheme and : ("https:").

usernamestr
passwordstr
hoststr

The host and the port, if any ("example.com:8080").

hostnamestr
portstr

The port, "" when it is absent or the scheme's default.

pathnamestr
searchstr

? and the query, "" when it is absent or empty.

hashstr

# and the fragment, "" when it is absent or empty.

originstr
search_paramstuple[tuple[str, str], ...]

The query's name-value pairs, as url.searchParams reads them (http.url.url_params gives them as UrlParams).

Functions

__init__

__init__(text, base=None) -> None
ParameterTypeDescription
textstr
basestr | Url | None = None
def __init__(self, text: str, base: str | Url | None = None) -> None:
    record = _parse(text, base)
    if record is None:
        suffix = "" if base is None else f" with base {str(base)!r}"
        raise ValueError(f"Invalid URL: {text!r}{suffix}")
    object.__setattr__(self, "record", record)
    object.__setattr__(self, "_href", record.href())

from_record

from_record(record) -> Self
ParameterTypeDescription
recordUrlRecord

Returns Self

@classmethod
def from_record(cls, record: UrlRecord) -> Self:
    url = cls.__new__(cls)
    object.__setattr__(url, "record", record)
    object.__setattr__(url, "_href", record.href())
    return url

parse

parse(text, base=None) -> Url | None

URL.parse: the URL, or None when text is not one.

ParameterTypeDescription
textstr
basestr | Url | None = None

Returns Url | None

@classmethod
def parse(cls, text: str, base: str | Url | None = None) -> Url | None:
    """``URL.parse``: the URL, or ``None`` when ``text`` is not one."""
    record = _parse(text, base)
    return None if record is None else cls.from_record(record)

can_parse

can_parse(text, base=None) -> bool

URL.canParse.

ParameterTypeDescription
textstr
basestr | Url | None = None

Returns bool

@staticmethod
def can_parse(text: str, base: str | Url | None = None) -> bool:
    """``URL.canParse``."""
    return _parse(text, base) is not None

to_json

to_json() -> str

URL.toJSON: the href.

Returns str

def to_json(self) -> str:
    """``URL.toJSON``: the ``href``."""
    return self._href