A robust and performant Swift 4.1 implementation of RFC6570 URI Template. Full Level 4 support is provided.
Static Library and Dynamic Framework targets are provided for iOS, MacOS, tvOS, and watchOS; integrate them as you normally would.
Add pod 'ScreamURITemplate', '~> 1.1'
to your Podfile
Add github "SwiftScream/URITemplate" ~> 1.1
to your Cartfile
Add .package(url: "https://github.com/SwiftScream/URITemplate.git", from: "1.1.0")
to your package.swift dependencies
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
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 URITemplate.Error.malformedTemplate(let position, let reason) {
// reason = "Empty Variable Name"
// position = 29th character
}
let template = try URITemplate(string:"https://api.github.com/repos/{owner}/{repository}/traffic/views")
let variableNames = template.variableNames
// ["owner", "repository"]
URITemplate
implements the Codable
protocol, enabling easy serialization to or from JSON objects.
struct HALObject : Codable {
let _links : [String:URITemplate]
}
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.