Skip to content

Commit d0d0594

Browse files
Add Value service
1 parent 27359d0 commit d0d0594

File tree

5 files changed

+79
-12
lines changed

5 files changed

+79
-12
lines changed

podcastindex-sdk/src/commonMain/kotlin/com/mr3y/podcastindex/PodcastIndexClient.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.mr3y.podcastindex.services.Misc
1010
import com.mr3y.podcastindex.services.Podcasts
1111
import com.mr3y.podcastindex.services.Recent
1212
import com.mr3y.podcastindex.services.Search
13+
import com.mr3y.podcastindex.services.Value
1314
import io.ktor.client.HttpClient
1415

1516
/**
@@ -64,6 +65,8 @@ public class PodcastIndexClient internal constructor(
6465

6566
public val recent: Recent = Recent(client)
6667

68+
public val value: Value = Value(client)
69+
6770
public val misc: Misc = Misc(client)
6871

6972
internal companion object {

podcastindex-sdk/src/commonMain/kotlin/com/mr3y/podcastindex/extensions/HttpClient.kt

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ internal fun HttpClientConfig<*>.installAuthenticationPlugin(authenticationInfo:
2121
// Add the necessary authentication headers to every request going out to PodcastIndex endpoints
2222
val requestAuthenticationPlugin = createClientPlugin("requestAuthenticatorPlugin") {
2323
onRequest { request, _ ->
24-
request.headers {
25-
val epoch = (Clock.System.now().toEpochMilliseconds() / 1000)
26-
append("User-Agent", authenticationInfo.userAgent)
27-
append("X-Auth-Date", epoch.toString())
28-
append("X-Auth-Key", authenticationInfo.key)
29-
append("Authorization", authHeader(epoch, authenticationInfo.key, authenticationInfo.secret))
24+
if (request.url.pathSegments.drop(3)[0] != "value") {
25+
request.headers {
26+
val epoch = (Clock.System.now().toEpochMilliseconds() / 1000)
27+
append("User-Agent", authenticationInfo.userAgent)
28+
append("X-Auth-Date", epoch.toString())
29+
append("X-Auth-Key", authenticationInfo.key)
30+
append("Authorization", authHeader(epoch, authenticationInfo.key, authenticationInfo.secret))
31+
}
3032
}
3133
}
3234
}
@@ -36,12 +38,14 @@ internal fun HttpClientConfig<*>.installAuthenticationPlugin(authenticationInfo:
3638
internal fun HttpClientConfig<*>.installRetryPlugin(authenticationInfo: Authentication, maxRetries: Int) {
3739
install(HttpRequestRetry) {
3840
modifyRequest { request ->
39-
// The server expects the auth date to be within a 3 minutes time window around the server time
40-
// but sometimes the request time/auth date is off by a few seconds/milliseconds, therefore,
41-
// to solve this, retry the request with a 1.5 minutes offset.
42-
val epoch = (Clock.System.now().toEpochMilliseconds() / 1000) - 150
43-
request.headers["X-Auth-Date"] = epoch.toString()
44-
request.headers["Authorization"] = authHeader(epoch, authenticationInfo.key, authenticationInfo.secret)
41+
if (request.url.pathSegments.drop(3)[0] != "value") {
42+
// The server expects the auth date to be within a 3 minutes time window around the server time
43+
// but sometimes the request time/auth date is off by a few seconds/milliseconds, therefore,
44+
// to solve this, retry the request with a 1.5 minutes offset.
45+
val epoch = (Clock.System.now().toEpochMilliseconds() / 1000) - 150
46+
request.headers["X-Auth-Date"] = epoch.toString()
47+
request.headers["Authorization"] = authHeader(epoch, authenticationInfo.key, authenticationInfo.secret)
48+
}
4549
}
4650
retryIf(maxRetries) { _, httpResponse ->
4751
when {

podcastindex-sdk/src/commonMain/kotlin/com/mr3y/podcastindex/model/PodcastFeed.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ public enum class Locked(public val code: Int) {
7979
public class Value4Value(
8080
@SerialName(value = "model") public val model: Value4ValueModel,
8181
@SerialName(value = "destinations") public val destinations: List<Value4ValueDestination>,
82+
@SerialName(value = "title") public val title: String? = null,
83+
@SerialName(value = "feedTitle") public val feedTitle: String? = null
8284
)
8385

8486
@Serializable
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.mr3y.podcastindex.model
2+
3+
import dev.drewhamilton.poko.Poko
4+
import kotlinx.serialization.SerialName
5+
import kotlinx.serialization.Serializable
6+
7+
@Serializable
8+
@Poko
9+
public class ValueFeedResult(
10+
@SerialName(value = "value") public val value: Value4Value,
11+
@SerialName(value = "description") public val description: String,
12+
)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.mr3y.podcastindex.services
2+
3+
import com.mr3y.podcastindex.PodcastIndexClient
4+
import com.mr3y.podcastindex.extensions.withErrorHandling
5+
import com.mr3y.podcastindex.model.MultipleEpisodesResult
6+
import com.mr3y.podcastindex.model.ValueFeedResult
7+
import io.ktor.client.HttpClient
8+
import io.ktor.client.request.get
9+
import io.ktor.client.request.parameter
10+
11+
public class Value internal constructor(private val client: HttpClient) {
12+
13+
public suspend fun byFeedId(
14+
id: Long,
15+
): ValueFeedResult = withErrorHandling {
16+
client.get("${PodcastIndexClient.BaseUrl}/value/byfeedid") {
17+
parameter("id", id)
18+
}
19+
}
20+
21+
public suspend fun byFeedUrl(
22+
url: String,
23+
): ValueFeedResult = withErrorHandling {
24+
client.get("${PodcastIndexClient.BaseUrl}/value/byfeedurl") {
25+
parameter("url", url)
26+
}
27+
}
28+
29+
public suspend fun byFeedGuid(
30+
guid: String,
31+
): ValueFeedResult = withErrorHandling {
32+
client.get("${PodcastIndexClient.BaseUrl}/value/bypodcastguid") {
33+
parameter("guid", guid)
34+
}
35+
}
36+
37+
public suspend fun byEpisodeGuid(
38+
podcastGuid: String,
39+
episodeGuid: String,
40+
): ValueFeedResult = withErrorHandling {
41+
client.get("${PodcastIndexClient.BaseUrl}/value/byepisodeguid") {
42+
parameter("podcastguid", podcastGuid)
43+
parameter("episodeguid", episodeGuid)
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)