mardi 21 avril 2015

How combine data from several responses in Scala Play2?

I need do some requests to different URLs, get data from their responses and put this info in one list, but i have some misunderstanding in this theme. 1) for one request i do

def doRequest: Future[WSResponse] = {
client
  .url("MY_URL")
  .withRequestTimeout(5000)
  .get()}

Then I parse json in response to List of my objects:

def list: Future[List[FoobarEntry]] = {
doRequest.map {
  response => {
    val json = response.json \ "foobar"
      json.validate[List[FoobarEntry]] match {
      case js:JsSuccess[List[FoobarEntry]]=>
        js.get
      case e:JsError => Logger.error(JsError.toFlatJson(e).toString()); List()
    }
  }
}}

I think that for several url i should write some look like

def doRequests: List[Future[WSResponse]] = {
List(client
     .url("URL_1")
     .withRequestTimeout(5000)
     .get(),
     client
     .url("URL_2")
     .withRequestTimeout(5000)
     .get())}

But how parse this list of Future[WSResponse] like my def list: Future[List[FoobarEntry]]?

Aucun commentaire:

Enregistrer un commentaire