Skip to content

Commit

Permalink
Fix PublicationService.get() (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
mickael-menu authored Nov 27, 2024
1 parent f8785fb commit 9117336
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 15 deletions.
7 changes: 5 additions & 2 deletions Sources/Shared/Publication/Publication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import CoreServices
import Foundation
import ReadiumInternal

/// Shared model for a Readium Publication.
public class Publication: Closeable, Loggable {
Expand Down Expand Up @@ -90,8 +91,10 @@ public class Publication: Closeable, Loggable {

/// Returns the resource targeted by the given `href`.
public func get<T: URLConvertible>(_ href: T) -> Resource? {
// Try first the original href and falls back to href without query and fragment.
container[href] ?? container[href.anyURL.removingQuery().removingFragment()]
services.first { $0.get(href) }
// Try first the original href and falls back to href without query and fragment.
?? container[href]
?? container[href.anyURL.removingQuery().removingFragment()]
}

/// Closes any opened resource associated with the `Publication`, including `services`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public final class GeneratedCoverService: CoverService {

public var links: [Link] { [coverLink] }

public func get(link: Link) -> Resource? {
guard link.href == coverLink.href else {
public func get<T>(_ href: T) -> (any Resource)? where T: URLConvertible {
guard href.anyURL.isEquivalentTo(coverLink.url()) else {
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ private let positionsLink = Link(
public extension PositionsService {
var links: [Link] { [positionsLink] }

func get(link: Link) -> Resource? {
guard link.href == positionsLink.href else {
func get<T>(_ href: T) -> (any Resource)? where T: URLConvertible {
guard href.anyURL.isEquivalentTo(positionsLink.url()) else {
return nil
}
return PositionsResource(positions: positions)
Expand Down
9 changes: 6 additions & 3 deletions Sources/Shared/Publication/Services/PublicationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ public protocol PublicationService: Closeable {
///
/// Called by `Publication.get()` for each request.
///
/// - Returns: The Resource containing the response, or null if the service doesn't recognize
/// this request.
func get(link: Link) -> Resource?
/// - Returns: The Resource containing the response, or null if the service
/// doesn't recognize this request.
func get<T: URLConvertible>(_ href: T) -> Resource?
}

public extension PublicationService {
var links: [Link] { [] }

func get<T: URLConvertible>(_ href: T) -> Resource? { nil }

@available(*, unavailable, message: "Use get(URLConvertible) instead")
func get(link: Link) -> Resource? { nil }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ContentProtectionServiceTests: XCTestCase {
func testGetUnknown() {
let service = TestContentProtectionService()

let resource = service.get(link: Link(href: "/unknown"))
let resource = service.get(AnyURL(string: "/unknown")!)

XCTAssertNil(resource)
}
Expand Down Expand Up @@ -93,11 +93,11 @@ struct TestContentProtectionService: ContentProtectionService {
var name: LocalizedString? = nil

func getCopy(text: String, peek: Bool) throws -> Resource {
try XCTUnwrap(get(link: Link(href: "~readium/rights/copy?text=\(text)&peek=\(peek)")))
try XCTUnwrap(get(AnyURL(string: "~readium/rights/copy?text=\(text)&peek=\(peek)")!))
}

func getPrint(pageCount: Int, peek: Bool) throws -> Resource {
try XCTUnwrap(get(link: Link(href: "~readium/rights/print?pageCount=\(pageCount)&peek=\(peek)")))
try XCTUnwrap(get(AnyURL(string: "~readium/rights/print?pageCount=\(pageCount)&peek=\(peek)")!))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class GeneratedCoverServiceTests: XCTestCase {
GeneratedCoverService(cover: cover),
GeneratedCoverService(makeCover: { .success(self.cover) }),
] {
let resource = try XCTUnwrap(service.get(link: Link(href: "~readium/cover")))
let resource = try XCTUnwrap(service.get(AnyURL(string: "~readium/cover")!))
let result = await resource.read().map(UIImage.init)
AssertImageEqual(result, .success(cover))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class PositionsServiceTests: XCTestCase {
func testGetPositions() async throws {
let service = TestPositionsService(positions)

let resource = service.get(link: Link(href: "~readium/positions"))
let resource = service.get(AnyURL(string: "~readium/positions")!)

let result = try await resource?.readAsString().get()
XCTAssertEqual(
Expand All @@ -137,7 +137,7 @@ class PositionsServiceTests: XCTestCase {
func testGetUnknown() {
let service = TestPositionsService(positions)

let resource = service.get(link: Link(href: "/unknown"))
let resource = service.get(AnyURL(string: "/unknown")!)

XCTAssertNil(resource)
}
Expand Down

0 comments on commit 9117336

Please sign in to comment.