Hi everyone,
I'm using akka-http 1.0-M5 and I want to make the following transformation:
From RequestContext to HttpEntity then ByteStrings andthen JsonString andthen Deserialize andthen [Domain object that came to server in payload]
The transformation path looks like: RequestContext -> HttpEntity -> ByteStrings -> JsonString -> Deserialize -> [Domain object]
Here is a naive implementation of how to get complete request body as a string:
// How to process request body
val collectBodySink = Sink.fold[String, String]("")(_ + _)
// Extract request body
val source = ctx.request.entity.getDataBytes().map {
chunk ⇒
chunk.decodeString(HttpCharsets.`UTF-8`.value)
}
// Define the way how materialization should follow
val runnable = source.toMat(collectBodySink)(Keep.right)
// Run materialization process
val requestBody = runnable.run()
// Wait till materialization will be finished
println(Await.result(requestBody, 5.seconds))
I thought that I can describe a bunch of flows:
- From RequestContext to HttpEntity
- From HttpEntity to ByteStrings
- From ByteStrings to JsonString
- etc
and later combine them all in the following way (pseudo code):
var domainObject = Source(ctx)
.via(Flow[RequestContext].to[HttpEntity]())
.via(Flow[HttpEntity].to[ByteStrings]())
.via(Flow[ByteStrings].to[JsonString]())
.via(Flow[JsonString].to[DomainObject]())
.toMat(Sink.ignore)
Questions regarding solution of this task:
- Is it a correct idea to split such transformation into small pieces in order to be able to combine them later?
- How such things are suppose to be handled / implemented?
- RequestContext contains a Request which contains HttpEntity which contains dataBytes as a Source. In order to implement Flow from HttpEntity to ByteStrings I'll have to materialize & run inside this Flow's body some RunnableFlow. Was it designed like that? Is it ok to do it this ways? Or I have to describe all flows, then combine then in some logical stream and only then call run once in order to start all those transformations ?
Thank you in advance,
Appriciate any help/info regarding this topic.
Br,
Pavel