2019-08-19 16:50:33 +02:00
|
|
|
package com.github.apognu.otter.repositories
|
|
|
|
|
|
|
|
import android.net.Uri
|
|
|
|
import com.github.apognu.otter.utils.*
|
|
|
|
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
|
2019-11-01 13:42:15 +01:00
|
|
|
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
|
|
|
|
|
2020-07-10 20:40:18 +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 {
|
2019-10-21 20:26:26 +02:00
|
|
|
Single, AtOnce, Progressive
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
|
2019-10-31 16:17:37 +01:00
|
|
|
override fun fetch(size: Int): Flow<Repository.Response<D>> = flow {
|
|
|
|
if (behavior == Behavior.Single && size != 0) return@flow
|
2019-10-21 20:26:26 +02:00
|
|
|
|
2019-10-29 23:41:44 +01:00
|
|
|
val page = ceil(size / AppContext.PAGE_SIZE.toDouble()).toInt() + 1
|
2019-08-19 16:50:33 +02:00
|
|
|
|
2020-06-13 19:34:57 +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())
|
2020-09-03 12:09:56 +02:00
|
|
|
.appendQueryParameter("scope", Settings.getScopes().joinToString(","))
|
2019-10-31 16:17:37 +01:00
|
|
|
.build()
|
|
|
|
.toString()
|
2019-08-19 16:50:33 +02:00
|
|
|
|
2020-06-13 19:34:57 +02:00
|
|
|
get(url).fold(
|
2019-10-31 16:17:37 +01:00
|
|
|
{ response ->
|
|
|
|
val data = response.getData()
|
2019-10-24 12:35:34 +02:00
|
|
|
|
2020-07-08 22:11:50 +02:00
|
|
|
when (behavior) {
|
2020-07-10 15:08:08 +02:00
|
|
|
Behavior.Single -> emit(Repository.Response(Repository.Origin.Network, data, page, false))
|
2020-07-08 22:11:50 +02:00
|
|
|
Behavior.Progressive -> emit(Repository.Response(Repository.Origin.Network, data, page, response.next != null))
|
2020-07-10 15:08:08 +02:00
|
|
|
|
2020-07-08 22:11:50 +02:00
|
|
|
else -> {
|
|
|
|
emit(Repository.Response(Repository.Origin.Network, data, page, response.next != null))
|
2019-10-29 23:41:44 +01:00
|
|
|
|
2020-07-10 15:08:08 +02:00
|
|
|
if (response.next != null) fetch(size + data.size).collect { emit(it) }
|
2020-07-08 22:11:50 +02:00
|
|
|
}
|
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)
|
2020-07-10 15:08:08 +02:00
|
|
|
else -> emit(Repository.Response(Repository.Origin.Network, listOf(), page, false))
|
2019-10-31 16:17:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}.flowOn(IO)
|
2019-08-19 16:50:33 +02:00
|
|
|
|
2020-07-10 20:40:18 +02:00
|
|
|
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> {
|
2020-06-21 14:16:30 +02:00
|
|
|
return try {
|
|
|
|
val request = Fuel.get(mustNormalizeUrl(url)).apply {
|
|
|
|
if (!Settings.isAnonymous()) {
|
|
|
|
header("Authorization", "Bearer ${Settings.getAccessToken()}")
|
|
|
|
}
|
2019-11-25 23:14:16 +01:00
|
|
|
}
|
2019-08-19 16:50:33 +02:00
|
|
|
|
2020-06-21 14:16:30 +02:00
|
|
|
val (_, response, result) = request.awaitObjectResponseResult(GenericDeserializer<R>(type))
|
2019-08-19 16:50:33 +02:00
|
|
|
|
2020-06-21 14:16:30 +02:00
|
|
|
if (response.statusCode == 401) {
|
|
|
|
return retryGet(url)
|
|
|
|
}
|
2019-08-19 16:50:33 +02:00
|
|
|
|
2020-06-21 14:16:30 +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> {
|
2020-06-21 14:16:30 +02:00
|
|
|
return try {
|
|
|
|
return if (HTTP.refresh()) {
|
|
|
|
val request = Fuel.get(mustNormalizeUrl(url)).apply {
|
|
|
|
if (!Settings.isAnonymous()) {
|
|
|
|
header("Authorization", "Bearer ${Settings.getAccessToken()}")
|
|
|
|
}
|
2019-11-25 23:14:16 +01:00
|
|
|
}
|
2019-08-19 16:50:33 +02:00
|
|
|
|
2020-06-21 14:16:30 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|