konnect.curl.url module
Easy URL manipulation
The Url class subclasses str but adds a new operator for joining URL parts inspired by
Path, and some URL manipulation methods:
With the division operator ‘/’ simple concatenation is performed, joining the two parts with a slash between them:
>>> Url("http://example.com/api/menu") / "spam"
'http://example.com/api/menu/spam'
>>> Url("http://example.com/api/menu/") / "spam"
'http://example.com/api/menu/spam'
Alternatively the Url.urljoin() method does a browser-style URL join, where the last
slash-separated component is replaced with the joined string; unless the string starts with
a slash in which case it replaces the entire path component; or with a schema in which case
it replaces the entire URL:
>>> Url("http://example.com/api/menu").urljoin("spam")
'http://example.com/api/spam'
>>> Url("http://example.com/api/menu/").urljoin("spam")
'http://example.com/api/menu/spam'
>>> Url("http://example.com/api/menu/").urljoin("/spam")
'http://example.com/spam'
>>> Url("http://example.com/api/menu/").urljoin("https://example.org/spam")
'https://example.org/spam'
The URL can be implicitly parsed with urlparse and the fields of the result
accessed through attributes of the Url class:
>>> Url("http://example.com/api/menu").scheme
'http'
>>> Url("http://example.com/api/menu").hostname
'example.com'
The parsed fields can be replaced to create a new URL with Url.urlreplace():
>>> url = Url("http://example.com/api/menu?choice=spam&number=2")
>>> url.urlreplace(hostname="example.org", query={"choice": "ham", "number": 1})
'http://example.org/api/menu?choice=ham&number=1'
Query parameters can be added or removed with Url.queryadd() and Url.queryremove():
>>> Url("http://example.com?food=spam").queryadd(place="Camelot")
'http://example.com?food=spam&place=Camelot'
>>> Url("http://example.com?food=spam").queryadd({"food": ["ham", "eggs"]})
'http://example.com?food=spam&food=ham&food=eggs'
>>> Url("http://example.com?food=spam").queryadd("food=eggs&place=Camelot")
'http://example.com?food=spam&food=eggs&place=Camelot'
>>> Url("http://example.com?food=spam&place=Camelot").queryremove("place")
'http://example.com?food=spam'
- type QuerySrc = Sequence[tuple[str, ParamValue]] | Mapping[str, ParamValue] | str
- class Url(_src: str, /)[source]
Bases:
strURL strings that can be examined and manipulated
- classmethod from_parts(scheme: str, hostname: str, port: int | str = '', path: str = '', query: QuerySrc = '', fragment: str = '') Self[source]
Create a new URL from component parts
Note
There is no ‘netloc’, ‘username’ or ‘password’ arguments. Credentials should not be encoded in URLs.
- urljoin(other: str, /) Url[source]
Join this URL as a base with a possibly relative URL to form an absolute URL
- urlreplace(scheme: str = '', hostname: str = '', port: int | str = 0, path: str = '', query: QuerySrc = '', fragment: str = '') Url[source]
Replace parts of the URL and return the new URL
Note
Username and password values cannot be replaced, and will be stripped from the new URL. These should not be encoded in URLs.
- queryadd(src: QuerySrc = (), **extra: ParamValue) Url[source]
Add query parameters to the URL and return the new URL