fix(app): replace leftover Retrofit createNote with KMP delegate
This commit is contained in:
@@ -56,14 +56,6 @@ object ApiConfig {
|
||||
if (u.endsWith("/")) u else "$u/"
|
||||
}
|
||||
|
||||
/** Svar från upload_image.php */
|
||||
data class UploadImageResp(
|
||||
val ok: Any? = null,
|
||||
@Json(name = "image_id") val imageId: Long? = null,
|
||||
val width: Int? = null,
|
||||
val height: Int? = null
|
||||
)
|
||||
|
||||
// --- Användarinfo från me.php ---
|
||||
data class MeResp(
|
||||
val ok: Boolean? = null,
|
||||
@@ -74,140 +66,12 @@ data class MeResp(
|
||||
@Json(name = "last_login") val lastLogin: String? = null,
|
||||
@Json(name = "is_suspended") val isSuspended: Int? = 0
|
||||
)
|
||||
// Body till nearby.php (POST)
|
||||
data class NearbyBody(
|
||||
val lat: Double,
|
||||
val lon: Double,
|
||||
@Json(name = "r") val radiusMeters: Int,
|
||||
val since: Long = 0,
|
||||
val limit: Int = 200
|
||||
)
|
||||
|
||||
/** Dina endpoints – samma namn/banor som i v0.0.1 */
|
||||
interface KlottrApi {
|
||||
|
||||
@Multipart
|
||||
@POST("upload_image.php")
|
||||
suspend fun uploadImage(
|
||||
@Part image: MultipartBody.Part,
|
||||
@Part("lat") lat: RequestBody,
|
||||
@Part("lon") lon: RequestBody
|
||||
): RResponse<UploadImageResp>
|
||||
|
||||
/** Ny variant till samma create.php när vi skickar dokument */
|
||||
|
||||
@POST("create.php")
|
||||
suspend fun createDoc(
|
||||
@Body body: CreateDocBody,
|
||||
@Header("X-Idempotency-Key") idempotencyKey: String? = null
|
||||
): RResponse<Map<String, Any>>
|
||||
|
||||
/** Hämta full text + bild‐meta när användaren är på plats */
|
||||
|
||||
@POST("get_note.php")
|
||||
suspend fun getNote(@Body body: NoteDetailBody): RResponse<NoteDetailDTO>
|
||||
|
||||
/** Hämtar en bildström via POST (form-url-encoded) så PHP får fält i $_POST. */
|
||||
@FormUrlEncoded
|
||||
@POST("get_image.php")
|
||||
@Streaming
|
||||
suspend fun getImagePost(
|
||||
@Field("id") id: Long,
|
||||
@Field("note") noteId: Long,
|
||||
@Field("lat") lat: Double,
|
||||
@Field("lon") lon: Double
|
||||
): RResponse<ResponseBody>
|
||||
|
||||
@POST("create.php")
|
||||
suspend fun create(
|
||||
@Body body: CreateBody,
|
||||
@Header("X-Idempotency-Key") idempotencyKey: String? = null
|
||||
): RResponse<Map<String, Any>>
|
||||
|
||||
/** Hämtar närliggande klotter (via POST-body i stället för GET). */
|
||||
@POST("nearby.php")
|
||||
suspend fun nearby(
|
||||
@Body body: NearbyBody,
|
||||
@Header("If-None-Match") etag: String? = null
|
||||
): RResponse<List<NoteDTO>>
|
||||
|
||||
// Hämta mig (kräver Bearer, kör POST utan body)
|
||||
@POST("me.php")
|
||||
suspend fun me(): RResponse<MeResp>
|
||||
|
||||
// Kontrollera tillgänglighet/format (form-POST)
|
||||
@FormUrlEncoded
|
||||
@POST("username_check.php")
|
||||
suspend fun usernameCheck(
|
||||
@Field("username") username: String
|
||||
): RResponse<UsernameCheckResp>
|
||||
|
||||
// Sätta/uppdatera användarnamn (kräver Bearer)
|
||||
@FormUrlEncoded
|
||||
@POST("username_set.php")
|
||||
suspend fun usernameSet(
|
||||
@Field("username") username: String
|
||||
): RResponse<UsernameSetResp>
|
||||
|
||||
// Soft delete av en not (kräver Bearer). POST JSON: { "id": 123 }
|
||||
@POST("delete_note.php")
|
||||
suspend fun deleteNote(
|
||||
@Body body: Map<String, Long>
|
||||
): RResponse<Map<String, Any>>
|
||||
|
||||
}
|
||||
|
||||
/** Lägger API‑nyckel + ev. Google ID‑token (Bearer) på varje request. */
|
||||
private class ApiKeyInterceptor : Interceptor {
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
val b = chain.request().newBuilder()
|
||||
.header("X-API-Key", ApiConfig.SERVER_API_KEY)
|
||||
|
||||
// Lägg med Google ID-token om vi har en (för server‑side verifikation)
|
||||
Auth.idToken?.let { token ->
|
||||
b.header("Authorization", "Bearer $token")
|
||||
}
|
||||
return chain.proceed(b.build())
|
||||
}
|
||||
}
|
||||
|
||||
/** Retrofit/OkHttp build – med timeouts och logg i debug. */
|
||||
object Api {
|
||||
|
||||
@Volatile var lastUploadError: String? = null
|
||||
|
||||
private val moshi: Moshi by lazy {
|
||||
Moshi.Builder()
|
||||
.addLast(KotlinJsonAdapterFactory())
|
||||
.build()
|
||||
}
|
||||
|
||||
private val http: OkHttpClient by lazy {
|
||||
OkHttpClient.Builder()
|
||||
.addInterceptor(ApiKeyInterceptor())
|
||||
.connectTimeout(10, TimeUnit.SECONDS)
|
||||
.readTimeout(10, TimeUnit.SECONDS)
|
||||
.writeTimeout(10, TimeUnit.SECONDS)
|
||||
.also { b ->
|
||||
if (BuildConfig.KLLOG_HTTP_DEBUG) {
|
||||
val log = HttpLoggingInterceptor()
|
||||
log.level = HttpLoggingInterceptor.Level.BODY
|
||||
b.addInterceptor(log)
|
||||
}
|
||||
}
|
||||
.build()
|
||||
}
|
||||
|
||||
private val retrofit: Retrofit by lazy {
|
||||
Retrofit.Builder()
|
||||
.baseUrl(ApiConfig.SERVER_API_BASE_URL)
|
||||
.client(http)
|
||||
.addConverterFactory(MoshiConverterFactory.create(moshi))
|
||||
.build()
|
||||
}
|
||||
|
||||
val svc: KlottrApi = retrofit.create(KlottrApi::class.java)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Hämtar nearby via delad KMP-klient med stöd för ETag och "since".
|
||||
* Kastar inte exception – returnerar bara kod + lista + ny etag.
|
||||
@@ -358,21 +222,20 @@ suspend fun createNote(
|
||||
createdAt: Long,
|
||||
clientRequestId: String?
|
||||
): Boolean {
|
||||
if (Auth.idToken == null || Auth.userId == null) {
|
||||
if (Auth.idToken == null || Auth.userId == null) {
|
||||
return false
|
||||
}
|
||||
|
||||
val body = CreateBody(
|
||||
lat = lat, lon = lon, text = text, createdAt = createdAt,
|
||||
return KlottrApiShared.create(
|
||||
baseUrl = ApiConfig.SERVER_API_BASE_URL,
|
||||
apiKey = ApiConfig.SERVER_API_KEY,
|
||||
bearerToken = Auth.idToken,
|
||||
lat = lat,
|
||||
lon = lon,
|
||||
text = text,
|
||||
createdAt = createdAt,
|
||||
userId = Auth.userId,
|
||||
clientRequestId = clientRequestId
|
||||
)
|
||||
val resp = try {
|
||||
Api.svc.create(body, idempotencyKey = clientRequestId)
|
||||
} catch (_: Throwable) {
|
||||
return false
|
||||
}
|
||||
return resp.isSuccessful && (resp.body()?.get("ok") == true)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user