Skip to content

Instantly share code, notes, and snippets.

@quii
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quii/a8165f87656c031cf9d6 to your computer and use it in GitHub Desktop.
Save quii/a8165f87656c031cf9d6 to your computer and use it in GitHub Desktop.
def index(doi: String) = Action.async { request =>
// Fake way of checking whether a particular user has access to the content or not (for demo purposes of course!)
val fakeAccessCheck = math.random < 0.50
for {
timeBody <- getBodyOf("http://localhost:9000/time")
fullTextBody <- getFulltextOrAbstract(doi,fakeAccessCheck)
downloadsBody <- getBodyOf(s"http://localhost:9000/downloads/${encodeDoi(doi)}")
} yield {
Ok(views.html.index(timeBody, fullTextBody, downloadsBody))
}
}
private def getBodyOf(url:String): Future[String] = WS.url(url).get.map(r=> r.body)
private def getFulltextOrAbstract(doi: String, hasAccess:Boolean): Future[String] = {
val encodedDoi = encodeDoi(doi)
val url = if(hasAccess) s"http://localhost:9000/fulltext/$encodedDoi" else s"http://localhost:9000/abstract/$encodedDoi"
getBodyOf(url)
}
private def encodeDoi(doi: String) = java.net.URLEncoder.encode(doi)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment