Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class SpotifyPlaylistClient @Inject constructor(
}
}

private fun refreshTokenLocked(current: SpotifyStoredToken): SpotifyStoredToken {
private suspend fun refreshTokenLocked(current: SpotifyStoredToken): SpotifyStoredToken {
Comment thread
SayanthRock marked this conversation as resolved.
val refreshToken = current.refreshToken
?.takeIf(String::isNotBlank)
?: error("Spotify authorisation expired. Re-authorise Spotify to continue.")
Expand All @@ -168,7 +168,7 @@ class SpotifyPlaylistClient @Inject constructor(
.header("Accept", "application/json")
.build()

client.newCall(request).execute().use { response ->
client.newCall(request).await().use { response ->
val body = response.body?.string().orEmpty()
if (!response.isSuccessful) {
error("Spotify token refresh failed with HTTP ${response.code}. Re-authorise Spotify.")
Expand All @@ -189,14 +189,14 @@ class SpotifyPlaylistClient @Inject constructor(
}
}

private fun executePlaylistRequest(playlistId: String, accessToken: String): Response {
private suspend fun executePlaylistRequest(playlistId: String, accessToken: String): Response {
val request = Request.Builder()
.url("$API_BASE_URL/playlists/$playlistId")
.get()
.header("Authorization", "Bearer $accessToken")
.header("Accept", "application/json")
.build()
return client.newCall(request).execute()
return client.newCall(request).await()
}

private fun Response.toResult(playlistId: String): Result<SpotifyPlaylistPreview> = use { response ->
Expand Down Expand Up @@ -365,3 +365,24 @@ private data class SpotifyAlbum(
val name: String = "",
val images: List<SpotifyImage> = emptyList(),
)

private suspend fun okhttp3.Call.await(): okhttp3.Response = kotlinx.coroutines.suspendCancellableCoroutine { continuation ->
enqueue(object : okhttp3.Callback {
override fun onResponse(call: okhttp3.Call, response: okhttp3.Response) {
continuation.resumeWith(Result.success(response))
}
Comment thread
SayanthRock marked this conversation as resolved.

override fun onFailure(call: okhttp3.Call, e: java.io.IOException) {
if (continuation.isCancelled) return
continuation.resumeWith(Result.failure(e))
}
})

continuation.invokeOnCancellation {
try {
cancel()
} catch (t: Throwable) {
// Ignore
}
}
}
Loading