Here's how I use WS in my side project:
val itselfNodeFuture = Statix.doParams( Statix.SolrSelectWSReq,
List(
"wt" -> "json",
"q" -> "*:*",
"fq" -> "node_type:collection",
"fq" -> "id:%d".format( nodeId),
"indent" -> "true",
"rows" -> "1",
"fl" -> "id,parent_id,title",
"fl" -> "date_created,date_about,date_modified")
).get()
//Use the first Await after the last future
val itselfJson = Await.result(
itselfNodeFuture, Duration("2 sec")).json
val mainRow = (itselfJson "response" "docs").as[ Seq[JsValue]]
val mainNodeParent = (mainRow(0) "parent_id").as[Long]
val mainNodeTitle = (mainRow(0) "title").as[String]
And here's the utility class I use, the doParams
is especially useful.
object Statix { //Noder must extend this
def SolrSelectWSReq = WS.url("http://127.0.0.1:8080/solr-store/collection1/select/")
def SolrUpdateWSReq = WS.url("http://127.0.0.1:8080/solr-store/collection1/update/json/")
def doParams(request: WS.WSRequestHolder, params: List[(String, String)]) = {
params.foldLeft( request){
(wsReq, tuple) => wsReq.withQueryString( tuple)}}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…