feat(shared): add expect/actual Ktor engine and Wikipedia client; fix Ktor import
This commit is contained in:
@@ -2,6 +2,7 @@ plugins {
|
||||
alias(libs.plugins.kotlin.multiplatform)
|
||||
alias(libs.plugins.android.kotlin.multiplatform.library)
|
||||
alias(libs.plugins.android.lint)
|
||||
alias(libs.plugins.kotlin.serialization)
|
||||
}
|
||||
|
||||
kotlin {
|
||||
@@ -60,7 +61,14 @@ kotlin {
|
||||
commonMain {
|
||||
dependencies {
|
||||
implementation(libs.kotlin.stdlib)
|
||||
implementation(libs.ktor.client.core)
|
||||
// Add KMP dependencies here
|
||||
|
||||
implementation(libs.ktor.client.content.negotiation)
|
||||
implementation(libs.ktor.serialization.kotlinx.json)
|
||||
// JSON-runtime explicit (bra för versionssäkring)
|
||||
implementation(libs.kotlinx.serialization.json)
|
||||
implementation(libs.ktor.client.logging)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,6 +83,8 @@ kotlin {
|
||||
// Add Android-specific dependencies here. Note that this source set depends on
|
||||
// commonMain by default and will correctly pull the Android artifacts of any KMP
|
||||
// dependencies declared in commonMain.
|
||||
implementation(libs.ktor.client.okhttp)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,6 +103,7 @@ kotlin {
|
||||
// part of KMP’s default source set hierarchy. Note that this source set depends
|
||||
// on common by default and will correctly pull the iOS artifacts of any
|
||||
// KMP dependencies declared in commonMain.
|
||||
implementation(libs.ktor.client.darwin)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package app.klottr.net
|
||||
|
||||
import io.ktor.client.engine.HttpClientEngineFactory
|
||||
import io.ktor.client.engine.okhttp.OkHttp
|
||||
|
||||
internal actual fun platformEngine(): HttpClientEngineFactory<*> = OkHttp
|
||||
@@ -0,0 +1,6 @@
|
||||
package app.klottr.net
|
||||
|
||||
import io.ktor.client.engine.HttpClientEngineFactory
|
||||
|
||||
// Plattformsabstraktion: vi väljer engine per plattform (OkHttp på Android, Darwin på iOS).
|
||||
internal expect fun platformEngine(): HttpClientEngineFactory<*>
|
||||
115
shared/src/commonMain/kotlin/app/klottr/net/WikipediaClient.kt
Normal file
115
shared/src/commonMain/kotlin/app/klottr/net/WikipediaClient.kt
Normal file
@@ -0,0 +1,115 @@
|
||||
package app.klottr.net
|
||||
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.plugins.DefaultRequest
|
||||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
|
||||
import io.ktor.client.plugins.logging.LogLevel
|
||||
import io.ktor.client.plugins.logging.Logging
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.request.parameter
|
||||
import io.ktor.serialization.kotlinx.json.json
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
/**
|
||||
* Delad Wikipedia-klient (KMP) via Ktor + kotlinx.serialization.
|
||||
* - Håller sig till samma API som din tidigare implementering i app (sv.wikipedia.org, formatversion=2).
|
||||
* - Returnerar tomma värden/emptyList vid nätfel (kraschar inte).
|
||||
* - User-Agent sätts per default request.
|
||||
*
|
||||
* OBS: Vi byter inte över app-koden till denna direkt. Först lägger vi in den i shared,
|
||||
* sedan provar vi att byta anrop på Android i ett separat, litet steg.
|
||||
*/
|
||||
object WikipediaShared {
|
||||
|
||||
// En HttpClient per process – återanvänds (lazy).
|
||||
private val client by lazy {
|
||||
HttpClient(platformEngine()) {
|
||||
// JSON-konfiguration: tolerant och ignorera okända fält
|
||||
install(ContentNegotiation) {
|
||||
json(
|
||||
Json {
|
||||
ignoreUnknownKeys = true
|
||||
isLenient = true
|
||||
}
|
||||
)
|
||||
}
|
||||
// Rimlig loggnivå – justeras senare vid behov
|
||||
install(Logging) {
|
||||
level = LogLevel.INFO
|
||||
}
|
||||
// Sätt en tydlig User-Agent
|
||||
install(DefaultRequest) {
|
||||
headers.append("User-Agent", "Klottr/0.3.0 (KMP)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ====== Geosearch (nära) ======
|
||||
|
||||
@Serializable
|
||||
private data class GeoSearchItem(
|
||||
val pageid: Long = 0,
|
||||
val title: String = "",
|
||||
val lat: Double = 0.0,
|
||||
val lon: Double = 0.0
|
||||
)
|
||||
|
||||
@Serializable private data class GeoSearchQuery(val geosearch: List<GeoSearchItem> = emptyList())
|
||||
@Serializable private data class GeoSearchResponse(val query: GeoSearchQuery? = null)
|
||||
|
||||
/**
|
||||
* Hämtar närliggande Wikipedia-sidor. Speglar din gamla getNearby().
|
||||
* Vid fel: returnerar tom lista.
|
||||
*/
|
||||
suspend fun getNearby(lat: Double, lon: Double, radiusMeters: Int): List<WikipediaPage> = try {
|
||||
val resp: GeoSearchResponse = client.get("https://sv.wikipedia.org/w/api.php") {
|
||||
parameter("action", "query")
|
||||
parameter("list", "geosearch")
|
||||
parameter("gscoord", "$lat|$lon")
|
||||
parameter("gsradius", radiusMeters)
|
||||
parameter("gslimit", 50)
|
||||
parameter("format", "json")
|
||||
parameter("formatversion", 2)
|
||||
}.body()
|
||||
|
||||
resp.query?.geosearch
|
||||
?.map { WikipediaPage(it.pageid, it.title, it.lat, it.lon) }
|
||||
?.orEmpty()
|
||||
?: emptyList()
|
||||
} catch (_: Throwable) {
|
||||
emptyList()
|
||||
}
|
||||
|
||||
// ====== Extract (sammanfattning) ======
|
||||
|
||||
@Serializable private data class ExtractPage(
|
||||
val pageid: Long? = null,
|
||||
val title: String? = null,
|
||||
val extract: String? = null
|
||||
)
|
||||
@Serializable private data class ExtractQuery(val pages: List<ExtractPage> = emptyList())
|
||||
@Serializable private data class ExtractResponse(val query: ExtractQuery? = null)
|
||||
|
||||
/**
|
||||
* Hämtar extrakt (introtext) för en pageId. Speglar din gamla getExtract().
|
||||
* Vid fel/404: returnerar tom WikipediaDetail med rätt pageId.
|
||||
*/
|
||||
suspend fun getExtract(pageId: Long): WikipediaDetail = try {
|
||||
val resp: ExtractResponse = client.get("https://sv.wikipedia.org/w/api.php") {
|
||||
parameter("action", "query")
|
||||
parameter("prop", "extracts")
|
||||
parameter("exintro", 1)
|
||||
parameter("explaintext", 1)
|
||||
parameter("format", "json")
|
||||
parameter("formatversion", 2)
|
||||
parameter("pageids", pageId)
|
||||
}.body()
|
||||
|
||||
val p = resp.query?.pages?.firstOrNull()
|
||||
WikipediaDetail(pageId, p?.title ?: "", p?.extract ?: "")
|
||||
} catch (_: Throwable) {
|
||||
WikipediaDetail(pageId, "", "")
|
||||
}
|
||||
}
|
||||
6
shared/src/iosMain/kotlin/PlatformHttp.ios.kt
Normal file
6
shared/src/iosMain/kotlin/PlatformHttp.ios.kt
Normal file
@@ -0,0 +1,6 @@
|
||||
package app.klottr.net
|
||||
|
||||
import io.ktor.client.engine.HttpClientEngineFactory
|
||||
import io.ktor.client.engine.darwin.Darwin
|
||||
|
||||
internal actual fun platformEngine(): HttpClientEngineFactory<*> = Darwin
|
||||
Reference in New Issue
Block a user