diff --git a/readium-lcp-swift/License/Model/Components/Link.swift b/readium-lcp-swift/License/Model/Components/Link.swift index 4357da5..7a077b4 100644 --- a/readium-lcp-swift/License/Model/Components/Link.swift +++ b/readium-lcp-swift/License/Model/Components/Link.swift @@ -56,23 +56,14 @@ public struct Link { /// Gets the valid URL if possible, applying the given template context as query parameters if the link is templated. /// eg. http://url{?id,name} + [id: x, name: y] -> http://url?id=x&name=y - func url(with parameters: [String: CustomStringConvertible]) -> URL? { - if !templated { - return URL(string: href) - } - - let urlString = href.replacingOccurrences(of: "\\{\\?.+?\\}", with: "", options: [.regularExpression]) - - guard var urlBuilder = URLComponents(string: urlString) else { - return nil - } + func url(with parameters: [String: LosslessStringConvertible]) -> URL? { + var href = self.href - // Add the template context as query parameters - urlBuilder.queryItems = parameters.map { param in - URLQueryItem(name: param.key, value: param.value.description) + if templated { + href = URITemplate(href).expand(with: parameters.mapValues { String(describing: $0) }) } - return urlBuilder.url + return URL(string: href) } /// Expands the href without any template context. diff --git a/readium-lcp-swift/License/Model/LicenseDocument.swift b/readium-lcp-swift/License/Model/LicenseDocument.swift index 946c270..d8dbcf0 100644 --- a/readium-lcp-swift/License/Model/LicenseDocument.swift +++ b/readium-lcp-swift/License/Model/LicenseDocument.swift @@ -101,7 +101,7 @@ public struct LicenseDocument { /// Gets and expands the URL for the given rel, if it exits. /// - Throws: `LCPError.invalidLink` if the URL can't be built. - func url(for rel: Rel, with parameters: [String: CustomStringConvertible] = [:]) throws -> URL { + func url(for rel: Rel, with parameters: [String: LosslessStringConvertible] = [:]) throws -> URL { guard let url = link(for: rel)?.url(with: parameters) else { throw ParsingError.url(rel: rel.rawValue) } diff --git a/readium-lcp-swift/License/Model/StatusDocument.swift b/readium-lcp-swift/License/Model/StatusDocument.swift index 11b392e..ec4c401 100644 --- a/readium-lcp-swift/License/Model/StatusDocument.swift +++ b/readium-lcp-swift/License/Model/StatusDocument.swift @@ -101,7 +101,7 @@ public struct StatusDocument { /// Gets and expands the URL for the given rel, if it exits. /// - Throws: `LCPError.invalidLink` if the URL can't be built. - func url(for rel: Rel, with parameters: [String: CustomStringConvertible] = [:]) throws -> URL { + func url(for rel: Rel, with parameters: [String: LosslessStringConvertible] = [:]) throws -> URL { guard let url = link(for: rel)?.url(with: parameters) else { throw ParsingError.url(rel: rel.rawValue) }