Skip to content

Commit 87ec94a

Browse files
committed
refactor: KtorGithubLanguageColorApi
1 parent 7891aee commit 87ec94a

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

shared/src/commonMain/kotlin/com/hoc081098/github_search_kmm/data/remote/KtorGithubLanguageColorApi.kt

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import io.ktor.client.request.get
1111
import io.ktor.http.Url
1212
import kotlinx.coroutines.withContext
1313

14+
typealias ColorResponseType = Map<String, Map<String, String?>>
15+
1416
internal open class KtorGithubLanguageColorApi(
1517
private val url: Url,
1618
private val httpClient: HttpClient,
@@ -21,21 +23,22 @@ internal open class KtorGithubLanguageColorApi(
2123
.catch {
2224
httpClient
2325
.get(url)
24-
.body<Map<String, Map<String, String?>>>()
26+
.body<ColorResponseType>()
27+
}
28+
.flatMap { it.toColors() }
29+
}
30+
31+
private fun ColorResponseType.toColors(): Either<IllegalStateException, Map<String, ArgbColor>> {
32+
return mapNotNull { (k, v) ->
33+
v["color"]?.let {
34+
k to it
2535
}
26-
.flatMap { map ->
27-
map
28-
.mapNotNull { (k, v) ->
29-
v["color"]?.let {
30-
k to it
31-
}
32-
}
33-
.traverse { (k, v) ->
34-
ArgbColor
35-
.parse(v)
36-
.mapLeft(::IllegalStateException)
37-
.map { k to it }
38-
}
36+
}
37+
.traverse { (k, v) ->
38+
ArgbColor
39+
.parse(v)
40+
.mapLeft(::IllegalStateException)
41+
.map { k to it }
3942
}
4043
.map { it.toMap() }
4144
}

0 commit comments

Comments
 (0)