|
|
|
@ -7,7 +7,6 @@ import com.github.kittinunf.fuel.Fuel
@@ -7,7 +7,6 @@ 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 |
|
|
|
|
import kotlinx.coroutines.Dispatchers.IO |
|
|
|
@ -92,37 +91,10 @@ class HttpUpstream<D : Any, R : OtterResponse<D>>(
@@ -92,37 +91,10 @@ class HttpUpstream<D : Any, R : OtterResponse<D>>(
|
|
|
|
|
val request = Fuel.get(mustNormalizeUrl(url)).apply { |
|
|
|
|
authorize(context) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
val (_, response, result) = request.awaitObjectResponseResult(GenericDeserializer<R>(type)) |
|
|
|
|
|
|
|
|
|
if (response.statusCode == 401) { |
|
|
|
|
return retryGet(url) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
val (_, _, result) = request.awaitObjectResponseResult(GenericDeserializer<R>(type)) |
|
|
|
|
result |
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
Result.error(FuelError.wrap(e)) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private suspend fun retryGet(url: String): Result<R, FuelError> { |
|
|
|
|
context?.let { |
|
|
|
|
return try { |
|
|
|
|
return if (http.refresh()) { |
|
|
|
|
val request = Fuel.get(mustNormalizeUrl(url)).apply { |
|
|
|
|
if (!Settings.isAnonymous()) { |
|
|
|
|
header("Authorization", "Bearer ${oAuth.state().accessToken}") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
request.awaitObjectResult(GenericDeserializer(type)) |
|
|
|
|
} else { |
|
|
|
|
Result.Failure(FuelError.wrap(RefreshError)) |
|
|
|
|
} |
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
Result.error(FuelError.wrap(e)) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
throw IllegalStateException("Illegal state: context is null") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|