Reputation: 17676
How can I map the JSON response of a play-ws async web request in a typesafe manner?
private def webRequest(): Map[String, Any] = {
case class QuantileResult(val_05: Double, val_25: Double, val_50: Double, val_75: Double, val_90: Double)
object QuantileResult {
implicit val quantileResultFormat = Json.format[QuantileResult]
}
implicit val quantileReads = Json.reads[QuantileResult]
val payload = Json.obj(
"json" -> Json.parse("""{"type":1,"x":[1,2,3,4,5,6,7,8,9,10],"probs":[0.05,0.25,0.75,0.95]}"""),
"windowWidth" -> JsNumber(11)
)
wsClient.url("http://public.opencpu.org/ocpu/library/stats/R/quantile/json")
.withHeaders("Content-Type" -> "application/json")
.post(payload)
.map { wsResponse =>
if (!(200 to 299).contains(wsResponse.status)) {
sys.error(s"Received unexpected status, open-cpu error ${wsResponse.status} : ${wsResponse.body}")
}
println(s"OK, received ${wsResponse.body}")
wsResponse.json.validate[QuantileResult]
match {
case JsSuccess(arrOut: QuantileResult, _) => QuantileResult //TODO how to perform mapping here to Map[String, Any]
case e: JsError => JsError.toFlatForm(e) // TODO empty array does not work here otherwise future[object] does not conform to expected return type of Map[String, Any]
}
}
}
What works is
wsClient.url("url").withHeaders("Content-Type" -> "application/json").post(payload).map { wsResponse => result = wsResponse.body }
val json = Json.parse(result)
val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
val returnValue = mapper.readValue[Map[String, Any]](result)
But that seems to be really clunky and is not async.
Upvotes: 2
Views: 1277
Reputation: 17676
The solution is to change the method signature to a proper future Future[Seq[Map[String, Any]]]
and resolve it via Await.result(someMethod), 20.seconds)
The mapping is performed like
wsResponse.json.validate[Seq[OutlierResult]] match {
case JsSuccess(result, _) => result.map(outlierRes => Map("period" -> outlierRes.period, "amount" -> outlierRes.amount, "outlier" -> outlierRes.outlier))
case JsError(error) => throw new OutlierParseException(error.toString())
}
even though this could be improved to not throw an Exception
Upvotes: 1