Last active
April 2, 2023 10:11
-
-
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
This file contains hidden or 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
// 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