Stefan Oltmann
07/23/2025, 4:29 PM@GET("plans/{id}/status")
fun getPlanStatusStreamById(@Path("id") id: String): Flow<BackupStatus>
The server sends the JSON as server-sent events.
I have some AI generated Flow Converter, but that doesn't work at all.
class SseConverterFactory(
private val json: Json
) : Converter.Factory {
override fun responseConverter(
typeData: TypeData,
ktorfit: Ktorfit
): Converter.ResponseConverter<HttpResponse, *>? {
if (typeData.typeInfo.type != Flow::class)
return null
println("Found Flow type: ${typeData.typeInfo}")
return object : Converter.ResponseConverter<HttpResponse, Flow<Any>> {
override fun convert(
getResponse: suspend () -> HttpResponse
): Flow<Any> {
return flow {
val response = getResponse()
println("SSE RESPONSE: $response")
val sseSession = response.body<ClientSSESession>()
println("SSE SESSION: $sseSession")
sseSession.incoming
.mapNotNull { event ->
event.data?.let { data ->
try {
json.decodeFromString<Any>(data)
} catch (e: Exception) {
println("Error decoding event: $e")
null
}
}
}
.collect { backupStatus ->
emit(backupStatus)
}
}
}
}
}
}
So what's the proper solution?JP Sugarbroad
07/23/2025, 7:38 PMStefan Oltmann
07/23/2025, 7:52 PMStefan Oltmann
07/23/2025, 7:53 PMJP Sugarbroad
07/23/2025, 7:54 PMKtorfit already has a converter for Flow
JP Sugarbroad
07/23/2025, 7:54 PMJP Sugarbroad
07/23/2025, 7:56 PMJP Sugarbroad
07/23/2025, 8:03 PMJP Sugarbroad
07/23/2025, 8:04 PM@Streaming
annotation: https://foso.github.io/Ktorfit/requests/#streamingJP Sugarbroad
07/23/2025, 8:05 PMStefan Oltmann
07/23/2025, 8:12 PMStefan Oltmann
07/23/2025, 8:13 PMI wonder if your custom converter is conflicting with the built-in oneI tried the provided FlowConverter, but that thing is not intended for this protocol.