Skip to content

Commit

Permalink
Add customization of HTTP client configuration (#10).
Browse files Browse the repository at this point in the history
  • Loading branch information
msimonides committed Nov 12, 2024
1 parent 4697e1e commit 25509ca
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public fun PodcastIndexClient(
require(userAgent.isNotBlank()) { "User agent cannot be blank" }
val config = PodcastIndexClientConfig().apply { block() }
return PodcastIndexClient(
httpClientConfig = {
defaultHttpClientConfig = {
installAuthenticationPlugin(authKey, authSecret, userAgent)
installRetryPlugin(authKey, authSecret, maxRetries = config.maxRetries)
installSerializationPlugin()
Expand All @@ -36,5 +36,6 @@ public fun PodcastIndexClient(
installTimeoutPlugin(config.defaultTimeout)
}
},
podcastIndexClientConfig = config
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public fun PodcastIndexClient(
require(userAgent.isNotBlank()) { "User agent cannot be blank" }
val config = PodcastIndexClientConfig().apply { block() }
return PodcastIndexClient(
httpClientConfig = {
defaultHttpClientConfig = {
installAuthenticationPlugin(authKey, authSecret, userAgent)
installRetryPlugin(authKey, authSecret, maxRetries = config.maxRetries)
installSerializationPlugin()
Expand All @@ -36,5 +36,6 @@ public fun PodcastIndexClient(
installTimeoutPlugin(config.defaultTimeout)
}
},
podcastIndexClientConfig = config
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ import io.ktor.client.HttpClientConfig
public class PodcastIndexClient
@InternalPodcastIndexApi
constructor(
private val httpClientConfig: HttpClientConfig<*>.() -> Unit,
defaultHttpClientConfig: HttpClientConfig<*>.() -> Unit,
podcastIndexClientConfig: PodcastIndexClientConfig
) {

private val client: HttpClient = HttpClient(defaultHttpClientEngineFactory()) {
httpClientConfig()
}
private val client: HttpClient =
podcastIndexClientConfig.httpClientBuilder().config(defaultHttpClientConfig)

public val podcasts: Podcasts = Podcasts(client)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.mr3y.podcastindex

import io.ktor.client.HttpClient
import io.ktor.client.HttpClientConfig
import io.ktor.client.engine.HttpClientEngineConfig
import io.ktor.client.engine.HttpClientEngineFactory

@DslMarker
@InternalPodcastIndexApi
public annotation class PodcastIndexConfigDsl
Expand All @@ -10,6 +15,9 @@ public annotation class PodcastIndexConfigDsl
@PodcastIndexConfigDsl
public class PodcastIndexClientConfig {

internal var httpClientBuilder: (() -> HttpClient) =
{ HttpClient(defaultHttpClientEngineFactory()) }

/**
* Maximum number of retries for failed requests before giving up. Default is 3.
*/
Expand All @@ -34,4 +42,20 @@ public class PodcastIndexClientConfig {
* The default timeout for requests in milliseconds. Default is 10_000.
*/
public var defaultTimeout: Long = 10_000

/**
* Creates an custom [HttpClient] with the specified [HttpClientEngineFactory] and optional
* [block] configuration.
*
* Note that [block] configuration doesn't replace the existing default configuration added by
* this library. It will be applied on top of it.
*/
public fun <T : HttpClientEngineConfig> httpClient(
engineFactory: HttpClientEngineFactory<T>,
block: HttpClientConfig<T>.() -> Unit = {}
) {
httpClientBuilder = {
HttpClient(engineFactory, block)
}
}
}

0 comments on commit 25509ca

Please sign in to comment.