2016-03-04 43 views
0

我遇到了一個我目前無法解決的問題。我正在做多個http請求,在每個響應中,它應該有一個Array[DTAnnotation]。我想積累所有結果列表到一個(這不是問題在這裏)。我的問題是我無法返回WSResponse的結果。我嘗試什麼:從多個HTTP請求中提取並累加結果

import mymodel.{DTFeatures, DTResponse, DTRequest, DTAnnotations} 

def checkForSpike 
    (
    awsKey : String, 
    collection : JSONCollection, 
    record : Record, 
    features : Array[DTFeatures] 
) : Unit = { 
    val url = config.getString("url").getOrElse 
    val holder = WS.url(url) 
    val complexHolder = 
    holder.withHeaders(("Content-Type","application/json")) 
    // excepting result is List[Array[DTAnnotations]] 
    val test : List[Array[DTAnnotations]] = 
    for(feature <- features) yield { 
    val dtFeature = Json.stringify(Json.toJson(DTRequest(feature))) 
    val futureResponse = complexHolder.post(dtFeature) 
    Logger.info("Make the HTTP POST Request in " + (t1 - t0) + " msecs") 
    futureResponse.map { response => 
     Logger.info("Get response in " + (System.currentTimeMillis - t1)) 
     if(response.status == 200) { 
     response.json.validate[DTResponse].map { dtResponse => 
      // I want to return this 
      dtResponse.annotations 
     }.recoverTotal { _ => 
      Logger.debug("invalid json") 
     } 
     } else { 
     Logger.debug(Json.prettyPrint(Json.obj("status" -> response.status, "body" -> response.body))) 
     } 
    } 
     Await.result(futureResponse, 10.seconds) 
    } 
} 

因爲響應是一個Future,我嘗試添加Await得到註解,但我在打字階段一個錯誤:

[error] found : Array[play.api.libs.ws.WSResponse] 
[error] required: List[Array[DTAnnotation]] 

我怎麼能解決這個問題?謝謝 !

回答

1

有一些錯誤可以避免這種情況發揮作用。我添加一個與您的預期類型一致的版本,如果您有問題,我會在評論中回答。

def checkForSpike 
    (
    awsKey: String, 
    collection: JSONCollection, 
    record: Record, 
    features: Array[DTFeatures] 
): Unit = { 
    val url = config.getString("url").getOrElse 
    val holder = WS.url(url) 
    val complexHolder = 
     holder.withHeaders(("Content-Type", "application/json")) 
    // excepting result is List[Array[DTAnnotations]] 
    val test: List[Array[DTAnnotations]] = 
     for (feature <- features.toList) yield { 
     val dtFeature = Json.stringify(Json.toJson(DTRequest(feature))) 
     val futureResponse = complexHolder.post(dtFeature) 
     val futureAnnotations: Future[Array[DTAnnotations]] = futureResponse.map { response => 
      if (response.status == 200) { 
      response.json.validate[DTResponse].map { dtResponse => 
       // I want to return this 
       dtResponse.annotations 
      }.recoverTotal { _ => 
       Logger.debug("invalid json") 
       ??? // An Array response should be expected, maybe empty 
      } 
      } else { 
      Logger.debug(Json.prettyPrint(Json.obj("status" -> response.status, "body" -> response.body))) 
      ??? // An Array response should be expected, maybe empty 
      } 
     } 

     Await.result(futureAnnotations, 10.seconds) 
     } 
    } 

問題:

  • 要素必須被轉換,如果你希望一個列表是通過爲修真返回
  • 未來的響應返回地圖 另一個未來列表,此值應在等待中使用
  • 爲確保所有分支中futureAnnotations的類型正確,類型應該是有效的
+0

工程像魅力!也感謝您的時間和意見 – alifirat

+1

不客氣。一旦你有它的工作,你可以改進一點。避免等待並閱讀有關組合期貨的信息。 Daniel Westheide的教程非常好。這是關於期貨的章節http://danielwestheide.com/blog/2013/01/09/the-neophytes-guide-to-scala-part-8-welcome-to-the-future.html –

+0

您是否打算使用'flatMap'而不是'map'? – alifirat