Closed twr143 closed 4 years ago
elaborated by myself downloading file as an fs2 stream, feel free to correct me in any case
import fs2._
private val streamingEndpoint = baseEndpoint.get
.in(HutsPath / "stream")
.out(header[String]("Content-Disposition"))
.out(streamBody[EntityBody[Task]](schemaFor[Byte], CodecFormat.OctetStream()))
.serverLogic[Task] {
_ =>
Stream.emit(List[Char]('a', 'b', 'c', 'd')).repeat.flatMap(list => Stream.chunk(Chunk.seq(list)))
.take(10000000).covary[Task].map(_.toByte).pure[Task]
.map(s => ("attachment; filename=resp-stream-file.txt", s)).toOut
}
for partial content the following endpoint might help
private val streamingEndpoint = baseEndpoint.get
.in(HutsPath / "stream")
.out(header[String]("Accept-Ranges"))
.out(header[String]("Content-Range"))
.out(header[String]("Content-Length"))
.out(statusCode)
.out(streamBody[EntityBody[Task]](schemaFor[Byte], CodecFormat.TextPlain()))
.serverLogic[Task] {
_ =>
val size = 100
(for {
r <- Stream.emit(List[Char]('a', 'b', 'c', 'd')).
repeat.flatMap(list => Stream.chunk(Chunk.seq(list))).metered[Task](100.millis)
.take(size).covary[Task].map(_.toByte)
.onFinalize(Task(logger.warn("stream finalized")))
.pure[Task]
.map(s => ("bytes", s"bytes 0-$size/$size", s"$size", sttp.model.StatusCode.unsafeApply(206), s))
_ <- Task.now(logger.warn("task finished working"))
} yield r).toOut
}
Thanks! I've added a simplified example to the sources
I've had a look at the streaming example for Akka , could you provide analogous example for streamBody for http4s and fs2 streams? Updated: not monix stream but fs2.