okio icon indicating copy to clipboard operation
okio copied to clipboard

JS sample bridging between Ktor and Okio

Open yschimke opened this issue 3 years ago • 0 comments

From this PR https://github.com/square/okhttp/pull/7326 which is just an experiment

It isn't clear how to handle these sorts of conversions

import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.bodyAsChannel
import io.ktor.utils.io.core.readBytes
import okhttp3.RequestBody
import okhttp3.ResponseBody

actual suspend fun HttpResponse.toHttpResponseBody(): ResponseBody {
  val bytes = this.bodyAsChannel().readRemaining().readBytes()
  val buffer = okio.Buffer()
  buffer.write(bytes)
  return HttpResponseBody(this, buffer)
}

actual suspend fun RequestBody.toHttpRequestBody(): Any {
  val buffer = okio.Buffer()
  this.writeTo(buffer)
  return buffer.readByteArray()
}

Is there a good pattern, or should there be a ktor-okio-converions module somewhere?

In the first example the ktor parts claim to be suspending to go from Channel to byteArray. But we then write to a Buffer, which is not.

Is there a good ByteArray -> BufferedSink conversion path?

yschimke avatar Jun 13 '22 08:06 yschimke