Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active April 2, 2023 10:11
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 dacr/3eed0b6c23bf81d73325ad560eedd499 to your computer and use it in GitHub Desktop.
Save dacr/3eed0b6c23bf81d73325ad560eedd499 to your computer and use it in GitHub Desktop.
lazy generic http web linking / published by https://github.com/dacr/code-examples-manager #1b2530a6-4ce3-4689-88a0-2dfba7ce4960/8cf3c3db9cb297fe94864a77e6a1d3730d0f1aed
// summary : lazy generic http web linking
// keywords : weblinking, http, lazy
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 1b2530a6-4ce3-4689-88a0-2dfba7ce4960
// created-on : 2020-08-04T20:57:04Z
// managed-by : https://github.com/dacr/code-examples-manager
// execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc'
/* Using Web Linking to get large amount of results : https://tools.ietf.org/html/rfc8288 */
// TODO Make the following code "generic"
/*
def userGists(user: GistUser): LazyList[GistInfo] = {
val nextLinkRE = """.*<([^>]+)>; rel="next".*""".r
def worker(nextQuery: Option[Uri], currentRemaining: Iterable[GistInfo]): LazyList[GistInfo] = {
(nextQuery, currentRemaining) match {
case (None, Nil) => LazyList.empty
case (_, head :: tail) => head #:: worker(nextQuery, tail)
case (Some(query), Nil) =>
val response = {
basicRequest
.get(query)
.header("Authorization", s"token $token")
.response(asJson[Array[GistInfo]])
.send()
}
response.body match {
case Left(responseException) =>
logger.error(s"List gists - Something wrong has happened", responseException)
throw responseException
case Right(gistsArray) =>
val next = response.header("Link") // it provides the link for the next & last page :)
val newNextQuery = next.collect { case nextLinkRE(uri) => uri"$uri" }
worker(newNextQuery, gistsArray.toList)
}
case other =>
logger.warn("Not understandable response : " + other.toString())
LazyList.empty
}
}
val count = 10
val userLogin = user.login
val startQuery = uri"$apiUrl/users/$userLogin/gists?page=1&per_page=$count"
worker(Some(startQuery), Nil)
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment