sdk-java
sdk-java copied to clipboard
Any plan to support Ktor?
Hi, I'm writing an application with the Ktor server framework.
Do you guys have any plan to support Ktor in the future?
We don't have plans to support Ktor.
are you finding difficulties in integrating Ktor with the SDK?
@wonsuc doesn't at least generic HTTP adapter work? Something like:
package com.example
import com.example.plugins.configureRouting
import io.cloudevents.core.message.MessageReader
import io.cloudevents.http.HttpMessageFactory
import io.cloudevents.http.impl.HttpMessageWriter
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.util.*
import kotlinx.coroutines.runBlocking
import java.util.function.BiConsumer
suspend fun getMessageReader(call: ApplicationCall): MessageReader {
return HttpMessageFactory.createReader({ processHeader ->
call.request.headers.flattenForEach { header, value -> processHeader.accept(header, value) }
}, call.request.receiveChannel().toByteArray());
}
fun getMessageWriter(call: ApplicationCall): HttpMessageWriter {
var contentType = ContentType.Any
val putHeader = BiConsumer<String,String> {header, value ->
if (header.equals("Content-Type", true)) {
contentType = ContentType.parse(value)
} else {
call.response.header(header, value)
}
}
return HttpMessageFactory.createWriter(putHeader) { body ->
runBlocking {
call.respondBytes(body, contentType, HttpStatusCode.OK)
}
}
}
fun main() {
embeddedServer(Netty, port = 8080, host = "127.0.0.1") {
configureRouting()
routing {
post("/") {
val ce = getMessageReader(call).toEvent()
println(ce)
getMessageWriter(call).writeBinary(ce)
}
}
}.start(wait = true)
}
Only problem with it is that ktor doesn't like manually setting of Content-Type header, but that ktor issue.
Another inconvenience is that our API is synchronous -- no co-routines.