Skip to content

pombredanne/urimagic

Repository files navigation

image

URIMagic

URIMagic is a Python library that provides full implementations of RFC 3986 URIs and RFC 6570 URI Templates.

Percent Encoding & Decoding

>>> from urimagic import percent_encode, percent_decode
>>> percent_encode("Mulder & Scully")
'Mulder%20%26%20Scully'
>>> percent_decode("Mulder%20%26%20Scully")
'Mulder & Scully'

Parsing a URI

>>> from urimagic import URI
>>> uri = URI("https://bob@example.com:8080/data/report.html?date=2000-12-25#summary")
>>> uri.scheme
'https'
>>> uri.authority
Authority('bob@example.com:8080')
>>> uri.user_info
'bob'
>>> uri.host
'example.com'
>>> uri.port
8080
>>> uri.host_port
'example.com:8080'
>>> uri.path
Path('/data/report.html')
>>> uri.query
Query('date=2000-12-25')
>>> uri.fragment
'summary'
>>> uri.hierarchical_part
'//bob@example.com:8080/data/report.html'
>>> uri.absolute_path_reference
'/data/report.html?date=2000-12-25#summary'
>>> uri.string
'https://bob@example.com:8080/data/report.html?date=2000-12-25#summary'

Authorities

>>> from urimagic import Authority
>>> auth = Authority("bob@example.com:8080")
>>> auth.user_info
'bob'
>>> auth.host
'example.com'
>>> auth.port
8080
>>> auth.host_port
'example.com:8080'
>>> auth.string
'bob@example.com:8080'

Paths

>>> from urimagic import Path
>>> Path("/foo/bar").segments
['', 'foo', 'bar']
>>> Path("/foo/bar").with_trailing_slash()
Path('/foo/bar/')
>>> Path("/foo/bar/").without_trailing_slash()
Path('/foo/bar')
>>> Path("/foo/bar").string
'/foo/bar'

Queries

>>> from urimagic import Query
>>> query = Query("cake=nice&cake=sweet&mushrooms=yuk")
>>> query.get("cake")
'nice'
>>> query.get("cake", 1)
'sweet'
>>> query.get("mushrooms")
'yuk'
>>> query.string
'cake=nice&cake=sweet&mushrooms=yuk'

Resolving new URIs

URI Templates

About

A Python library providing full implementations of RFC 3986 URIs and RFC 6570 URI Templates.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published