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

I am trying to make a request to send a Pusher api, but I'm having problems returning the right type, I do not match the type; 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
}
+5
source share
3 answers

A request created using postis asynchronous. This call returns immediately, but does not return an object Response. Instead, it returns an object Future[Response]that will contain the object Responseas soon as the HTTP request is executed asynchronously.

, :

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

.

+4

map:

WS.url("http://"+domain+url+"?"+params+"&auth_signature="+signatureEncoded).post(body).map(_)
+3

, , Future[ws.Response]. , AsyncResult Async { ... }, Play .

def webServiceResult = Action { implicit request =>
  Async {
    // ... your logic
    trigger(channel, event, message).map { response =>
      // Do something with the response, e.g. convert to Json
    }
  }
}
+3

All Articles