feat(shared): add KMP nearby.php client and delegate Android fetchNearby
This commit is contained in:
@@ -15,6 +15,7 @@ import retrofit2.http.*
|
|||||||
import retrofit2.http.Url
|
import retrofit2.http.Url
|
||||||
import app.klottr.util.Auth
|
import app.klottr.util.Auth
|
||||||
import app.klottr.BuildConfig
|
import app.klottr.BuildConfig
|
||||||
|
import app.klottr.net.KlottrApiShared
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
@@ -204,10 +205,9 @@ object Api {
|
|||||||
|
|
||||||
val svc: KlottrApi = retrofit.create(KlottrApi::class.java)
|
val svc: KlottrApi = retrofit.create(KlottrApi::class.java)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hämtar nearby med stöd för ETag och "since".
|
* Hämtar nearby via delad KMP-klient med stöd för ETag och "since".
|
||||||
* Kasta inte exception – returnera bara kod + lista + ny etag.
|
* Kastar inte exception – returnerar bara kod + lista + ny etag.
|
||||||
*/
|
*/
|
||||||
suspend fun fetchNearby(
|
suspend fun fetchNearby(
|
||||||
lat: Double,
|
lat: Double,
|
||||||
@@ -217,38 +217,21 @@ suspend fun fetchNearby(
|
|||||||
lastEtag: String? = null,
|
lastEtag: String? = null,
|
||||||
limit: Int = 200
|
limit: Int = 200
|
||||||
): NearbyResult {
|
): NearbyResult {
|
||||||
val resp = try {
|
// Delegera till shared, injicera runtime-konfig härifrån (inga hemligheter i shared)
|
||||||
Api.svc.nearby(
|
return KlottrApiShared.nearby(
|
||||||
body = NearbyBody(
|
baseUrl = ApiConfig.SERVER_API_BASE_URL,
|
||||||
|
apiKey = ApiConfig.SERVER_API_KEY,
|
||||||
|
bearerToken = app.klottr.util.Auth.idToken,
|
||||||
lat = lat,
|
lat = lat,
|
||||||
lon = lon,
|
lon = lon,
|
||||||
radiusMeters = radiusMeters,
|
radiusMeters = radiusMeters,
|
||||||
since = sinceMs,
|
sinceMs = sinceMs,
|
||||||
|
lastEtag = lastEtag,
|
||||||
limit = limit
|
limit = limit
|
||||||
),
|
|
||||||
etag = lastEtag
|
|
||||||
)
|
|
||||||
} catch (t: Throwable) {
|
|
||||||
return NearbyResult(code = 599, items = emptyList(), etag = lastEtag)
|
|
||||||
}
|
|
||||||
|
|
||||||
val code = resp.code()
|
|
||||||
val newEtag = resp.headers()["ETag"] ?: lastEtag
|
|
||||||
val body = if (resp.isSuccessful) resp.body().orEmpty() else emptyList()
|
|
||||||
|
|
||||||
// 🔧 Tydligare loggning vid felkoder – annars ser det bara ut som tom lista
|
|
||||||
if (!resp.isSuccessful) {
|
|
||||||
val err = resp.errorBody()?.string()
|
|
||||||
app.klottr.util.AppLog.d { "nearby.php FAIL code=$code url=${ApiConfig.SERVER_API_BASE_URL}nearby.php err=$err" }
|
|
||||||
}
|
|
||||||
|
|
||||||
return NearbyResult(
|
|
||||||
code = code,
|
|
||||||
items = body,
|
|
||||||
etag = newEtag
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Laddar upp en bild (Uri) till upload_image.php.
|
* Laddar upp en bild (Uri) till upload_image.php.
|
||||||
* Returnerar image_id eller null om något gick fel.
|
* Returnerar image_id eller null om något gick fel.
|
||||||
|
|||||||
40
app/src/main/java/app/klottr/net/SharedFacade.kt
Normal file
40
app/src/main/java/app/klottr/net/SharedFacade.kt
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package app.klottr.net
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enkel facade för iOS/Swift (och ev. annan plattform) att anropa delad logik.
|
||||||
|
* Notera: Inga hemligheter ligger här. iOS-appen skickar in nycklar/owners som parametrar.
|
||||||
|
*/
|
||||||
|
class SharedFacade {
|
||||||
|
|
||||||
|
// --- Wikipedia ---
|
||||||
|
suspend fun wikipediaNearby(lat: Double, lon: Double, radiusMeters: Int): List<WikipediaPage> =
|
||||||
|
WikipediaShared.getNearby(lat, lon, radiusMeters)
|
||||||
|
|
||||||
|
suspend fun wikipediaExtract(pageId: Long): WikipediaDetail =
|
||||||
|
WikipediaShared.getExtract(pageId)
|
||||||
|
|
||||||
|
// --- Fornsök ---
|
||||||
|
suspend fun fornsokNearby(lat: Double, lon: Double, radiusMeters: Int): List<FornsokItem> =
|
||||||
|
FornsokShared.getNearby(lat, lon, radiusMeters)
|
||||||
|
|
||||||
|
// --- DigitaltMuseum ---
|
||||||
|
suspend fun digitaltMuseumNearby(
|
||||||
|
apiKey: String,
|
||||||
|
ownersCsv: String?,
|
||||||
|
lat: Double,
|
||||||
|
lon: Double,
|
||||||
|
radiusMeters: Int
|
||||||
|
): List<DigitaltMuseumItem> {
|
||||||
|
val owners = ownersCsv
|
||||||
|
?.split(',', ';', ' ')
|
||||||
|
?.map { it.trim() }
|
||||||
|
?.filter { it.isNotEmpty() }
|
||||||
|
return DigitaltMuseumShared.getNearby(
|
||||||
|
apiKey = apiKey,
|
||||||
|
owners = owners,
|
||||||
|
lat = lat,
|
||||||
|
lon = lon,
|
||||||
|
radiusMeters = radiusMeters
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
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