Return boolean from async request play framework scala

358 views Asked by At

I want to return a boolean from the network request and perform action based on that boolean response. Right now I can't do that because the type Any doesnt get converted to boolean. Could someone help?

  def tetetetestae() = Action.async { implicit request =>
    request.session.get("email") match {
      case Some(email) =>
        for {
          t <- test("","","","","")
        } yield {
          if(t){
            Ok("")
          } else {
            Ok("")
          }
        }
    }
  }
}

  def test(email: String, firstName: String, lastName: String, c_password: String, n_password: String) = {
    WS.url("http://hire.monster.com:81/authenticateUser")
      .post(Map("email" -> Seq(email), "password" -> Seq(c_password)))
      .map(response =>
      if(response.body.contains("true")){
        for(result <- WS.url("http://localhost:9001/settings/updateUser")
          .post(Map("email" -> Seq(email), "firstName" -> Seq(firstName), "lastName" -> Seq(lastName), "n_password" -> Seq(n_password))))
          yield result.body.contains("true")
      } else {
        false
      })
  }
1

There are 1 answers

0
Carlos Vilchez On

You can try this:

def test(email: String, firstName: String, lastName: String, c_password: String, n_password: String): Future[Boolean] = {
  WS.url("http://hire.monster.com:81/authenticateUser")
    .post(Map("email" -> Seq(email), "password" -> Seq(c_password)))
    .flatMap(response =>
    if(response.body.contains("true")){
      for(result <- WS.url("http://localhost:9001/settings/updateUser")
        .post(Map("email" -> Seq(email), "firstName" -> Seq(firstName), "lastName" -> Seq(lastName), "n_password" -> Seq(n_password))))
        yield result.body.contains("true")
    } else {
      Future(false)
    })
}

This will return the Future[Boolean] you need.