How to using Akka-Http make transformation: RequestContext -> HttpEntity -> ByteStrings -> JsonString -> Deserialize -> [Domain object]

971 views
Skip to first unread message

pavel meledin

unread,
Apr 23, 2015, 11:46:32 AM4/23/15
to akka...@googlegroups.com
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:
  1. Is it a correct idea to split such transformation into small pieces in order to be able to combine them later?
  2. How such things are suppose to be handled / implemented?
  3. 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




Martynas Mickevičius

unread,
May 14, 2015, 8:25:59 AM5/14/15
to akka...@googlegroups.com
Hi Pavel,

you are able to transform request to the domain object leveraging Mashalling/Unmarshalling without any custom code.


If you want to use something other that spray-json, you can write your own marshaller/unmarshaller. Here is the source code of spray-json marshaller and unmarshaller: https://github.com/akka/akka/blob/release-2.3-dev/akka-http-marshallers-scala/akka-http-spray-json/src/main/scala/akka/http/scaladsl/marshallers/sprayjson/SprayJsonSupport.scala

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Martynas Mickevičius
TypesafeReactive Apps on the JVM

Rafał Krzewski

unread,
May 14, 2015, 8:58:10 AM5/14/15
to akka...@googlegroups.com
Pavel,
here's another example of using akka-http client to execute HTTP requests and materialize responses into domain objects:

cheers,
Rafał
Reply all
Reply to author
Forward
0 new messages