File tree Expand file tree Collapse file tree 2 files changed +8
-16
lines changed
src/main/kotlin/com/ctrlhub/core Expand file tree Collapse file tree 2 files changed +8
-16
lines changed Original file line number Diff line number Diff line change @@ -2,21 +2,23 @@ package com.ctrlhub.core
2
2
3
3
import com.ctrlhub.core.http.KtorClientFactory
4
4
import io.ktor.client.*
5
+ import io.ktor.client.plugins.defaultRequest
5
6
6
7
/* *
7
8
* The facade object through which interaction with the API occurs.
8
9
*/
9
10
class Api (
10
11
var httpClient : HttpClient = KtorClientFactory .create()
11
12
) {
12
- private var sessionToken: String? = null
13
-
14
13
fun withHttpClientConfig (config : HttpClientConfig <* >.() -> Unit ) {
15
14
httpClient = KtorClientFactory .create(configBlock = config)
16
15
}
17
16
18
- fun applySessionToken (newSessionToken : String ) {
19
- sessionToken = newSessionToken
20
- httpClient = KtorClientFactory .createWithExistingConfig(httpClient, newSessionToken)
17
+ fun applySessionToken (sessionToken : String ) {
18
+ httpClient = KtorClientFactory .create(httpClient) {
19
+ defaultRequest {
20
+ headers.append(" X-Session-Token" , sessionToken)
21
+ }
22
+ }
21
23
}
22
24
}
Original file line number Diff line number Diff line change @@ -21,28 +21,18 @@ import java.util.concurrent.atomic.AtomicReference
21
21
object KtorClientFactory {
22
22
fun create (
23
23
httpClient : HttpClient = HttpClient (CIO ),
24
- sessionToken : String? = null,
25
24
configBlock : HttpClientConfig <* >.() -> Unit = {}
26
25
): HttpClient {
27
- return configureHttpClient(httpClient, sessionToken, configBlock)
28
- }
29
-
30
- fun createWithExistingConfig (
31
- existingClient : HttpClient ,
32
- sessionToken : String? = null
33
- ): HttpClient {
34
- return configureHttpClient(existingClient, sessionToken)
26
+ return configureHttpClient(httpClient, configBlock)
35
27
}
36
28
37
29
private fun configureHttpClient (
38
30
baseClient : HttpClient ,
39
- sessionToken : String? = null,
40
31
configBlock : HttpClientConfig <* >.() -> Unit = {}
41
32
): HttpClient {
42
33
return baseClient.config {
43
34
defaultRequest {
44
35
url(Config .apiBaseUrl)
45
- sessionToken?.let { headers.append(" X-Session-Token" , it) }
46
36
headers.appendIfNameAbsent(HttpHeaders .ContentType , " application/json" )
47
37
}
48
38
expectSuccess = true
You can’t perform that action at this time.
0 commit comments