feature: SSE endpoints
Prototype (my demand):
@SSE
@GET("events")
suspend fun fetchEvents(): Flow<ServerSentEvent>
data class ServerSentEvent(
val id: String?,
val type: String?
val data: String,
)
Pending:
- Should it be migrated into base module? (e.g.
@SSEcan replace@GET) - What kind of result should be supported?
- Write in Kotlin or Java?
- Auto-reconnecting like JS? Or improve okhttp-sse module.
- How can we handle connection events like open/disconnected here?
- How to handle
id,type,datafor exampleInt?forid, enum fortypeand JSONdata?
java.util.concurrent.Flow (Java 9+) / java.util.stream.Stream (Java 8+) might be the integrated ones... But Stream is not designed for tasks like this.
These questions need to be answered before actual works.
closes https://github.com/square/retrofit/issues/1029
I think this can probably be entirely implemented in a call adapter with no changes to the core, but I'd have to try it.
We might have more than one adapter for SSE (for example RxJava & Kotlinx coroutines), so the basic definition like ServerSentEvent (or ServerSentEvent<T>) probably need to be in the core module.
Call adapters can delegate to each other. They do not need to be in the core.
I planned to add a callback style for core module but it requires some improvements on okhttp-sse, another PR for that will be opened later.
@JakeWharton Please review current work of this for further change/improvement/completion.
Current usage:
@Streaming
@GET
fun ktxFlow(): Flow<ServerSentEvent<String, String, String>>
@Streaming
@GET
fun jucFlow(): Flow.Publisher<ServerSentEvent<String, String, String>>
@Streaming
@GET
fun callback(): EventSource<String, String, String>
The fields of ServerSentEvent will be converted as a String ResponseBody.