Created
January 30, 2017 08:14
-
-
Save outime/2be39706c541e2f92a785e4104124e87 to your computer and use it in GitHub Desktop.
How to handle timeout with WS api in Play2.1/Scala 2.10
This file contains hidden or 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 asFutureEither[T](f: Future[T]): Future[Either[Throwable, T]] = | |
f map { Right(_) } recover { case x => Left(x) } |
This file contains hidden or 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
import play.api._ | |
import play.api.mvc._ | |
import play.api.libs.ws.WS | |
import scala.concurrent.Future | |
import play.api.libs.concurrent.Execution.Implicits._ | |
import java.util.concurrent.TimeoutException | |
object Application extends Controller { | |
def index = Action { | |
Async { | |
val request = WS.url("https://api.github.com/repos/playframework/Play20/commits") | |
.withTimeout(3000).get | |
request map { response => | |
Ok(response.json) | |
} recover { | |
case t: TimeoutException => | |
RequestTimeout(t.getMessage) | |
case e => | |
ServiceUnavailable(e.getMessage) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment