mirror of
https://github.com/Redume/Kekkai.git
synced 2025-02-23 20:51:25 +03:00
сделана сериализация апишки ддг т.к там функция
This commit is contained in:
parent
7bcf2c897c
commit
2ea972e77d
3 changed files with 41 additions and 3 deletions
|
@ -3,5 +3,5 @@ package su.redume.core.network.api
|
|||
import su.redume.core.network.model.CurrencyDto
|
||||
|
||||
interface FiatCurrencyServer {
|
||||
suspend fun currency(): List<CurrencyDto>
|
||||
suspend fun currency(value: String, pair: String): List<CurrencyDto>
|
||||
}
|
|
@ -8,13 +8,14 @@ import io.ktor.client.plugins.defaultRequest
|
|||
import io.ktor.http.*
|
||||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
|
||||
import io.ktor.client.request.*
|
||||
import javax.inject.Inject
|
||||
import su.redume.core.network.api.FiatCurrencyServer
|
||||
import su.redume.core.network.model.CurrencyDto
|
||||
import su.redume.utils.js
|
||||
|
||||
class FiatCurrencyServerImpl @Inject constructor() : FiatCurrencyServer {
|
||||
class FiatCurrencyServerImpl : FiatCurrencyServer {
|
||||
private val httpClient = HttpClient {
|
||||
install(ContentNegotiation) {
|
||||
js()
|
||||
json(
|
||||
Json {
|
||||
ignoreUnknownKeys = true
|
||||
|
|
37
src/main/kotlin/su/redume/utils/JsConverter.kt
Normal file
37
src/main/kotlin/su/redume/utils/JsConverter.kt
Normal file
|
@ -0,0 +1,37 @@
|
|||
package su.redume.utils
|
||||
|
||||
import io.ktor.http.*
|
||||
import io.ktor.serialization.*
|
||||
import io.ktor.util.reflect.*
|
||||
import io.ktor.utils.io.*
|
||||
import io.ktor.utils.io.charsets.*
|
||||
import io.ktor.utils.io.jvm.javaio.*
|
||||
import kotlinx.serialization.InternalSerializationApi
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.serializer
|
||||
|
||||
class JsConverter : ContentConverter {
|
||||
|
||||
@OptIn(InternalSerializationApi::class)
|
||||
override suspend fun deserialize(
|
||||
charset: Charset,
|
||||
typeInfo: TypeInfo,
|
||||
content: ByteReadChannel
|
||||
): Any {
|
||||
val rawJs = content.toInputStream().reader(charset).readText()
|
||||
val json = rawJs
|
||||
.replace("[a-z_0-9]+\\(".toRegex(), "")
|
||||
.replace(");", "")
|
||||
val serializer = typeInfo.type.serializer()
|
||||
val jsonSerializer = Json {
|
||||
ignoreUnknownKeys = true
|
||||
}
|
||||
return jsonSerializer.decodeFromString(serializer, json)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun Configuration.js() {
|
||||
val converter = JsConverter()
|
||||
register(ContentType.Application.JavaScript, converter)
|
||||
}
|
Loading…
Add table
Reference in a new issue