Created
February 22, 2015 22:22
-
-
Save stephennancekivell/e778101317cc55b6720a to your computer and use it in GitHub Desktop.
WS executeRecovering
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.libs.ws._ | |
def get(url: String): Future[WSResponse] = { | |
val split = url.split("/") | |
val hostname = split(0) | |
val path = "/"+split(1) | |
def getHosts = Try(InetAddress.getAllByName(hostname).map(_.getHostAddress).toSeq) | |
def executeRecovering(triedHosts: Seq[String] = Nil): Future[WSResponse] = { | |
getHosts match { | |
case Failure(e) => Future.failed(e) | |
case Success(hosts) => | |
val unTriedHosts = hosts.filterNot(triedHosts.contains) | |
unTriedHosts.size match { | |
case 0 => Future.failed(new RuntimeException("no can do")) | |
case 1 => WS.url(unTriedHosts.head+path).get() | |
case _ => WS.url(unTriedHosts.head+path).get().recoverWith { | |
case e => executeRecovering(triedHosts ++ Seq(unTriedHosts.head)) | |
} | |
} | |
} | |
} | |
executeRecovering() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment