chatapp/app/SseCallAdapterFactory.kt
Minecon724 48f721e756
Response streaming
Also had to rework message displaying a bit, to accommodate
2025-06-21 15:41:04 +02:00

26 lines
No EOL
847 B
Kotlin

class SseCallAdapterFactory(
private val client: OkHttpClient,
private val moshi: Moshi // Or Gson
) : CallAdapter.Factory() {
override fun get(
returnType: Type,
annotations: Array<out Annotation>,
retrofit: Retrofit
): CallAdapter<*, *>? {
// Ensure the return type is a Flow
if (getRawType(returnType) != Flow::class.java) {
return null
}
// Ensure the Flow's generic type is SseEvent
val flowType = getParameterUpperBound(0, returnType as ParameterizedType)
if (getRawType(flowType) != SseEvent::class.java) {
return null
}
// Get the generic type of SseEvent<T>
val eventType = getParameterUpperBound(0, flowType as ParameterizedType)
return SseCallAdapter<Any>(client, moshi, eventType)
}
}