From 95be1a26f244608fdc37f30bb31ea9e9f37ed951 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 10 Feb 2023 15:14:03 +0600 Subject: [PATCH] improvements in HttpResponse.bodyOrNull --- CHANGELOG.md | 4 ++++ .../kotlin/dev/inmo/micro_utils/ktor/client/BodyOrNull.kt | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4388c09b9ac..f104a5c4759 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 0.16.9 +* `Ktor`: + * `Client` + * `HttpResponse.bodyOrNull` now retrieve callback to check if body should be received or null + ## 0.16.8 * `Versions`: diff --git a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/BodyOrNull.kt b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/BodyOrNull.kt index 55abf3b7c87..251d5ab3703 100644 --- a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/BodyOrNull.kt +++ b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/BodyOrNull.kt @@ -4,6 +4,6 @@ import io.ktor.client.call.body import io.ktor.client.statement.HttpResponse import io.ktor.http.HttpStatusCode -suspend inline fun HttpResponse.bodyOrNull() = takeIf { - status == HttpStatusCode.OK -} ?.body() +suspend inline fun HttpResponse.bodyOrNull( + statusFilter: (HttpResponse) -> Boolean = { it.status == HttpStatusCode.OK } +) = takeIf(statusFilter) ?.body()