Skip to content

Commit

Permalink
Revert to previous API signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
mickael-menu committed Oct 12, 2023
1 parent 2e2c995 commit c2778b1
Show file tree
Hide file tree
Showing 26 changed files with 292 additions and 337 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,14 @@ public data class Link(
json: JSONObject,
mediaTypeRetriever: MediaTypeRetriever = MediaTypeRetriever()
): Link {
val hrefString = json.optNullableString("href")
?: throw LcpException.Parsing.Link

val href =
if (json.optBoolean("templated", false)) {
Href.fromTemplate(hrefString)
} else {
Url(hrefString)?.let { Href(it) }
val href = json.optNullableString("href")
?.let {
Href(
href = it,
templated = json.optBoolean("templated", false)
)
}
?: throw LcpException.Parsing.Link
?: throw LcpException.Parsing.Link

return Link(
href = href,
Expand Down
64 changes: 30 additions & 34 deletions readium/opds/src/test/java/org/readium/r2/opds/OPDS1ParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ class OPDS1ParserTest {
),
links = mutableListOf(
Link(
href = Href(Url("https://example.com/opds-catalogs/root.xml")!!),
href = Href("https://example.com/opds-catalogs/root.xml")!!,
mediaType = MediaType(
"application/atom+xml;profile=opds-catalog;kind=navigation"
)!!,
rels = setOf("self"),
properties = Properties()
),
Link(
href = Href(Url("https://example.com/opds-catalogs/root.xml")!!),
href = Href("https://example.com/opds-catalogs/root.xml")!!,
mediaType = MediaType(
"application/atom+xml;profile=opds-catalog;kind=navigation"
)!!,
Expand All @@ -51,23 +51,23 @@ class OPDS1ParserTest {
),
navigation = mutableListOf(
Link(
href = Href(Url("https://example.com/opds-catalogs/popular.xml")!!),
href = Href("https://example.com/opds-catalogs/popular.xml")!!,
mediaType = MediaType(
"application/atom+xml;profile=opds-catalog;kind=acquisition"
)!!,
title = "Popular Publications",
rels = setOf("http://opds-spec.org/sort/popular")
),
Link(
href = Href(Url("https://example.com/opds-catalogs/new.xml")!!),
href = Href("https://example.com/opds-catalogs/new.xml")!!,
mediaType = MediaType(
"application/atom+xml;profile=opds-catalog;kind=acquisition"
)!!,
title = "New Publications",
rels = setOf("http://opds-spec.org/sort/new")
),
Link(
href = Href(Url("https://example.com/opds-catalogs/unpopular.xml")!!),
href = Href("https://example.com/opds-catalogs/unpopular.xml")!!,
mediaType = MediaType(
"application/atom+xml;profile=opds-catalog;kind=acquisition"
)!!,
Expand Down Expand Up @@ -102,28 +102,28 @@ class OPDS1ParserTest {
assertEquals(
mutableListOf(
Link(
href = Href(Url("https://example.com/opds-catalogs/vampire.farming.xml")!!),
href = Href("https://example.com/opds-catalogs/vampire.farming.xml")!!,
mediaType = MediaType(
"application/atom+xml;profile=opds-catalog;kind=acquisition"
)!!,
rels = setOf("related")
),
Link(
href = Href(Url("https://example.com/opds-catalogs/unpopular.xml")!!),
href = Href("https://example.com/opds-catalogs/unpopular.xml")!!,
mediaType = MediaType(
"application/atom+xml;profile=opds-catalog;kind=acquisition"
)!!,
rels = setOf("self")
),
Link(
href = Href(Url("https://example.com/opds-catalogs/root.xml")!!),
href = Href("https://example.com/opds-catalogs/root.xml")!!,
mediaType = MediaType(
"application/atom+xml;profile=opds-catalog;kind=navigation"
)!!,
rels = setOf("start")
),
Link(
href = Href(Url("https://example.com/opds-catalogs/root.xml")!!),
href = Href("https://example.com/opds-catalogs/root.xml")!!,
mediaType = MediaType(
"application/atom+xml;profile=opds-catalog;kind=navigation"
)!!,
Expand All @@ -139,12 +139,12 @@ class OPDS1ParserTest {
metadata = OpdsMetadata(title = "Categories"),
links = mutableListOf(
Link(
href = Href(Url("https://example.com/sci-fi")!!),
href = Href("https://example.com/sci-fi")!!,
title = "Science-Fiction",
rels = setOf("http://opds-spec.org/facet")
),
Link(
href = Href(Url("https://example.com/romance")!!),
href = Href("https://example.com/romance")!!,
title = "Romance",
rels = setOf("http://opds-spec.org/facet"),
properties = Properties(mapOf("numberOfItems" to 600))
Expand Down Expand Up @@ -175,37 +175,35 @@ class OPDS1ParserTest {
Contributor(
localizedName = LocalizedString("Bob the Recursive"),
links = listOf(
Link(href = Href(Url("http://opds-spec.org/authors/1285")!!))
Link(href = Href("http://opds-spec.org/authors/1285")!!)
)
)
),
description = "The story of the son of the Bob and the gallant part he played in the lives of a man and a woman."
),
links = listOf(
Link(
href = Href(Url("https://example.com/covers/4561.thmb.gif")!!),
href = Href("https://example.com/covers/4561.thmb.gif")!!,
mediaType = MediaType("image/gif")!!,
rels = setOf("http://opds-spec.org/image/thumbnail")
),
Link(
href = Href(
Url(
"https://example.com/opds-catalogs/entries/4571.complete.xml"
)!!
),
"https://example.com/opds-catalogs/entries/4571.complete.xml"
)!!,
mediaType = MediaType(
"application/atom+xml;type=entry;profile=opds-catalog"
)!!,
title = "Complete Catalog Entry for Bob, Son of Bob",
rels = setOf("alternate")
),
Link(
href = Href(Url("https://example.com/content/free/4561.epub")!!),
href = Href("https://example.com/content/free/4561.epub")!!,
mediaType = MediaType("application/epub+zip")!!,
rels = setOf("http://opds-spec.org/acquisition")
),
Link(
href = Href(Url("https://example.com/content/free/4561.mobi")!!),
href = Href("https://example.com/content/free/4561.mobi")!!,
mediaType = MediaType("application/x-mobipocket-ebook")!!,
rels = setOf("http://opds-spec.org/acquisition")
)
Expand All @@ -215,7 +213,7 @@ class OPDS1ParserTest {
PublicationCollection(
links = listOf(
Link(
href = Href(Url("https://example.com/covers/4561.lrg.png")!!),
href = Href("https://example.com/covers/4561.lrg.png")!!,
mediaType = MediaType("image/png")!!,
rels = setOf("http://opds-spec.org/image")
)
Expand All @@ -238,19 +236,19 @@ class OPDS1ParserTest {
Contributor(
localizedName = LocalizedString("Stampy McGee"),
links = listOf(
Link(href = Href(Url("http://opds-spec.org/authors/21285")!!))
Link(href = Href("http://opds-spec.org/authors/21285")!!)
)
),
Contributor(
localizedName = LocalizedString("Alice McGee"),
links = listOf(
Link(href = Href(Url("http://opds-spec.org/authors/21284")!!))
Link(href = Href("http://opds-spec.org/authors/21284")!!)
)
),
Contributor(
localizedName = LocalizedString("Harold McGee"),
links = listOf(
Link(href = Href(Url("http://opds-spec.org/authors/21283")!!))
Link(href = Href("http://opds-spec.org/authors/21283")!!)
)
)
),
Expand All @@ -261,7 +259,7 @@ class OPDS1ParserTest {
),
links = listOf(
Link(
href = Href(Url("https://example.com/content/buy/11241.epub")!!),
href = Href("https://example.com/content/buy/11241.epub")!!,
mediaType = MediaType("application/epub+zip")!!,
rels = setOf("http://opds-spec.org/acquisition/buy"),
properties = Properties(
Expand All @@ -274,7 +272,7 @@ class OPDS1ParserTest {
PublicationCollection(
links = listOf(
Link(
href = Href(Url("https://example.com/covers/11241.lrg.jpg")!!),
href = Href("https://example.com/covers/11241.lrg.jpg")!!,
mediaType = MediaType("image/jpeg")!!,
rels = setOf("http://opds-spec.org/image")
)
Expand Down Expand Up @@ -311,36 +309,34 @@ class OPDS1ParserTest {
Contributor(
localizedName = LocalizedString("Bob the Recursive"),
links = listOf(
Link(href = Href(Url("http://opds-spec.org/authors/1285")!!))
Link(href = Href("http://opds-spec.org/authors/1285")!!)
)
)
),
description = "The story of the son of the Bob and the gallant part he played in the lives of a man and a woman. Bob begins his humble life under the wandering eye of his senile mother, but quickly learns how to escape into the wilder world. Follow Bob as he uncovers his father's past and uses those lessons to improve the lives of others."
),
links = listOf(
Link(
href = Href(Url("https://example.com/covers/4561.thmb.gif")!!),
href = Href("https://example.com/covers/4561.thmb.gif")!!,
mediaType = MediaType("image/gif")!!,
rels = setOf("http://opds-spec.org/image/thumbnail")
),
Link(
href = Href(
Url(
"https://example.com/opds-catalogs/entries/4571.complete.xml"
)!!
),
"https://example.com/opds-catalogs/entries/4571.complete.xml"
)!!,
mediaType = MediaType(
"application/atom+xml;type=entry;profile=opds-catalog"
)!!,
rels = setOf("self")
),
Link(
href = Href(Url("https://example.com/content/free/4561.epub")!!),
href = Href("https://example.com/content/free/4561.epub")!!,
mediaType = MediaType("application/epub+zip")!!,
rels = setOf("http://opds-spec.org/acquisition")
),
Link(
href = Href(Url("https://example.com/content/free/4561.mobi")!!),
href = Href("https://example.com/content/free/4561.mobi")!!,
mediaType = MediaType("application/x-mobipocket-ebook")!!,
rels = setOf("http://opds-spec.org/acquisition")
)
Expand All @@ -350,7 +346,7 @@ class OPDS1ParserTest {
PublicationCollection(
links = listOf(
Link(
href = Href(Url("https://example.com/covers/4561.lrg.png")!!),
href = Href("https://example.com/covers/4561.lrg.png")!!,
mediaType = MediaType("image/png")!!,
rels = setOf("http://opds-spec.org/image")
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,23 @@ import timber.log.Timber
@Parcelize
public class Href private constructor(private val href: Url) : Parcelable {

public companion object {
/**
* Creates an [Href] from a valid URL.
*/
public operator fun invoke(href: SharedUrl): Href =
Href(StaticUrl(href))
/**
* Creates an [Href] from a valid URL.
*/
public constructor(href: SharedUrl) : this(StaticUrl(href))

public companion object {
/**
* Creates an [Href] from an URL template (RFC 6570).
* Creates an [Href] from a valid URL or URL template (RFC 6570).
*
* @param templated Indicates whether [href] is a URL template.
*/
@Suppress("RedundantNullableReturnType") // In case we validate the URI template at some point.
public fun fromTemplate(href: String): Href? =
Href(TemplatedUrl(href))
public operator fun invoke(href: String, templated: Boolean = false): Href? =
if (templated) {
Href(TemplatedUrl(href))
} else {
SharedUrl(href)?.let { Href(StaticUrl(it)) }
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public data class Link(

val templated = json.optBoolean("templated", false)
val href = if (templated) {
Href.fromTemplate(hrefString)
Href(hrefString, templated = true)
} else {
// We support existing publications with incorrect HREFs (not valid percent-encoded
// URIs). We try to parse them first as valid, but fall back on a percent-decoded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ private sealed class RouteHandler {
private val path = "/~readium/rights/copy"

override val link: Link = Link(
href = Href.fromTemplate("$path{?text,peek}")!!,
href = Href("$path{?text,peek}", templated = true)!!,
mediaType = mediaType
)

Expand Down Expand Up @@ -323,7 +323,7 @@ private sealed class RouteHandler {
private val path = "/~readium/rights/print"

override val link = Link(
href = Href.fromTemplate("$path{?pageCount,peek}")!!,
href = Href("$path{?pageCount,peek}", templated = true)!!,
mediaType = mediaType
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.readium.r2.shared.assertJSONEquals
import org.readium.r2.shared.toJSON
import org.readium.r2.shared.util.Url
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
Expand Down Expand Up @@ -46,8 +45,8 @@ class ContributorTest {
roles = setOf("bassist"),
position = 4.0,
links = listOf(
Link(href = Href(Url("http://link1")!!)),
Link(href = Href(Url("http://link2")!!))
Link(href = Href("http://link1")!!),
Link(href = Href("http://link2")!!)
)
),
Contributor.fromJSON(
Expand Down Expand Up @@ -218,8 +217,8 @@ class ContributorTest {
roles = setOf("bassist"),
position = 4.0,
links = listOf(
Link(href = Href(Url("http://link1")!!)),
Link(href = Href(Url("http://link2")!!))
Link(href = Href("http://link1")!!),
Link(href = Href("http://link2")!!)
)
).toJSON()
)
Expand Down
Loading

0 comments on commit c2778b1

Please sign in to comment.