Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type mismatch; found : scala.concurrent.Future[play.api.libs.ws.Response] required: play.api.libs.ws.Response

I am trying to make a post request to Pusher api, but I am having trouble returning the right type, I a type mismatch; found : scala.concurrent.Future[play.api.libs.ws.Response] required: play.api.libs.ws.Response

def trigger(channel:String, event:String, message:String): ws.Response = {
val domain = "api.pusherapp.com"
val url = "/apps/"+appId+"/channels/"+channel+"/events";
val body = message

val params = List( 
  ("auth_key", key),
  ("auth_timestamp", (new Date().getTime()/1000) toInt ),
  ("auth_version", "1.0"),
  ("name", event),
  ("body_md5", md5(body))
).sortWith((a,b) => a._1 < b._1 ).map( o => o._1+"="+URLEncoder.encode(o._2.toString)).mkString("&");

    val signature = sha256(List("POST", url, params).mkString("\n"), secret.get); 
    val signatureEncoded = URLEncoder.encode(signature, "UTF-8");
    implicit val timeout = Timeout(5 seconds)
    WS.url("http://"+domain+url+"?"+params+"&auth_signature="+signatureEncoded).post(body
}
like image 595
flubba Avatar asked Mar 28 '13 21:03

flubba


1 Answers

The request you are making with post is asynchronous. That call returns immediately, but does not return a Response object. Instead, it returns a Future[Response] object, which will contain the Response object once the http request is completed asynchronously.

If you want to block execution until the request is completed, do:

val f = Ws.url(...).post(...)
Await.result(f)

See more about futures here.

like image 120
axel22 Avatar answered Sep 20 '22 13:09

axel22