commonMain.io.ipfs.kotlin.commands.Repo.kt Maven / Gradle / Ivy
package io.ipfs.kotlin.commands
import io.ipfs.kotlin.IPFSConnection
import io.ipfs.kotlin.model.Key
import io.ipfs.kotlin.model.KeyV2
import io.ktor.client.statement.*
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
class Repo(val ipfs: IPFSConnection) {
suspend fun gc() = ipfs.callCmd("repo/gc").let { listFromNDJson(it) }
suspend fun listFromNDJson(source: HttpResponse): List {
val jsonString = source.bodyAsText()
return try {
jsonString.parseNDJSON { it?.let { Json.decodeFromString(it).Key.hash } }
} catch (e: Throwable) {
jsonString.parseNDJSON { Json.decodeFromString(it!!).Key }
}
}
// http://ndjson.org
private fun String.parseNDJSON(convert: (input: String?) -> String?) = replace("\r", "").split("\n").asSequence().filter { !it.isEmpty() }.map {
convert(it)
}.filterNotNull().toList()
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy