Skip to content

How to implement ndjson Response converter #756

Answered by ag2s20150909
ag2s20150909 asked this question in Q&A
Discussion options

You must be logged in to vote

The current solution is to write it manually, it would be nice to generate it automatically.

interface TestService{

    @Streaming
    @GET("/api/test")
    suspend fun _test() : HttpStatement
}

fun TestService.testFlow():Flow<FooResponse> = callbackFlow {
    val channel: ByteReadChannel=_test().body()
    while (!channel.isClosedForRead) {
        val line = channel.readUTF8Line()

        if (!line.isNullOrBlank()) {
            try {
                val obj=if (line.startsWith("data:")){
                    //for SSE text/event-stream
                    ktjson.decodeFromString(FooResponse.serializer(),line.substringAfter("data:"))
                }else{
                    //for Nd…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by ag2s20150909
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant