Skip to content
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

Conversation

msimonides
Copy link
Contributor

No description provided.

@mr3y-the-programmer mr3y-the-programmer added the enhancement New feature or request label Nov 11, 2024
@mr3y-the-programmer
Copy link
Owner

mr3y-the-programmer commented Nov 11, 2024

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

Copy link
Owner

@mr3y-the-programmer mr3y-the-programmer left a 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:

  1. 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.
  2. 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 ⭐

@msimonides msimonides force-pushed the custom_http_client_config branch from b33e1d7 to 25509ca Compare November 12, 2024 21:23
@msimonides
Copy link
Contributor Author

Ah, so basically approach no.2 but there's no need to pass the configuration through - it can be applied later.

I made the httpClientBuilder non-nullable, it's initialized to build the default HttpClient

@mr3y-the-programmer
Copy link
Owner

Thanks for your contribution, I'll make a release with this new feature.

@mr3y-the-programmer mr3y-the-programmer merged commit 91b1b94 into mr3y-the-programmer:main Nov 12, 2024
1 check passed
@msimonides msimonides deleted the custom_http_client_config branch November 13, 2024 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants