Skip to content

Commit 721195c

Browse files
committed
chore: switch to using ktor
1 parent 6c13c57 commit 721195c

File tree

9 files changed

+68
-112
lines changed

9 files changed

+68
-112
lines changed

composeApp/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ kotlin {
9999
iosMain.dependencies {
100100
implementation(libs.sqldelight.native)
101101
implementation(libs.bundles.mobile)
102+
implementation(libs.bundles.ios)
102103
}
103104
val desktopMain by getting {
104105
dependencies {

composeApp/src/androidMain/kotlin/org/ooni/probe/net/Http.android.kt

Lines changed: 0 additions & 38 deletions
This file was deleted.

composeApp/src/commonMain/kotlin/org/ooni/probe/di/Dependencies.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import org.ooni.engine.Engine
1717
import org.ooni.engine.NetworkTypeFinder
1818
import org.ooni.engine.OonimkallBridge
1919
import org.ooni.engine.TaskEventMapper
20-
import org.ooni.probe.net.httpGetBytes
2120
import org.ooni.probe.Database
2221
import org.ooni.probe.background.RunBackgroundTask
2322
import org.ooni.probe.config.BatteryOptimization
@@ -210,7 +209,6 @@ class Dependencies(
210209
private val downloader by lazy {
211210
DownloadFile(
212211
fileSystem = FileSystem.SYSTEM,
213-
fetchBytes = ::httpGetBytes,
214212
)
215213
}
216214

composeApp/src/commonMain/kotlin/org/ooni/probe/domain/DownloadFile.kt

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package org.ooni.probe.domain
22

3+
import io.ktor.client.HttpClient
4+
import io.ktor.client.request.get
5+
import io.ktor.client.statement.bodyAsBytes
6+
import io.ktor.http.isSuccess
37
import okio.FileSystem
48
import okio.Path
59
import okio.Path.Companion.toPath
610
import okio.buffer
711
import okio.use
12+
import org.ooni.engine.models.Failure
813
import org.ooni.engine.models.Result
14+
import org.ooni.engine.models.Success
915
import org.ooni.probe.data.models.GetBytesException
1016

1117
/**
@@ -15,7 +21,6 @@ import org.ooni.probe.data.models.GetBytesException
1521
*/
1622
class DownloadFile(
1723
private val fileSystem: FileSystem,
18-
private val fetchBytes: suspend (url: String) -> Result<ByteArray, GetBytesException>,
1924
) {
2025
suspend operator fun invoke(
2126
url: String,
@@ -30,4 +35,32 @@ class DownloadFile(
3035
target
3136
}
3237
}
38+
39+
40+
/**
41+
* Perform a simple HTTP GET and return the raw response body bytes.
42+
* Uses Ktor HttpClient for cross-platform HTTP operations.
43+
*/
44+
suspend fun fetchBytes(url: String): Result<ByteArray, GetBytesException> {
45+
val client: HttpClient = HttpClient()
46+
47+
return try {
48+
val response = client.get(url)
49+
val bytes = response.bodyAsBytes()
50+
51+
if (response.status.isSuccess()) {
52+
Success(bytes)
53+
} else {
54+
Failure(
55+
GetBytesException(
56+
Exception("HTTP ${response.status.value} while GET $url: ${bytes.decodeToString()}")
57+
)
58+
)
59+
}
60+
} catch (e: Throwable) {
61+
Failure(GetBytesException(e))
62+
} finally {
63+
client.close()
64+
}
65+
}
3366
}

composeApp/src/commonMain/kotlin/org/ooni/probe/domain/FetchGeoIpDbUpdates.kt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,31 @@ class FetchGeoIpDbUpdates(
3030
private const val GEOIP_DB_REPO: String = "aanorbel/oomplt-mmdb"
3131
}
3232

33-
suspend operator fun invoke(): Result<Path?, MkException> =
34-
getLatestEngineVersion()
33+
suspend operator fun invoke(): Result<Path?, MkException> {
34+
// Check if we've already checked today
35+
val lastCheckMillis = preferencesRepository.getValueByKey(SettingsKey.MMDB_LAST_CHECK).first() as? Long
36+
if (lastCheckMillis != null) {
37+
val currentTimeMillis = Clock.System.now().toEpochMilliseconds()
38+
val oneDayInMillis = 24 * 60 * 60 * 1000L
39+
val timeSinceLastCheck = currentTimeMillis - lastCheckMillis
40+
41+
if (timeSinceLastCheck < oneDayInMillis) {
42+
// Less than a day has passed, skip the check
43+
return Success(null)
44+
}
45+
}
46+
47+
return getLatestEngineVersion()
3548
.flatMap { version ->
3649
val latest: String =
3750
version ?: return@flatMap Failure(MkException(IllegalStateException("Failed to fetch latest GeoIP DB release")))
3851
val (isLatest, latestVersion) = isGeoIpDbLatest(latest)
3952
if (isLatest) {
53+
// Update last check time even when already at latest version
54+
preferencesRepository.setValueByKey(
55+
SettingsKey.MMDB_LAST_CHECK,
56+
Clock.System.now().toEpochMilliseconds(),
57+
)
4058
Success(null)
4159
} else {
4260
val url = buildGeoIpDbUrl(latestVersion)
@@ -58,6 +76,7 @@ class FetchGeoIpDbUpdates(
5876
}
5977
}
6078
}
79+
}
6180

6281
/**
6382
* Compare latest and current version integers and return pair of latest state and actual version number

composeApp/src/commonMain/kotlin/org/ooni/probe/net/Http.kt

Lines changed: 0 additions & 10 deletions
This file was deleted.

composeApp/src/desktopMain/kotlin/org/ooni/probe/net/Http.desktop.kt

Lines changed: 0 additions & 1 deletion
This file was deleted.

composeApp/src/iosMain/kotlin/org/ooni/probe/net/Http.ios.kt

Lines changed: 0 additions & 57 deletions
This file was deleted.

gradle/libs.versions.toml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ sqldelight = "2.1.0"
1212
dataStoreVersion = "1.1.4"
1313
junitKtx = "1.3.0"
1414
mokoPermissions = "0.20.1"
15+
ktor = "3.3.1"
1516

1617
[plugins]
1718

@@ -48,6 +49,10 @@ window-size = { module = "org.jetbrains.compose.material3:material3-window-size-
4849
back-handler = { module = "org.jetbrains.compose.ui:ui-backhandler", version.ref = "compose-plugin" }
4950
material-icons = { module = "org.jetbrains.compose.material:material-icons-core", version = "1.7.3" }
5051
dark-mode-detector = { module = "io.github.kdroidfilter:platformtools.darkmodedetector", version = "0.7.4" }
52+
ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
53+
ktor-client-jvm = { module = "io.ktor:ktor-client-java", version.ref = "ktor" }
54+
ktor-client-okhttp = { module = "io.ktor:ktor-client-okhttp", version.ref = "ktor" }
55+
ktor-client-darwin = { module = "io.ktor:ktor-client-darwin", version.ref = "ktor" }
5156

5257
# Preferences
5358
androidx-datastore-core-okio = { group = "androidx.datastore", name = "datastore-core-okio", version.ref = "dataStoreVersion" }
@@ -132,6 +137,7 @@ tooling = [
132137
"markdown",
133138
"kottie",
134139
"web-view",
140+
"ktor-client-core",
135141
]
136142
android = [
137143
# "android-oonimkall",
@@ -140,6 +146,10 @@ android = [
140146
"android-work",
141147
"sqldelight-android",
142148
"android-appcompat",
149+
"ktor-client-okhttp",
150+
]
151+
ios = [
152+
"ktor-client-darwin"
143153
]
144154
mobile = [
145155
"moko-permissions-compose",
@@ -151,7 +161,8 @@ desktop = [
151161
"directories",
152162
"auto-launch",
153163
"pratanumandal-unique",
154-
"desktop-oonimkall"
164+
"desktop-oonimkall",
165+
"ktor-client-jvm",
155166
]
156167
full = [
157168
"sentry",

0 commit comments

Comments
 (0)