From a4814d8ef2a19bfc38d3fc791fc4f537057f7d1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Thu, 31 Mar 2016 15:07:28 +0200 Subject: [PATCH 1/2] DIY pagination --- R/github.R | 2 +- R/pagination.R | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 R/pagination.R diff --git a/R/github.R b/R/github.R index be6a3d3..b218b6a 100644 --- a/R/github.R +++ b/R/github.R @@ -257,7 +257,7 @@ get.github.context <- function() }) output <- list(ok = r$status_code %in% expect.code, content = result, headers = r$headers, - code = r$status_code) + code = r$status_code, req = req, method = method, params = params) ## class(output) <- "github" output } diff --git a/R/pagination.R b/R/pagination.R new file mode 100644 index 0000000..c1ffb10 --- /dev/null +++ b/R/pagination.R @@ -0,0 +1,33 @@ +#' @export +next.page <- function(response, ..., ctx = get.github.context()) { + links <- .parse_links(response) + if (is.null(links[["next"]])) { + warning("No more pages.", call. = FALSE) + return(NULL) + } + + params <- response$params + params$page <- .parse_page_from_link(links[["next"]]) + + if (is.null(params$page)) { + stop("Could not find next page from url ", links[["next"]], + call. = FALSE) + } + + .api.request(ctx = ctx, req = response$req, method = response$method, + params = params, ...) +} + +.parse_links <- function(response) { + regex <- "[<]([^>]+)[>]; rel=\"([^\"]+)\"" + raw_links <- response$headers$link + link_matches <- str_match_all(raw_links, regex)[[1]][,1] + urls <- str_replace(link_matches, regex, "\\1") + targets <- str_replace(link_matches, regex, "\\2") + names(urls) <- targets + as.list(urls) +} + +.parse_page_from_link <- function(url) { + parse_url(url)$query$page +} From 692cf6122140dcd9aeb7d500d191977126e59850 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Thu, 31 Mar 2016 15:22:25 +0200 Subject: [PATCH 2/2] early exit if links not found --- R/pagination.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/pagination.R b/R/pagination.R index c1ffb10..ecaf890 100644 --- a/R/pagination.R +++ b/R/pagination.R @@ -21,6 +21,7 @@ next.page <- function(response, ..., ctx = get.github.context()) { .parse_links <- function(response) { regex <- "[<]([^>]+)[>]; rel=\"([^\"]+)\"" raw_links <- response$headers$link + if (is.null(raw_links)) return(NULL) link_matches <- str_match_all(raw_links, regex)[[1]][,1] urls <- str_replace(link_matches, regex, "\\1") targets <- str_replace(link_matches, regex, "\\2")