@@ -2,6 +2,7 @@ package com.ctrlhub.core.http
2
2
3
3
import com.ctrlhub.core.Config
4
4
import io.ktor.client.HttpClient
5
+ import io.ktor.client.HttpClientConfig
5
6
import io.ktor.client.engine.cio.CIO
6
7
import io.ktor.client.plugins.UserAgent
7
8
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
@@ -10,34 +11,41 @@ import io.ktor.http.HttpHeaders
10
11
import io.ktor.serialization.kotlinx.json.json
11
12
import io.ktor.util.appendIfNameAbsent
12
13
import kotlinx.serialization.json.Json
14
+ import java.util.concurrent.atomic.AtomicReference
13
15
14
16
/* *
15
17
* A wrapper around the Ktor client. This allows default configuration
16
18
* to be applied that is specific to the Ctrl Hub API, but is also flexible
17
19
* in that an existing Ktor client can be passed, and config is applied to that.
18
20
*/
19
21
object KtorClientFactory {
20
- private lateinit var httpClient: HttpClient
21
-
22
- fun create (sessionToken : String? ): HttpClient {
23
- if (! this ::httpClient.isInitialized) {
24
- val httpClient = HttpClient (CIO )
25
- this .httpClient = configureHttpClient(httpClient, baseUrl = Config .apiBaseUrl, sessionToken = sessionToken)
26
- }
27
-
28
- return this .httpClient
22
+ // Function to create an HttpClient with configuration and optional session token
23
+ fun create (
24
+ httpClient : HttpClient = HttpClient (CIO ),
25
+ sessionToken : String? = null,
26
+ configBlock : HttpClientConfig <* >.() -> Unit = {}
27
+ ): HttpClient {
28
+ return configureHttpClient(httpClient, sessionToken, configBlock)
29
29
}
30
30
31
- fun create (httpClient : HttpClient ): HttpClient {
32
- this .httpClient = httpClient
33
- return configureHttpClient(httpClient, Config .apiBaseUrl)
31
+ // Function to create a new HttpClient based on an existing HttpClient configuration
32
+ fun createWithExistingConfig (
33
+ existingClient : HttpClient ,
34
+ sessionToken : String? = null
35
+ ): HttpClient {
36
+ return configureHttpClient(existingClient, sessionToken) // Reapply session token while retaining existing config
34
37
}
35
38
36
- private fun configureHttpClient (baseClient : HttpClient , baseUrl : String , sessionToken : String? = null): HttpClient {
39
+ // Function to configure the HttpClient
40
+ private fun configureHttpClient (
41
+ baseClient : HttpClient ,
42
+ sessionToken : String? = null,
43
+ configBlock : HttpClientConfig <* >.() -> Unit = {}
44
+ ): HttpClient {
37
45
return baseClient.config {
38
46
defaultRequest {
39
- url(baseUrl )
40
- sessionToken?.let { headers.append(" X-Session-Token" , it) }
47
+ url(Config .apiBaseUrl )
48
+ sessionToken?.let { headers.append(" X-Session-Token" , it) } // Apply session token if present
41
49
headers.appendIfNameAbsent(HttpHeaders .ContentType , " application/json" )
42
50
}
43
51
expectSuccess = true
@@ -50,6 +58,7 @@ object KtorClientFactory {
50
58
install(UserAgent ) {
51
59
agent = Config .userAgent
52
60
}
61
+ configBlock() // Apply additional user-defined config
53
62
}
54
63
}
55
64
}
0 commit comments