Hi,
Is there any way easy way to unmarshal XML to a case class. Compared to
the spray API, the solution I found is too verbose, but I don't seem to
find a good combinator to use.
I have the following route:
pathEnd {
post {
entity(as[ProcessCommandsRequest]) { req =>
onComplete(oorStub ? req) {
case Success(msg: ProcessCommandsResponse) => complete(msg)
case _ =>
complete(StatusCodes.InternalServerError -> "")
}
}
}
The marshaller trait is the following. Everything works, but the API is too
verbose. A whole lot of unmarshallers, and marshallers for that matter are
defined in implicit scope. Is there any combinator or construction
off-the-shelf available in akka-http that can reduce the amount of
boilerplate, the chain of flatmaps. Compared to goold-ol' Spray, this is a
whole lot of code. Somebody have any ideas? Regards, Dennis
import akka.actor.ActorSystem
import akka.event.LoggingAdapter
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import akka.http.scaladsl.marshallers.xml.ScalaXmlSupport
import akka.http.scaladsl.marshalling.{Marshaller, Marshalling}
import akka.http.scaladsl.model._
import akka.http.scaladsl.unmarshalling.{FromEntityUnmarshaller,
FromRequestUnmarshaller, Unmarshaller}
import akka.stream.FlowMaterializer
import com.example.ProcessCommandsRequest
import spray.json.DefaultJsonProtocol
import scala.concurrent.{ExecutionContext, Future}
import scala.xml.NodeSeq
trait StubMarshallers extends ScalaXmlSupport with DefaultJsonProtocol with
SprayJsonSupport {
implicit def system: ActorSystem
implicit def ec: ExecutionContext
implicit def materializer: FlowMaterializer
implicit def log: LoggingAdapter
// unmarshallers
implicit val pcUnmarshaller = Unmarshaller[NodeSeq, ProcessCommandsRequest] {
xml =>
Future(ProcessCommandsRequest("", Nil))
}
implicit val pcrUnmarshaller: FromRequestUnmarshaller[ProcessCommandsRequest]
= Unmarshaller[HttpRequest, ProcessCommandsRequest] { req =>
fruReqToEntity(req).flatMap(httpEntity =>
defaultNodeSeqUnmarshaller(materializer, ec)(httpEntity)).flatMap(nodeSeq =>
pcUnmarshaller(nodeSeq))
}
implicit val fruReqToEntity: FromRequestUnmarshaller[HttpEntity] =
Unmarshaller[HttpRequest, HttpEntity] { req =>
import scala.concurrent.duration._
req.entity.toStrict(10.seconds).map { strict =>
HttpEntity(ContentType(MediaTypes.`text/xml`), strict.data)
}
}
// marshallers
implicit val pcrMarshaller = Marshaller[OorStub.ProcessCommandsResponse,
HttpResponse] { pcr =>
Future(List(
Marshalling.Opaque { () =>
pcr.error.map { _ =>
HttpResponse().withEntity(ContentType(MediaTypes.`text/xml`),
OorStubXml.optInFailureResponse)
}.getOrElse {
HttpResponse().withEntity(ContentType(MediaTypes.`text/xml`),
OorStubXml.optInSuccessResponse)
}
}
))
}
implicit val futureMarshaller =
Marshaller.futureMarshaller[OorStub.ProcessCommandsResponse, HttpResponse]
}
--
>>>>>>>>>> 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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.