URI Template (std-uritemplate)

2.0.8 · active · verified Thu Apr 09

std-uritemplate is a Python library providing a robust and compliant implementation of RFC 6570, the URI Template specification. It allows users to expand URI templates with provided variables, generating complete URIs. The library is actively maintained, with frequent patch releases within major versions, and is currently at version 2.0.8.

Warnings

Install

Imports

Quickstart

Initialize a URITemplate object with your template string. Use the `expand` method to fill in variables and generate the final URI. `partial_expand` can be used to expand a subset of variables, returning a new `URITemplate` object with the remaining unexpanded variables.

from uritemplate import URITemplate

template_str = "https://example.com/api/{resource}{?id,name}"
tpl = URITemplate(template_str)

# Expand with simple variables
expanded_uri = tpl.expand(resource="users", id=123, name="Alice")
print(f"Expanded URI: {expanded_uri}")

# Partial expansion
partially_expanded_tpl = tpl.partial_expand(resource="posts")
print(f"Partially Expanded URI (with resource): {partially_expanded_tpl.template}")

# Final expansion of partially expanded template
final_uri = partially_expanded_tpl.expand(id=456)
print(f"Final URI: {final_uri}")

view raw JSON →