-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
73 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...src/commonMain/kotlin/domain/mediasource/subscription/MediaSourceSubscriptionRequester.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (C) 2024 OpenAni and contributors. | ||
* | ||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. | ||
* Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link. | ||
* | ||
* https://github.com/open-ani/ani/blob/main/LICENSE | ||
*/ | ||
|
||
package me.him188.ani.app.domain.mediasource.subscription | ||
|
||
import io.ktor.client.HttpClient | ||
import io.ktor.client.request.get | ||
import io.ktor.client.request.parameter | ||
import io.ktor.client.statement.HttpResponse | ||
import io.ktor.client.statement.bodyAsChannel | ||
import io.ktor.utils.io.CancellationException | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.serialization.json.io.decodeFromSource | ||
import me.him188.ani.app.data.models.ApiResponse | ||
import me.him188.ani.app.data.models.map | ||
import me.him188.ani.app.data.models.runApiRequest | ||
import me.him188.ani.app.domain.mediasource.codec.MediaSourceCodecManager | ||
import me.him188.ani.app.platform.currentAniBuildConfig | ||
import me.him188.ani.utils.coroutines.withExceptionCollector | ||
import me.him188.ani.utils.ktor.toSource | ||
|
||
class MediaSourceSubscriptionRequester( | ||
private val client: Flow<HttpClient> | ||
) { | ||
/** | ||
* 执行网络请求, 下载新订阅数据. 遇到错误时将会返回 [ApiResponse.failure] | ||
*/ | ||
suspend fun request( | ||
subscription: MediaSourceSubscription, | ||
): ApiResponse<SubscriptionUpdateData> { | ||
val clientInstance = client.first() | ||
|
||
suspend fun HttpResponse.decode() = bodyAsChannel().toSource().use { | ||
MediaSourceCodecManager.Companion.json.decodeFromSource( | ||
SubscriptionUpdateData.serializer(), | ||
it, | ||
) | ||
} | ||
|
||
withExceptionCollector { | ||
// 首先直连 | ||
try { | ||
return ApiResponse.success(clientInstance.get(subscription.url).decode()) | ||
} catch (e: CancellationException) { | ||
throw e | ||
} catch (e: Exception) { | ||
collect(e) // continue | ||
} | ||
|
||
// 失败则尝试代理服务 | ||
return runApiRequest { | ||
clientInstance.get(currentAniBuildConfig.aniAuthServerUrl + "/v1/subs/proxy") { | ||
parameter("url", subscription.url) | ||
} | ||
}.map { | ||
it.decode() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters