Skip to content

Robust and performant Swift implementation of RFC6570 URI Template

License

Notifications You must be signed in to change notification settings

SwiftScream/URITemplate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

0fc6fb3 · Jan 2, 2025
Dec 2, 2024
Jan 1, 2025
Jan 1, 2025
May 18, 2018
Dec 3, 2024
Jan 2, 2023
Dec 3, 2024
Dec 30, 2024
Jun 2, 2024
Dec 1, 2024
Jan 2, 2025
Apr 26, 2018
Nov 29, 2024
Jan 2, 2025
Dec 2, 2024

Repository files navigation

ScreamURITemplate

A robust and performant Swift 6 implementation of RFC6570 URI Template. Full Level 4 support is provided.

CI Codecov branch

license GitHub release

Getting Started

Swift Package Manager

Add .package(url: "https://github.com/SwiftScream/URITemplate.git", from: "5.0.0") to your Package.swift dependencies

Usage

Template Processing

import ScreamURITemplate

let template = try URITemplate(string:"https://api.github.com/repos/{owner}/{repository}/traffic/views")
let variables = ["owner":"SwiftScream", "repository":"URITemplate"]
let urlString = try template.process(variables)
// https://api.github.com/repos/SwiftScream/URITemplate/traffic/views

When Things Go Wrong

Both template initialization and processing can fail; throwing a URITemplate.Error The error cases contain associated values specifying a string reason for the error and the index into the template string that the error occurred.

do {
    _ = try URITemplate(string: "https://api.github.com/repos/{}/{repository}")
} catch {
    // error.reason = "Empty Variable Name"
    // error.position = 29th character
}

Get variable names used in a template

let template = try URITemplate(string:"https://api.github.com/repos/{owner}/{repository}/traffic/views")
let variableNames = template.variableNames
// ["owner", "repository"]

Codable Support

URITemplate implements the Codable protocol, enabling easy serialization to or from JSON objects.

struct HALObject : Codable {
    let _links : [String:URITemplate]
}

Tests

The library is tested against the standard test suite, as well as some additional tests for behavior specific to this implementation. It is intended to keep test coverage as high as possible.