The use case is actually fairly typical. A lot of web services use authorization tokens that you retrieve at the start of a session and you need to send those back on subsequent requests.
I know I can do it like this:
lazy val myData = {
val request = ws.url("/some/url").withAuth(user, password, WSAuthScheme.BASIC).withHeaders("Accept" -> "application/json")
Await.result(request.get().map{x => x.json }, 120.seconds)
}
That just feels wrong as all the docs say never us Await.
Is there a Future/Promise Scala style way of handling this?
I've found .onComplete
which allows me to run code upon the completion of a Promise however without using a (mutable) var
I see no way of getting a value in that scope into a lazy val
in a different scope. Even with a var
there is a possible timing issue -- hence the evils of mutable variables :)
Any other way of doing this?