Skip to content

Instantly share code, notes, and snippets.

@andyczerwonka
Created May 22, 2012 19:34
Show Gist options
  • Save andyczerwonka/2771131 to your computer and use it in GitHub Desktop.
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
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
}
@andyczerwonka
Copy link
Author

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 a play.api.libs.concurrent.Promise[play.libs.WS.Response]. How should I be approaching this?

@sadache
Copy link

sadache commented May 22, 2012

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

@andyczerwonka
Copy link
Author

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