feat: fetch beat metadata from api

This commit is contained in:
2026-05-31 11:41:06 +02:00
parent 633afd115d
commit f7c5b932e9
7 changed files with 176 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
package at.lockstep.player.data
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
@@ -7,10 +8,56 @@ import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import java.io.IOException
sealed class MetadataFetchResult {
data class Found(val collection: JSONObject) : MetadataFetchResult()
data object NotFound : MetadataFetchResult()
}
class MetadataSyncClient(
private val httpClient: OkHttpClient,
private val baseUrl: String,
) {
@Throws(IOException::class)
fun fetchMetadata(
accessToken: String,
trackId: String,
type: String,
): MetadataFetchResult {
val url =
baseUrl.toHttpUrl()
.newBuilder()
.addPathSegments("metadata")
.addQueryParameter("trackId", trackId)
.addQueryParameter("type", type)
.build()
val request =
Request.Builder()
.url(url)
.addHeader("Authorization", "Bearer $accessToken")
.get()
.build()
httpClient.newCall(request).execute().use { response ->
if (response.code == 404) {
return MetadataFetchResult.NotFound
}
val bodyText = response.body?.string().orEmpty()
if (!response.isSuccessful) {
val message =
runCatching { JSONObject(bodyText).optString("error") }
.getOrNull()
?.takeIf { it.isNotBlank() }
?: response.message
throw IOException(message)
}
val collection =
runCatching { JSONObject(bodyText).getJSONObject("collection") }
.getOrNull()
?: throw IOException("Missing collection in response")
return MetadataFetchResult.Found(collection)
}
}
@Throws(IOException::class)
fun uploadCollection(
accessToken: String,