https://github.com/SwiftScream/URITemplate
Robust and performant Swift implementation of RFC6570 URI Template
https://github.com/SwiftScream/URITemplate
rfc-6570 swift uri-template uri-templates
Last synced: 4 months ago
JSON representation
Robust and performant Swift implementation of RFC6570 URI Template
- Host: GitHub
- URL: https://github.com/SwiftScream/URITemplate
- Owner: SwiftScream
- License: apache-2.0
- Created: 2018-05-01T12:02:24.000Z (about 7 years ago)
- Default Branch: develop
- Last Pushed: 2025-01-02T02:23:59.000Z (6 months ago)
- Last Synced: 2025-03-02T00:47:37.007Z (4 months ago)
- Topics: rfc-6570, swift, uri-template, uri-templates
- Language: Swift
- Size: 156 KB
- Stars: 13
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# ScreamURITemplate
A robust and performant Swift 6 implementation of [RFC6570](https://tools.ietf.org/html/rfc6570) URI Template. Full Level 4 support is provided.
[](https://github.com/SwiftScream/URITemplate/actions/workflows/ci.yml)
[](https://codecov.io/gh/SwiftScream/URITemplate/branch/master)[](https://swiftpackageindex.com/SwiftScream/URITemplate)
[](https://swiftpackageindex.com/SwiftScream/URITemplate)[](https://raw.githubusercontent.com/SwiftScream/URITemplate/master/LICENSE) [](https://github.com/SwiftScream/URITemplate/releases/latest)
## 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
```swift
import ScreamURITemplatelet 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.```swift
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
```swift
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.```swift
struct HALObject : Codable {
let _links : [String:URITemplate]
}
```## Tests
The library is tested against the [standard test suite](https://github.com/uri-templates/uritemplate-test), as well as some additional tests for behavior specific to this implementation. It is intended to keep test coverage as high as possible.