Skip to content

Instantly share code, notes, and snippets.

@negator
Last active May 18, 2017 15:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save negator/4361905 to your computer and use it in GitHub Desktop.
Save negator/4361905 to your computer and use it in GitHub Desktop.
Example paging enumerator using Play! WS.
import play.api.libs.ws._
import play.api.libs.iteratee._
def pagingEnumerator(url:String):Enumerator[JsValue]={
var maybeNextUrl = Some(url) //Next url to fetch
Enumerator.fromCallback[JsValue] ( retriever = {
val maybeResponsePromise =
maybeNextUrl map { nextUrl=>
WS.url(nextUrl).get.map { reponse =>
val json = response.json
maybeNextUrl = (json \ "next_url").asOpt[String]
val code = response.status //Potential error handling here
json
}
}
/* maybeResponsePromise will be an Option[Promise[JsValue]].
* Need to 'flip' it, to make it a Promise[Option[JsValue]] to
* conform to the fromCallback constraints */
maybeResponsePromise match {
case Some(responsePromise) => responsePromise map Some.apply
case None => PlayPromise pure None
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment