gpt4 book ai didi

scala - 类型不匹配;发现 : scala. 并发。Future[play.api.libs.ws.Response] 需要 : play. api.libs.ws.Response

转载 作者:行者123 更新时间:2023-12-03 04:18:57 25 4
gpt4 key购买 nike

我正在尝试向 Pusher api 发出发布请求,但我无法返回正确的类型,因为类型不匹配;找到:scala.concurrent.Future[play.api.libs.ws.Response] 需要: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
}

最佳答案

您使用 post 发出的请求是异步的。该调用立即返回,但不返回 Response 对象。相反,它返回一个 Future[Response] 对象,一旦 http 请求异步完成,该对象将包含 Response 对象。

如果您想阻止执行直到请求完成,请执行以下操作:

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

查看有关 future 的更多信息 here .

关于scala - 类型不匹配;发现 : scala. 并发。Future[play.api.libs.ws.Response] 需要 : play. api.libs.ws.Response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15692681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com