feat(shared): add KMP nearby.php client and delegate Android fetchNearby
This commit is contained in:
109
shared/src/commonMain/kotlin/app/klottr/net/KlottrApiShared.kt
Normal file
109
shared/src/commonMain/kotlin/app/klottr/net/KlottrApiShared.kt
Normal file
@@ -0,0 +1,109 @@
|
||||
package app.klottr.net
|
||||
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.client.request.post
|
||||
import io.ktor.client.request.setBody
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.HttpHeaders
|
||||
import io.ktor.http.contentType
|
||||
import io.ktor.serialization.kotlinx.json.json
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.json.*
|
||||
|
||||
/**
|
||||
* Minimal delad klient för Klottrs server-API.
|
||||
* Endast nearby.php i detta steg. Kastar inte utåt; mappas till NearbyResult.
|
||||
* OBS: Hemligheter injiceras via parametrar.
|
||||
*/
|
||||
object KlottrApiShared {
|
||||
|
||||
// Återanvänd Ktor-klient med plattforms-motor (OkHttp på Android, Darwin på iOS)
|
||||
private val client by lazy {
|
||||
HttpClient(platformEngine()) {
|
||||
install(ContentNegotiation) {
|
||||
json(
|
||||
Json {
|
||||
ignoreUnknownKeys = true
|
||||
isLenient = true
|
||||
explicitNulls = false
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Body till nearby.php (privat här; Androids Retrofit-variant har egen NearbyBody)
|
||||
@kotlinx.serialization.Serializable
|
||||
private data class NearbyBodyShared(
|
||||
val lat: Double,
|
||||
val lon: Double,
|
||||
@SerialName("r") val radiusMeters: Int,
|
||||
val since: Long = 0,
|
||||
val limit: Int = 200
|
||||
)
|
||||
|
||||
private fun ensureSlash(u: String): String = if (u.endsWith("/")) u else "$u/"
|
||||
|
||||
/**
|
||||
* KMP-variant av nearby.php. Returnerar alltid NearbyResult (inga throw utåt).
|
||||
* Parse görs manuellt via JsonObject → NoteDTO för att slippa ändra befintliga modeller.
|
||||
*/
|
||||
suspend fun nearby(
|
||||
baseUrl: String,
|
||||
apiKey: String,
|
||||
bearerToken: String?,
|
||||
lat: Double,
|
||||
lon: Double,
|
||||
radiusMeters: Int,
|
||||
sinceMs: Long,
|
||||
lastEtag: String?,
|
||||
limit: Int = 200
|
||||
): NearbyResult {
|
||||
val url = ensureSlash(baseUrl) + "nearby.php"
|
||||
val body = NearbyBodyShared(lat = lat, lon = lon, radiusMeters = radiusMeters, since = sinceMs, limit = limit)
|
||||
|
||||
val resp = try {
|
||||
client.post(url) {
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(body)
|
||||
header("X-API-Key", apiKey)
|
||||
if (bearerToken != null) header(HttpHeaders.Authorization, "Bearer $bearerToken")
|
||||
if (lastEtag != null) header(HttpHeaders.IfNoneMatch, lastEtag)
|
||||
}
|
||||
} catch (_: Throwable) {
|
||||
return NearbyResult(code = 599, items = emptyList(), etag = lastEtag)
|
||||
}
|
||||
|
||||
val code = resp.status.value
|
||||
val newEtag = resp.headers[HttpHeaders.ETag] ?: lastEtag
|
||||
|
||||
if (code != 200) {
|
||||
// 304 etc → tom lista men korrekt kod/etag tillbaka
|
||||
return NearbyResult(code = code, items = emptyList(), etag = newEtag)
|
||||
}
|
||||
|
||||
// Svar: JSON-array av objekt med fälten i/a/o/t/c/u (kompakt)
|
||||
val jsonArray = try {
|
||||
// Läs som JsonElement för manuell mapping till NoteDTO (utan @Serializable-krav)
|
||||
resp.body<JsonArray>()
|
||||
} catch (_: Throwable) {
|
||||
return NearbyResult(code = code, items = emptyList(), etag = newEtag)
|
||||
}
|
||||
|
||||
val items = jsonArray.mapNotNull { el ->
|
||||
val obj = el as? JsonObject ?: return@mapNotNull null
|
||||
val i = obj["i"]?.jsonPrimitive?.longOrNull ?: return@mapNotNull null
|
||||
val a = obj["a"]?.jsonPrimitive?.doubleOrNull ?: return@mapNotNull null
|
||||
val o = obj["o"]?.jsonPrimitive?.doubleOrNull ?: return@mapNotNull null
|
||||
val t = obj["t"]?.jsonPrimitive?.contentOrNull ?: return@mapNotNull null
|
||||
val c = obj["c"]?.jsonPrimitive?.longOrNull ?: return@mapNotNull null
|
||||
val u = obj["u"]?.jsonPrimitive?.contentOrNull ?: return@mapNotNull null
|
||||
NoteDTO(i = i, a = a, o = o, t = t, c = c, u = u)
|
||||
}
|
||||
|
||||
return NearbyResult(code = code, items = items, etag = newEtag)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user