@@ -23,7 +23,7 @@ import sttp.tapir.*
23
23
import scala .concurrent .{ExecutionContext , Future }
24
24
import scala .util .*
25
25
import org .apache .pekko
26
- import pekko .stream .scaladsl .{Flow , Source }
26
+ import pekko .stream .scaladsl .{Flow , Source , Sink }
27
27
import pekko .util .ByteString
28
28
import sttp .tapir .server .play .PlayServerOptions
29
29
@@ -41,18 +41,26 @@ def handleErrors[T](f: Future[T]): Future[Either[ErrorInfo, T]] =
41
41
Success (Left (e.getMessage))
42
42
}
43
43
44
- def logic (s : (Long , Source [ByteString , Any ])): Future [( Long , Source [ ByteString , Any ]) ] = {
44
+ def logic (s : (Long , Source [ByteString , Any ])): Future [String ] = {
45
45
val (length, stream) = s
46
- println(s " Received $length bytes, ${stream.map(_.length)} bytes in total " )
47
- Future .successful((length, stream))
46
+ println(s " Transmitting $length bytes... " )
47
+ val result = stream
48
+ .runFold(List .empty[ByteString ])((acc, byteS) => acc :+ byteS)
49
+ .map(_.reduce(_ ++ _).decodeString(" UTF-8" ))
50
+ result.onComplete {
51
+ case Failure (ex) =>
52
+ println(s " Stream failed with exception: $ex" )
53
+ case Success (s) =>
54
+ println(s " Stream finished: ${s.length}/ $length transmitted " )
55
+ }
56
+ result
48
57
}
49
58
50
59
val e = endpoint.post
51
60
.in(" chunks" )
52
61
.in(header[Long ](HeaderNames .ContentLength ))
53
62
.in(streamTextBody(PekkoStreams )(CodecFormat .TextPlain ()))
54
- .out(header[Long ](HeaderNames .ContentLength ))
55
- .out(streamTextBody(PekkoStreams )(CodecFormat .TextPlain ()))
63
+ .out(stringBody)
56
64
.errorOut(plainBody[ErrorInfo ])
57
65
.serverLogic(logic.andThen(handleErrors))
58
66
0 commit comments