RFC 6570 URI Template Processor

1.3.0 · maintenance · verified Sat Mar 28

The `uri-template` library provides an implementation of RFC 6570 URI Templates in Python. It supports template expansion in strict adherence to the RFC, while also introducing several extensions for handling non-string values, nested structures, and partial expansions. The current version is 1.3.0, released in June 2023. It appears to be maintained with a slower release cadence by a single maintainer.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic URI template expansion using both the standalone `expand` function and the `URITemplate` class. It also shows how to perform partial template expansions.

from uri_template import URITemplate, expand

# Using the expand function for a simple, one-off expansion
uri_str = "http://example.com/api/{resource}/{id}"
expanded_uri_func = expand(uri_str, resource='users', id='123')
print(f"Function expanded: {expanded_uri_func}")

# Using the URITemplate class for repeated expansions or advanced features
template = URITemplate("http://example.com/search{?query,limit}")
expanded_uri_class = template.expand(query='python', limit=10)
print(f"Class expanded: {expanded_uri_class}")

# Example of partial expansion
partial_template = URITemplate("http://example.com/items/{category}/{item_id}")
partially_expanded = partial_template.partial(category='electronics')
print(f"Partially expanded: {partially_expanded}")
# Further expand the partially expanded template
final_expanded = partially_expanded.expand(item_id='laptop-x')
print(f"Further expanded: {final_expanded}")

view raw JSON →