funkwhale-app-android/app/src/main/java/audio/funkwhale/ffa/repositories/HttpUpstream.kt

117 lines
3.5 KiB
Kotlin
Raw Normal View History

package audio.funkwhale.ffa.repositories
2019-08-19 16:50:33 +02:00
import android.net.Uri
import audio.funkwhale.ffa.utils.*
2019-08-19 16:50:33 +02:00
import com.github.kittinunf.fuel.Fuel
import com.github.kittinunf.fuel.core.FuelError
import com.github.kittinunf.fuel.core.ResponseDeserializable
import com.github.kittinunf.fuel.coroutines.awaitObjectResponseResult
import com.github.kittinunf.fuel.coroutines.awaitObjectResult
import com.github.kittinunf.result.Result
import com.google.gson.Gson
2019-10-31 16:17:37 +01:00
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
2019-10-31 16:17:37 +01:00
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
2019-08-19 16:50:33 +02:00
import java.io.Reader
import java.lang.reflect.Type
import kotlin.math.ceil
2021-07-22 14:45:04 +02:00
class HttpUpstream<D : Any, R : OtterResponse<D>>(
val behavior: Behavior,
private val url: String,
private val type: Type
) : Upstream<D> {
2019-08-19 16:50:33 +02:00
enum class Behavior {
Single, AtOnce, Progressive
2019-08-19 16:50:33 +02:00
}
2021-06-29 09:37:36 +02:00
override fun fetch(size: Int): Flow<Repository.Response<D>> = flow<Repository.Response<D>> {
2019-10-31 16:17:37 +01:00
if (behavior == Behavior.Single && size != 0) return@flow
val page = ceil(size / AppContext.PAGE_SIZE.toDouble()).toInt() + 1
2019-08-19 16:50:33 +02:00
val url =
2019-10-31 16:17:37 +01:00
Uri.parse(url)
.buildUpon()
.appendQueryParameter("page_size", AppContext.PAGE_SIZE.toString())
.appendQueryParameter("page", page.toString())
.appendQueryParameter("scope", Settings.getScopes().joinToString(","))
2019-10-31 16:17:37 +01:00
.build()
.toString()
2019-08-19 16:50:33 +02:00
get(url).fold(
2019-10-31 16:17:37 +01:00
{ response ->
val data = response.getData()
when (behavior) {
2021-07-22 14:45:04 +02:00
Behavior.Single -> emit(networkResponse(data, page, false))
Behavior.Progressive -> emit(networkResponse(data, page, response.next != null))
else -> {
2021-07-22 14:45:04 +02:00
emit(networkResponse(data, page, response.next != null))
if (response.next != null) fetch(size + data.size).collect { emit(it) }
}
2019-08-19 16:50:33 +02:00
}
2019-10-31 16:17:37 +01:00
},
{ error ->
when (error.exception) {
is RefreshError -> EventBus.send(Event.LogOut)
2021-07-22 14:45:04 +02:00
else -> emit(networkResponse(listOf(), page, false))
2019-10-31 16:17:37 +01:00
}
}
)
}.flowOn(IO)
2019-08-19 16:50:33 +02:00
2021-07-22 14:45:04 +02:00
private fun networkResponse(data: List<D>, page: Int, hasMore: Boolean) = Repository.Response(
Repository.Origin.Network,
data,
page,
hasMore
)
class GenericDeserializer<T : OtterResponse<*>>(val type: Type) : ResponseDeserializable<T> {
2019-08-19 16:50:33 +02:00
override fun deserialize(reader: Reader): T? {
return Gson().fromJson(reader, type)
}
}
suspend fun get(url: String): Result<R, FuelError> {
return try {
val request = Fuel.get(mustNormalizeUrl(url)).apply {
if (!Settings.isAnonymous()) {
header("Authorization", "Bearer ${Settings.getAccessToken()}")
}
}
2019-08-19 16:50:33 +02:00
val (_, response, result) = request.awaitObjectResponseResult(GenericDeserializer<R>(type))
2019-08-19 16:50:33 +02:00
if (response.statusCode == 401) {
return retryGet(url)
}
2019-08-19 16:50:33 +02:00
result
} catch (e: Exception) {
Result.error(FuelError.wrap(e))
}
2019-08-19 16:50:33 +02:00
}
private suspend fun retryGet(url: String): Result<R, FuelError> {
return try {
return if (HTTP.refresh()) {
val request = Fuel.get(mustNormalizeUrl(url)).apply {
if (!Settings.isAnonymous()) {
header("Authorization", "Bearer ${Settings.getAccessToken()}")
}
}
2019-08-19 16:50:33 +02:00
request.awaitObjectResult(GenericDeserializer(type))
} else {
Result.Failure(FuelError.wrap(RefreshError))
}
} catch (e: Exception) {
Result.error(FuelError.wrap(e))
2019-08-19 16:50:33 +02:00
}
}
2021-07-02 13:55:49 +02:00
}