There is a REST api which returns a json with a list of jsons (named results) and a url to the next batch of results (this url is equal to null for the last "page"). I want to make an aggregate of the whole results (concatenate all the lists of jsons into a single one). I am using spray-client to do the GET requests and this is what I came up with:
val request: HttpRequest => Future[MyResponse] = sendReceive ~> unmarshal[MyResponse]
def getCandidatesStartingFrom(url: String): Future[List[Candidate]] =
request(Get(url)).flatMap {
response =>
val next = response.next match {
case Some(nextUrl) => getCandidatesStartingFrom(nextUrl)
case None => Future.successful(Nil)
}
next.map(response.results ++ _)
}
My question is: is there a way to make this more functional? (avoid the recursion or maybe make it tail recursive?). Or even, does spray support idiomatically this kind of aggregations?
can be written as the following. But it's not that much of an improvement.