Created
May 22, 2012 19:34
-
-
Save andyczerwonka/2771131 to your computer and use it in GitHub Desktop.
I'm trying to understand how to best deal with sending an async request to a web service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def measures(user: User): List[Measure] = { | |
val prefs = UserPreferences.findByUser(user) // get my prefs | |
val request = generateRequest(prefs) // build up a WS request | |
val promise = WS.url(exportEndpoint).post(request) | |
promise.orTimeout(Nil, 100000).map { responseOrTimeout => | |
responseOrTimeout.fold( | |
response => { | |
val body = promise.value.get.body | |
val lines = body.split("\n").map(_.split(",")) | |
lines.map { | |
case Array(a, b, c) => Measure( | |
a.substring(1, a.length() - 1), | |
dateFrom(b.substring(1, b.length() - 1)), | |
c.substring(1, c.length() - 1).toDouble) | |
}.toList | |
}, | |
timeout => Nil) | |
}.value.get | |
} |
You should use play.api.libs.ws.WS and NOT the one under play.libs._
Everything unser play.libs is for java, everything under play.api is for Scala
Thanks - now things are making more sense. I made changes to the gist to reflect the correct API.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was looking at http://www.playframework.org/documentation/2.0.1/ScalaAsync and am trying to understand how to deal with a timeout on an external WS call. The WS client API hands me a
play.libs.F.Promise[play.libs.WS.Response]
, while the examples are using aplay.api.libs.concurrent.Promise[play.libs.WS.Response]
. How should I be approaching this?