Last active
May 18, 2017 15:15
-
-
Save negator/4361905 to your computer and use it in GitHub Desktop.
Example paging enumerator using Play! WS.
This file contains 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._ | |
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