feat(shared): add KMP create.php (text & doc) and delegate Android createNote*

This commit is contained in:
2025-09-06 16:48:30 +02:00
parent b3dcb91922
commit 477b1cb022
2 changed files with 130 additions and 14 deletions

View File

@@ -52,6 +52,30 @@ object KlottrApiShared {
val lon: Double
)
@kotlinx.serialization.Serializable
private data class CreateBodyShared(
val lat: Double,
val lon: Double,
val text: String,
val createdAt: Long,
val userId: String? = null,
val clientRequestId: String? = null
)
@kotlinx.serialization.Serializable
private data class CreateDocBodyShared(
val lat: Double,
val lon: Double,
val title: String,
val body: String,
val imageId: Long? = null,
val radiusM: Int = 20,
val createdAt: Long,
val userId: String? = null,
val clientRequestId: String? = null
)
private fun ensureSlash(u: String): String = if (u.endsWith("/")) u else "$u/"
/**
@@ -190,5 +214,100 @@ object KlottrApiShared {
userId = userId
)
}
/**
* KMP-variant av create.php (textbaserad). Returnerar true om { ok: true }.
*/
suspend fun create(
baseUrl: String,
apiKey: String,
bearerToken: String?,
lat: Double,
lon: Double,
text: String,
createdAt: Long,
userId: String?,
clientRequestId: String?
): Boolean {
val url = ensureSlash(baseUrl) + "create.php"
val body = CreateBodyShared(
lat = lat, lon = lon, text = text, createdAt = createdAt,
userId = userId, clientRequestId = clientRequestId
)
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 (clientRequestId != null) header("X-Idempotency-Key", clientRequestId)
}
} catch (_: Throwable) {
return false
}
if (resp.status.value != 200) return false
val obj = try { resp.body<JsonObject>() } catch (_: Throwable) { return false }
val okPrim = obj["ok"]?.jsonPrimitive
return when {
okPrim == null -> true
okPrim.isString -> okPrim.content.equals("true", true) || okPrim.content == "1" || okPrim.content.equals("ok", true)
okPrim.booleanOrNull != null -> okPrim.boolean
okPrim.longOrNull != null -> okPrim.long != 0L
else -> true
}
}
/**
* KMP-variant av create.php (dokumentvarianten). Returnerar true om { ok: true }.
*/
suspend fun createDoc(
baseUrl: String,
apiKey: String,
bearerToken: String?,
lat: Double,
lon: Double,
title: String,
body: String,
imageId: Long?,
radiusM: Int,
createdAt: Long,
userId: String?,
clientRequestId: String?
): Boolean {
val url = ensureSlash(baseUrl) + "create.php"
val payload = CreateDocBodyShared(
lat = lat, lon = lon,
title = title, body = body,
imageId = imageId, radiusM = radiusM,
createdAt = createdAt,
userId = userId,
clientRequestId = clientRequestId
)
val resp = try {
client.post(url) {
contentType(ContentType.Application.Json)
setBody(payload)
header("X-API-Key", apiKey)
if (bearerToken != null) header(HttpHeaders.Authorization, "Bearer $bearerToken")
if (clientRequestId != null) header("X-Idempotency-Key", clientRequestId)
}
} catch (_: Throwable) {
return false
}
if (resp.status.value != 200) return false
val obj = try { resp.body<JsonObject>() } catch (_: Throwable) { return false }
val okPrim = obj["ok"]?.jsonPrimitive
return when {
okPrim == null -> true
okPrim.isString -> okPrim.content.equals("true", true) || okPrim.content == "1" || okPrim.content.equals("ok", true)
okPrim.booleanOrNull != null -> okPrim.boolean
okPrim.longOrNull != null -> okPrim.long != 0L
else -> true
}
}
}