mirror of
https://github.com/Suwayomi/TachideskJUI.git
synced 2026-01-29 15:04:08 +01:00
Update dependencies
This commit is contained in:
@@ -26,10 +26,10 @@ import me.tatarka.inject.annotations.Provides
|
||||
interface DataComponent {
|
||||
|
||||
@Provides
|
||||
fun ktorfit(http: Http, serverPreferences: ServerPreferences, flowIOResponseConverter: FlowIOResponseConverter) = Ktorfit
|
||||
fun ktorfit(http: Http, serverPreferences: ServerPreferences) = Ktorfit
|
||||
.Builder()
|
||||
.httpClient(http)
|
||||
.responseConverter(flowIOResponseConverter)
|
||||
.converterFactories(FlowConverterFactory())
|
||||
.baseUrl(serverPreferences.serverUrl().get().toString().addSuffix('/'))
|
||||
.build()
|
||||
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
package ca.gosyer.jui.data
|
||||
|
||||
import ca.gosyer.jui.core.lang.IO
|
||||
import de.jensklingenberg.ktorfit.Ktorfit
|
||||
import de.jensklingenberg.ktorfit.converter.Converter
|
||||
import de.jensklingenberg.ktorfit.internal.TypeData
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.statement.HttpResponse
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.flowOn
|
||||
|
||||
class FlowConverterFactory : Converter.Factory {
|
||||
|
||||
private class FlowResponseConverter(
|
||||
val typeData: TypeData,
|
||||
val ktorfit: Ktorfit
|
||||
) : Converter.ResponseConverter<HttpResponse, Flow<Any?>> {
|
||||
|
||||
override fun convert(getResponse: suspend () -> HttpResponse): Flow<Any?> {
|
||||
return flow {
|
||||
val response = getResponse()
|
||||
|
||||
val convertedBody = ktorfit.nextSuspendResponseConverter(
|
||||
null,
|
||||
typeData.typeArgs.first()
|
||||
)?.convert(response)
|
||||
?: response.body(typeData.typeArgs.first().typeInfo)
|
||||
emit(convertedBody)
|
||||
}.flowOn(Dispatchers.IO)
|
||||
}
|
||||
}
|
||||
|
||||
override fun responseConverter(
|
||||
typeData: TypeData,
|
||||
ktorfit: Ktorfit
|
||||
): Converter.ResponseConverter<HttpResponse, *>? {
|
||||
if (typeData.typeInfo.type == Flow::class) {
|
||||
return FlowResponseConverter(typeData, ktorfit)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
override fun suspendResponseConverter(
|
||||
typeData: TypeData,
|
||||
ktorfit: Ktorfit
|
||||
): Converter.SuspendResponseConverter<HttpResponse, *>? {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
package ca.gosyer.jui.data
|
||||
|
||||
import ca.gosyer.jui.core.lang.IO
|
||||
import de.jensklingenberg.ktorfit.Ktorfit
|
||||
import de.jensklingenberg.ktorfit.converter.request.ResponseConverter
|
||||
import de.jensklingenberg.ktorfit.internal.TypeData
|
||||
import io.ktor.client.statement.HttpResponse
|
||||
import io.ktor.util.reflect.TypeInfo
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.flowOn
|
||||
import kotlinx.serialization.json.Json
|
||||
import me.tatarka.inject.annotations.Inject
|
||||
|
||||
class FlowIOResponseConverter @Inject constructor(private val json: Json) : ResponseConverter {
|
||||
|
||||
override fun supportedType(typeData: TypeData, isSuspend: Boolean): Boolean {
|
||||
return typeData.qualifiedName == "kotlinx.coroutines.flow.Flow"
|
||||
}
|
||||
|
||||
override fun <RequestType : Any?> wrapResponse(
|
||||
typeData: TypeData,
|
||||
requestFunction: suspend () -> Pair<TypeInfo, HttpResponse?>,
|
||||
ktorfit: Ktorfit,
|
||||
): Any {
|
||||
return flow {
|
||||
try {
|
||||
val (info, response) = requestFunction()
|
||||
if (info.type == HttpResponse::class) {
|
||||
emit(response!!)
|
||||
} else {
|
||||
emit(decodeType(response!!, info, json))
|
||||
}
|
||||
} catch (exception: Exception) {
|
||||
throw exception
|
||||
}
|
||||
}.flowOn(Dispatchers.IO)
|
||||
}
|
||||
}
|
||||
|
||||
expect suspend fun decodeType(response: HttpResponse, typeInfo: TypeInfo, json: Json): Any
|
||||
@@ -1,17 +0,0 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
package ca.gosyer.jui.data
|
||||
|
||||
import io.ktor.client.statement.HttpResponse
|
||||
import io.ktor.client.statement.bodyAsText
|
||||
import io.ktor.util.reflect.TypeInfo
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.serializer
|
||||
|
||||
actual suspend fun decodeType(response: HttpResponse, typeInfo: TypeInfo, json: Json): Any {
|
||||
return json.decodeFromString(serializer(typeInfo.kotlinType!!), response.bodyAsText())!!
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
package ca.gosyer.jui.data
|
||||
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.statement.HttpResponse
|
||||
import io.ktor.util.reflect.TypeInfo
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
actual suspend fun decodeType(response: HttpResponse, typeInfo: TypeInfo, json: Json): Any {
|
||||
return response.body(typeInfo)
|
||||
}
|
||||
Reference in New Issue
Block a user