Prevent long-running requests to make the app crash when user is logging out.

This commit is contained in:
Antoine POPINEAU 2020-06-21 14:16:30 +02:00
parent 490de25b05
commit a19e500f09
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
3 changed files with 37 additions and 25 deletions

View File

@ -29,9 +29,9 @@ class FavoritesRepository(override val context: Context?) : Repository<Track, Tr
track.downloaded = downloaded.contains(track.id)
track.bestUpload()?.let { upload ->
val url = mustNormalizeUrl(upload.listen_url)
track.cached = Otter.get().exoCache.isCached(url, 0, upload.duration * 1000L)
maybeNormalizeUrl(upload.listen_url)?.let { url ->
track.cached = Otter.get().exoCache.isCached(url, 0, upload.duration * 1000L)
}
}
track

View File

@ -65,32 +65,40 @@ class HttpUpstream<D : Any, R : FunkwhaleResponse<D>>(val behavior: Behavior, pr
}
suspend fun get(url: String): Result<R, FuelError> {
val request = Fuel.get(mustNormalizeUrl(url)).apply {
if (!Settings.isAnonymous()) {
header("Authorization", "Bearer ${Settings.getAccessToken()}")
}
}
val (_, response, result) = request.awaitObjectResponseResult(GenericDeserializer<R>(type))
if (response.statusCode == 401) {
return retryGet(url)
}
return result
}
private suspend fun retryGet(url: String): Result<R, FuelError> {
return if (HTTP.refresh()) {
return try {
val request = Fuel.get(mustNormalizeUrl(url)).apply {
if (!Settings.isAnonymous()) {
header("Authorization", "Bearer ${Settings.getAccessToken()}")
}
}
request.awaitObjectResult(GenericDeserializer(type))
} else {
Result.Failure(FuelError.wrap(RefreshError))
val (_, response, result) = request.awaitObjectResponseResult(GenericDeserializer<R>(type))
if (response.statusCode == 401) {
return retryGet(url)
}
result
} catch (e: Exception) {
Result.error(FuelError.wrap(e))
}
}
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()}")
}
}
request.awaitObjectResult(GenericDeserializer(type))
} else {
Result.Failure(FuelError.wrap(RefreshError))
}
} catch (e: Exception) {
Result.error(FuelError.wrap(e))
}
}
}

View File

@ -22,9 +22,13 @@ fun Any.log() {
}
fun maybeNormalizeUrl(rawUrl: String?): String? {
if (rawUrl == null || rawUrl.isEmpty()) return null
try {
if (rawUrl == null || rawUrl.isEmpty()) return null
return mustNormalizeUrl(rawUrl)
return mustNormalizeUrl(rawUrl)
} catch (e: Exception) {
return null
}
}
fun mustNormalizeUrl(rawUrl: String): String {