Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
osoykan committed Nov 28, 2024
1 parent be8a6b6 commit 886361c
Showing 1 changed file with 7 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.trendyol.stove.testing.e2e.http

import arrow.core.toOption
import com.trendyol.stove.testing.e2e.serialization.StoveSerde
import io.ktor.client.statement.*
import io.ktor.http.*
Expand All @@ -13,10 +14,7 @@ fun <T> HttpStatement.readJsonTextStream(transform: suspend (line: String) -> T)
execute {
check(it.status.isSuccess()) { "Request failed with status: ${it.status}" }
while (!it.content.isClosedForRead) {
it.content.readUTF8Line()?.let { line ->
if (line.isBlank()) return@let
emit(transform(line))
}
it.content.readUTF8LineNonEmpty { line -> emit(transform(line)) }

Check warning on line 17 in lib/stove-testing-e2e-http/src/main/kotlin/com/trendyol/stove/testing/e2e/http/streaming.kt

View check run for this annotation

Codecov / codecov/patch

lib/stove-testing-e2e-http/src/main/kotlin/com/trendyol/stove/testing/e2e/http/streaming.kt#L17

Added line #L17 was not covered by tests
}
}
}
Expand All @@ -27,14 +25,15 @@ fun <T> HttpStatement.readJsonContentStream(transform: suspend (line: ByteReadCh
execute {
check(it.status.isSuccess()) { "Request failed with status: ${it.status}" }
while (!it.content.isClosedForRead) {
it.content.readUTF8Line()?.let { line ->
if (line.isBlank()) return@let
emit(transform(ByteReadChannel(line.toByteArray())))
}
it.content.readUTF8LineNonEmpty { line -> emit(transform(ByteReadChannel(line.toByteArray()))) }
}
}
}

private suspend fun ByteReadChannel.readUTF8LineNonEmpty(onRead: suspend (String) -> Unit) {
readUTF8Line().toOption().filter { it.isNotBlank() }.map { onRead(it) }
}

/**
* Serializes the items to a stream of JSON strings.
*/
Expand Down

0 comments on commit 886361c

Please sign in to comment.