Skip to content

Commit 5f3c819

Browse files
committed
minor refactor
1 parent a788677 commit 5f3c819

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/jvmMain/kotlin/dev/sargunv/pokekotlin/client/PokeApiClient.kt

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,26 @@ class PokeApiClient(
1111
baseUrl: String = "https://pokeapi.co/api/v2/",
1212
engine: HttpClientEngine? = null,
1313
configure: HttpClientConfig<*>.() -> Unit = {},
14-
) :
15-
PokeApi by (run {
16-
fun HttpClientConfig<*>.fullyConfigure() {
14+
) : PokeApi by getInstance(baseUrl, engine, configure) {
15+
private companion object {
16+
private fun HttpClientConfig<*>.configureWithJson(configure: HttpClientConfig<*>.() -> Unit) {
1717
install(ContentNegotiation) { json(PokeApiJson) }
1818
configure()
1919
}
20-
Builder()
21-
.apply {
22-
baseUrl(baseUrl)
23-
httpClient(
24-
if (engine != null) HttpClient(engine) { fullyConfigure() }
25-
else HttpClient { fullyConfigure() }
20+
21+
private fun getInstance(
22+
baseUrl: String,
23+
engine: HttpClientEngine?,
24+
configure: HttpClientConfig<*>.() -> Unit,
25+
) =
26+
Builder()
27+
.baseUrl(baseUrl)
28+
.httpClient(
29+
client =
30+
if (engine == null) HttpClient { configureWithJson(configure) }
31+
else HttpClient(engine) { configureWithJson(configure) }
2632
)
27-
}
28-
.build()
29-
.createPokeApi()
30-
})
33+
.build()
34+
.createPokeApi()
35+
}
36+
}

0 commit comments

Comments
 (0)