-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add customization of HTTP client configuration (#10). #11
Add customization of HTTP client configuration (#10). #11
Conversation
Thank you for your effort. I'll review this as soon as I get some free time in the next few days. feel free to make any other modifications if you find any thing to improve |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @msimonides. I reviewed your changes, here are my notes:
- Revert changes to
CHANGELOG.md
&README
, I'll update those as soon as I release a new version after merging your changes and I'll give you the credit for it. - I discovered that we can apply your proposed changes but also without sacrificing the type safety as you mentioned in the issue description, here is a pseudocode for the solution:
- Update PodcastIndexClientConfig to have a new function that accepts typed
HttpClientEngineFactory
internal var httpClientBuilder: (() -> HttpClient)? = null
/**
* 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)
}
}
- then update
PodcastIndexClient
in base module:
public class PodcastIndexClient
@InternalPodcastIndexApi
constructor(
- private val httpClientConfig: HttpClientConfig<*>.() -> Unit,
+ private val defaultHttpClientConfig: HttpClientConfig<*>.() -> Unit,
+ private val podcastIndexClientConfig: PodcastIndexClientConfig
) {
- private val client: HttpClient = HttpClient(defaultHttpClientEngineFactory()) {
- httpClientConfig()
+ private val client: HttpClient = podcastIndexClientConfig.httpClientBuilder?.invoke()?.config(defaultHttpClientConfig) ?: HttpClient(defaultHttpClientEngineFactory()) {
+ defaultHttpClientConfig()
}
- Update usages in ktor2 & ktor3
PodcastIndexClient
accordingly.
The end user can then call & configure the client like this:
val client = PodcastIndexClient(
API_KEY,
API_SECRET,
uAgentString,
) {
enableLogging = true
maxRetries = 4
httpClient(OkHttp) {
// Okhttp specific configuration without any explicit casting
}
}
Which is idiomatic, type-safe, and doesn't break any existing public API 🚀
Once you do these changes, we can approve & merge the changes ⭐
b33e1d7
to
25509ca
Compare
Ah, so basically approach no.2 but there's no need to pass the configuration through - it can be applied later. I made the |
Thanks for your contribution, I'll make a release with this new feature. |
No description provided.