Skip to content

Commit

Permalink
Upgrade ktor to v3
Browse files Browse the repository at this point in the history
  • Loading branch information
Iamlooker committed Dec 21, 2024
1 parent 97e8215 commit acbdf31
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 28 deletions.
1 change: 1 addition & 0 deletions core/network/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
alias(libs.plugins.looker.jvm.library)
alias(libs.plugins.looker.hilt)
alias(libs.plugins.looker.lint)
alias(libs.plugins.ktor)
}

dependencies {
Expand Down
30 changes: 6 additions & 24 deletions core/network/src/main/kotlin/com/looker/network/KtorDownloader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,9 @@ import io.ktor.http.HttpStatusCode
import io.ktor.http.etag
import io.ktor.http.isSuccess
import io.ktor.http.lastModified
import io.ktor.utils.io.ByteReadChannel
import io.ktor.utils.io.CancellationException
import io.ktor.utils.io.core.ByteReadPacket
import io.ktor.utils.io.core.isEmpty
import io.ktor.utils.io.core.readBytes
import io.ktor.utils.io.jvm.javaio.copyTo
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.isActive
import kotlinx.coroutines.withContext
import java.io.File
import java.io.IOException
Expand Down Expand Up @@ -88,7 +83,7 @@ internal class KtorDownloader(
if (networkResponse !is NetworkResponse.Success) {
return@execute networkResponse
}
response.bodyAsChannel() saveTo target
response.bodyAsChannel().copyTo(target.outputStream())
validator?.validate(target)
networkResponse
}
Expand Down Expand Up @@ -139,27 +134,14 @@ internal class KtorDownloader(
}
onDownload { read, total ->
if (block != null) {
block(DataSize(read + (fileSize ?: 0L)), DataSize(total + (fileSize ?: 0L)))
block(
DataSize(read + (fileSize ?: 0L)),
DataSize((total ?: 0L) + (fileSize ?: 0L))
)
}
}
}

suspend infix fun ByteReadChannel.saveTo(target: File) =
withContext(Dispatchers.IO) {
while (!isClosedForRead && isActive) {
val packet = readRemaining(DEFAULT_BUFFER_SIZE.toLong())
packet.appendTo(target)
}
}

suspend fun ByteReadPacket.appendTo(file: File) =
withContext(Dispatchers.IO) {
while (!isEmpty && isActive) {
val bytes = readBytes()
file.appendBytes(bytes)
}
}

fun HttpResponse.asNetworkResponse(): NetworkResponse =
if (status.isSuccess() || status == HttpStatusCode.NotModified) {
NetworkResponse.Success(status.value, lastModified(), etag())
Expand Down
9 changes: 5 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ kotlinxDatetime = "0.6.1"
kotlinxSerializationJson = "1.7.3"
ksp = "2.1.0-1.0.29"
ktlint = "12.1.2"
ktor = "2.3.13"
ktor = "3.0.3"
libsu = "6.0.0"
room = "2.6.1"
shizuku = "13.0.0"
Expand Down Expand Up @@ -83,9 +83,9 @@ kotlinx-coroutines-guava = { group = "org.jetbrains.kotlinx", name = "kotlinx-co
kotlinx-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "kotlinxCoroutines" }
kotlinx-datetime = { group = "org.jetbrains.kotlinx", name = "kotlinx-datetime", version.ref = "kotlinxDatetime" }
kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "kotlinxSerializationJson" }
ktor-core = { group = "io.ktor", name = "ktor-client-core", version.ref = "ktor" }
ktor-okhttp = { group = "io.ktor", name = "ktor-client-okhttp", version.ref = "ktor" }
ktor-mock = { group = "io.ktor", name = "ktor-client-mock", version.ref = "ktor" }
ktor-core = { group = "io.ktor", name = "ktor-client-core" }
ktor-okhttp = { group = "io.ktor", name = "ktor-client-okhttp" }
ktor-mock = { group = "io.ktor", name = "ktor-client-mock" }
libsu-core = { group = "com.github.topjohnwu.libsu", name = "core", version.ref = "libsu" }
room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "room" }
room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "room" }
Expand Down Expand Up @@ -113,6 +113,7 @@ kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-parcelize = { id = "org.jetbrains.kotlin.plugin.parcelize", version.ref = "kotlin" }
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
ktor = { id = "io.ktor.plugin", version.ref = "ktor" }

# Plugins defined by this project
looker-android-application = { id = "looker.android.application", version = "unspecified" }
Expand Down

0 comments on commit acbdf31

Please sign in to comment.