Skip to content

Commit 724e1a1

Browse files
committed
Added possibility to configure the http client during initialization
1 parent bcdd6e3 commit 724e1a1

File tree

1 file changed

+26
-18
lines changed
  • kotlin-http-client/kotlin-http-client-ktor/src/main/kotlin/com/linkedplanet/kotlinhttpclient/ktor

1 file changed

+26
-18
lines changed

kotlin-http-client/kotlin-http-client-ktor/src/main/kotlin/com/linkedplanet/kotlinhttpclient/ktor/KtorHttpClient.kt

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ import com.linkedplanet.kotlinhttpclient.api.http.BaseHttpClient
2525
import com.linkedplanet.kotlinhttpclient.api.http.HttpResponse
2626
import com.linkedplanet.kotlinhttpclient.error.HttpDomainError
2727
import io.ktor.client.HttpClient
28+
import io.ktor.client.HttpClientConfig
2829
import io.ktor.client.call.receive
2930
import io.ktor.client.engine.apache.Apache
31+
import io.ktor.client.engine.apache.ApacheEngineConfig
3032
import io.ktor.client.features.auth.Auth
3133
import io.ktor.client.features.auth.providers.BasicAuthCredentials
3234
import io.ktor.client.features.auth.providers.basic
@@ -37,29 +39,14 @@ import io.ktor.client.request.forms.MultiPartFormDataContent
3739
import io.ktor.client.request.forms.formData
3840
import io.ktor.http.*
3941

40-
fun httpClient(username: String, password: String) =
41-
HttpClient(Apache) {
42-
expectSuccess = false
43-
install(JsonFeature) {
44-
serializer = GsonSerializer()
45-
}
46-
install(Auth) {
47-
basic {
48-
sendWithoutRequest { true }
49-
credentials {
50-
BasicAuthCredentials(username = username, password = password)
51-
}
52-
}
53-
}
54-
}
55-
5642
class KtorHttpClient(
5743
private val baseUrl: String,
5844
username: String,
59-
password: String
45+
password: String,
46+
configureHttpClient: (HttpClientConfig<ApacheEngineConfig>) -> Unit = {}
6047
) : BaseHttpClient() {
6148

62-
private var httpClient: HttpClient = httpClient(username, password)
49+
private var httpClient: HttpClient = createHttpClient(username, password, configureHttpClient)
6350

6451
private fun prepareRequest(
6552
requestBuilder: HttpRequestBuilder,
@@ -176,3 +163,24 @@ class KtorHttpClient(
176163
).left()
177164
}
178165
}
166+
167+
private fun createHttpClient(
168+
username: String,
169+
password: String,
170+
configureHttpClient: (HttpClientConfig<ApacheEngineConfig>) -> Unit
171+
) =
172+
HttpClient(Apache) {
173+
expectSuccess = false
174+
install(JsonFeature) {
175+
serializer = GsonSerializer()
176+
}
177+
install(Auth) {
178+
basic {
179+
sendWithoutRequest { true }
180+
credentials {
181+
BasicAuthCredentials(username = username, password = password)
182+
}
183+
}
184+
}
185+
configureHttpClient(this)
186+
}

0 commit comments

Comments
 (0)