1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
|
by_options <- c("limit_offset", "page_perpage")
#' @title Paginator client
#' @description A client to help you paginate
#'
#' @export
#' @template r6
#' @param path URL path, appended to the base URL
#' @param query query terms, as a named list. any numeric values are
#' passed through [format()] to prevent larger numbers from being
#' scientifically formatted
#' @param body body as an R list
#' @param encode one of form, multipart, json, or raw
#' @param disk a path to write to. if NULL (default), memory used.
#' See [curl::curl_fetch_disk()] for help.
#' @param stream an R function to determine how to stream data. if
#' NULL (default), memory used. See [curl::curl_fetch_stream()]
#' for help
#' @param ... For `retry`, the options to be passed on to the method
#' implementing the requested verb, including curl options. Otherwise,
#' curl options, only those in the acceptable set from [curl::curl_options()]
#' except the following: httpget, httppost, post, postfields, postfieldsize,
#' and customrequest
#' @details See [HttpClient()] for information on parameters
#' @section Methods to paginate:
#'
#' Supported now:
#'
#' - `limit_offset`: the most common way (in my experience), so is the default.
#' This method involves setting how many records and what record to start at
#' for each request. We send these query parameters for you.
#' - `page_perpage`: set the page to fetch and (optionally) how many records
#' to get per page
#'
#' Supported later, hopefully:
#'
#' - `link_headers`: link headers are URLS for the next/previous/last
#' request given in the response header from the server. This is relatively
#' uncommon, though is recommended by JSONAPI and is implemented by a
#' well known API (GitHub).
#' - `cursor`: this works by a single string given back in each response, to
#' be passed in the subsequent response, and so on until no more records
#' remain. This is common in Solr
#'
#' @return a list, with objects of class [HttpResponse()].
#' Responses are returned in the order they are passed in.
#'
#' @examples \dontrun{
#' if (interactive()) {
#' # limit/offset approach
#' con <- HttpClient$new(url = "https://api.crossref.org")
#' cc <- Paginator$new(client = con, limit_param = "rows",
#' offset_param = "offset", limit = 50, chunk = 10)
#' cc
#' cc$get('works')
#' cc
#' cc$responses()
#' cc$status()
#' cc$status_code()
#' cc$times()
#' # cc$content()
#' cc$parse()
#' lapply(cc$parse(), jsonlite::fromJSON)
#'
#' # page/per page approach (with no per_page param allowed)
#' conn <- HttpClient$new(url = "https://discuss.ropensci.org")
#' cc <- Paginator$new(client = conn, by = "page_perpage",
#' page_param = "page", per_page_param = "per_page", limit = 90, chunk = 30)
#' cc
#' cc$get('c/usecases/l/latest.json')
#' cc$responses()
#' lapply(cc$parse(), jsonlite::fromJSON)
#'
#' # page/per_page
#' conn <- HttpClient$new('https://api.inaturalist.org')
#' cc <- Paginator$new(conn, by = "page_perpage", page_param = "page",
#' per_page_param = "per_page", limit = 90, chunk = 30)
#' cc
#' cc$get('v1/observations', query = list(taxon_name="Helianthus"))
#' cc$responses()
#' res <- lapply(cc$parse(), jsonlite::fromJSON)
#' res[[1]]$total_results
#' vapply(res, "[[", 1L, "page")
#' vapply(res, "[[", 1L, "per_page")
#' vapply(res, function(w) NROW(w$results), 1L)
#' ## another
#' ccc <- Paginator$new(conn, by = "page_perpage", page_param = "page",
#' per_page_param = "per_page", limit = 500, chunk = 30, progress = TRUE)
#' ccc
#' ccc$get('v1/observations', query = list(taxon_name="Helianthus"))
#' res2 <- lapply(ccc$parse(), jsonlite::fromJSON)
#' vapply(res2, function(w) NROW(w$results), 1L)
#'
#' # progress bar
#' (con <- HttpClient$new(url = "https://api.crossref.org"))
#' cc <- Paginator$new(client = con, limit_param = "rows",
#' offset_param = "offset", limit = 50, chunk = 10,
#' progress = TRUE)
#' cc
#' cc$get('works')
#' }}
Paginator <- R6::R6Class(
'Paginator',
public = list(
#' @field http_req an object of class `HttpClient`
http_req = NULL,
#' @field by (character) how to paginate. Only 'limit_offset' supported
#' for now. In the future will support 'link_headers' and 'cursor'.
#' See Details.
by = "limit_offset",
#' @field chunk (numeric/integer) the number by which to chunk
#' requests, e.g., 10 would be be each request gets 10 records.
#' number is passed through [format()] to prevent larger numbers
#' from being scientifically formatted
chunk = NULL,
#' @field limit_param (character) the name of the limit parameter.
#' Default: limit
limit_param = NULL,
#' @field offset_param (character) the name of the offset parameter.
#' Default: offset
offset_param = NULL,
#' @field limit (numeric/integer) the maximum records wanted.
#' number is passed through [format()] to prevent larger numbers
#' from being scientifically formatted
limit = NULL,
#' @field page_param (character) the name of the page parameter.
#' Default: NULL
page_param = NULL,
#' @field per_page_param (character) the name of the per page parameter.
#' Default: NULL
per_page_param = NULL,
#' @field progress (logical) print a progress bar, using [utils::txtProgressBar].
#' Default: `FALSE`.
progress = FALSE,
#' @description print method for `Paginator` objects
#' @param x self
#' @param ... ignored
print = function(x, ...) {
cat("<crul paginator> ", sep = "\n")
cat(paste0(
" base url: ",
if (is.null(self$http_req)) self$http_req$handle$url else self$http_req$url),
sep = "\n")
cat(paste0(" by: ", self$by), sep = "\n")
cat(paste0(" chunk: ", self$chunk %||% "<none>"), sep = "\n")
cat(paste0(" limit_param: ", self$limit_param %||% "<none>"), sep = "\n")
cat(paste0(" offset_param: ", self$offset_param %||% "<none>"), sep = "\n")
cat(paste0(" limit: ", self$limit %||% "<none>"), sep = "\n")
cat(paste0(" page_param: ", self$page_param %||% "<none>"), sep = "\n")
cat(paste0(" per_page_param: ", self$per_page_param %||% "<none>"), sep = "\n")
cat(paste0(" progress: ", self$progress %||% ""), sep = "\n")
cat(paste0(" status: ",
if (length(private$resps) == 0) {
"not run yet"
} else {
paste0(length(private$resps), " requests done")
}), sep = "\n")
invisible(self)
},
#' @description Create a new `Paginator` object
#' @param client an object of class `HttpClient`, from a call to [HttpClient]
#' @param by (character) how to paginate. Only 'limit_offset' supported for
#' now. In the future will support 'link_headers' and 'cursor'. See Details.
#' @param limit_param (character) the name of the limit parameter.
#' Default: limit
#' @param offset_param (character) the name of the offset parameter.
#' Default: offset
#' @param limit (numeric/integer) the maximum records wanted
#' @param chunk (numeric/integer) the number by which to chunk requests,
#' e.g., 10 would be be each request gets 10 records
#' @param page_param (character) the name of the page parameter.
#' @param per_page_param (character) the name of the per page parameter.
#' @param progress (logical) print a progress bar, using [utils::txtProgressBar].
#' Default: `FALSE`.
#' @return A new `Paginator` object
initialize = function(client, by = "limit_offset", limit_param = NULL,
offset_param = NULL, limit = NULL, chunk = NULL,
page_param = NULL, per_page_param = NULL, progress = FALSE) {
## checks
if (!inherits(client, "HttpClient")) stop("'client' has to be an object of class 'HttpClient'",
call. = FALSE)
self$http_req <- client
if (by == "query_params") {
warning("by='query_params' has been changed to 'limit_offset'", call.=FALSE)
by <- "limit_offset"
}
if (!by %in% by_options) {
stop("'by' must be one of: ", paste0(by_options, collapse = ", "),
call. = FALSE)
}
self$by <- by
if (!missing(chunk)) {
assert(chunk, c("numeric", "integer"))
if (chunk < 1 || chunk %% 1 != 0) stop("'chunk' must be an integer and > 0")
}
self$chunk <- chunk
if (!missing(limit_param)) assert(limit_param, "character")
self$limit_param <- limit_param
if (!missing(offset_param)) assert(offset_param, "character")
self$offset_param <- offset_param
if (!missing(limit)) {
assert(limit, c("numeric", "integer"))
if (!is.null(chunk)) {
if (chunk %% 1 != 0) stop("'limit' must be an integer")
}
}
self$limit <- limit
assert(progress, "logical")
self$progress <- progress
if (!missing(page_param)) assert(page_param, "character")
self$page_param <- page_param
if (!missing(per_page_param)) assert(per_page_param, "character")
self$per_page_param <- per_page_param
if (self$by == "limit_offset") {
# calculate pagination values
private$offset_iters <- c(0, seq(from=0, to=fround(self$limit, 10),
by=self$chunk)[-1])
private$offset_args <- as.list(stats::setNames(private$offset_iters,
rep(self$offset_param, length(private$offset_iters))))
private$limit_chunks <- rep(self$chunk, length(private$offset_iters))
diffy <- self$limit - private$offset_iters[length(private$offset_iters)]
if (diffy != self$chunk) {
private$limit_chunks[length(private$limit_chunks)] <- diffy
}
}
if (self$by == "page_perpage") {
if (is.null(self$chunk)) {
self$chunk <- if (!is.null(self$per_page_param)) {
self$per_page_param
} else {
stop("if `per_page_param` is NULL, you must set `chunk`",
call.=FALSE)
}
}
private$offset_iters <- c(0, seq(from=0, to=fround(self$limit, 10),
by=self$chunk)[-1])
private$offset_args <- as.list(stats::setNames(1:length(private$offset_iters),
rep(self$page_param, length(private$offset_iters))))
if (!is.null(self$per_page_param)) {
pp <- as.list(stats::setNames(rep(self$chunk, length(private$offset_iters)),
rep(self$per_page_param, length(private$offset_iters))))
for (i in seq_along(pp)) private$offset_args[[i]] <- c(private$offset_args[i], pp[i])
private$offset_args <- unname(private$offset_args)
} else {
for (i in seq_along(private$offset_args)) private$offset_args[i] <- list(private$offset_args[i])
}
}
},
#' @description make a paginated GET request
get = function(path = NULL, query = list(), ...) {
private$page("get", path, query, ...)
},
#' @description make a paginated POST request
post = function(path = NULL, query = list(), body = NULL,
encode = "multipart", ...) {
private$page("post", path, query, body, encode, ...)
},
#' @description make a paginated PUT request
put = function(path = NULL, query = list(), body = NULL,
encode = "multipart", ...) {
private$page("put", path, query, body, encode, ...)
},
#' @description make a paginated PATCH request
patch = function(path = NULL, query = list(), body = NULL,
encode = "multipart", ...) {
private$page("patch", path, query, body, encode, ...)
},
#' @description make a paginated DELETE request
delete = function(path = NULL, query = list(), body = NULL,
encode = "multipart", ...) {
private$page("delete", path, query, body, encode, ...)
},
#' @description make a paginated HEAD request
#' @details not sure if this makes any sense or not yet
head = function(path = NULL, ...) {
private$page("head", path, ...)
},
#' @description list responses
#' @return a list of `HttpResponse` objects, empty list before requests made
responses = function() {
private$resps %||% list()
},
#' @description Get HTTP status codes for each response
#' @return numeric vector, empty numeric vector before requests made
status_code = function() {
vapply(private$resps, function(z) z$status_code, 1)
},
#' @description List HTTP status objects
#' @return a list of `http_code` objects, empty list before requests made
status = function() {
lapply(private$resps, function(z) z$status_http())
},
#' @description parse content
#' @param encoding (character) the encoding to use in parsing.
#' default:"UTF-8"
#' @return character vector, empty character vector before
#' requests made
parse = function(encoding = "UTF-8") {
vapply(private$resps, function(z) z$parse(encoding = encoding), "")
},
#' @description Get raw content for each response
#' @return raw list, empty list before requests made
content = function() {
lapply(private$resps, function(z) z$content)
},
#' @description curl request times
#' @return list of named numeric vectors, empty list before requests made
times = function() {
lapply(private$resps, function(z) z$times)
},
#' @description get the URL that would be sent (i.e., before executing
#' the request) the only things that change the URL are path and query
#' parameters; body and any curl options don't change the URL
#' @return URLs (character)
#' @examples \dontrun{
#' cli <- HttpClient$new(url = "https://api.crossref.org")
#' cc <- Paginator$new(client = cli, limit_param = "rows",
#' offset_param = "offset", limit = 50, chunk = 10)
#' cc$url_fetch('works')
#' cc$url_fetch('works', query = list(query = "NSF"))
#' }
url_fetch = function(path = NULL, query = list()) {
urls <- c()
for (i in seq_along(private$offset_iters)) {
off <- private$offset_args[i]
off[self$limit_param] <- private$limit_chunks[i]
urls[i] <- self$http_req$url_fetch(path, query = ccp(c(query, off)))
}
return(urls)
}
),
private = list(
offset_iters = NULL,
offset_args = NULL,
limit_chunks = NULL,
resps = NULL,
page = function(method, path, query, body, encode, ...) {
tmp <- list()
if (self$progress) {
pb <- utils::txtProgressBar(min = 0, max = length(private$offset_iters),
initial = 0, style = 3)
on.exit(close(pb), add = TRUE)
}
if (self$by == "limit_offset") {
for (i in seq_along(private$offset_iters)) {
if (self$progress) utils::setTxtProgressBar(pb, i)
off <- private$offset_args[i]
off[self$limit_param] <- private$limit_chunks[i]
tmp[[i]] <- switch(
method,
get = self$http_req$get(path, query = ccp(c(query, off)), ...),
post = self$http_req$post(path, query = ccp(c(query, off)),
body = body, encode = encode, ...),
put = self$http_req$put(path, query = ccp(c(query, off)),
body = body, encode = encode, ...),
patch = self$http_req$patch(path, query = ccp(c(query, off)),
body = body, encode = encode, ...),
delete = self$http_req$delete(path, query = ccp(c(query, off)),
body = body, encode = encode, ...),
head = self$http_req$head(path, ...)
)
# cat("\n")
}
}
if (self$by == "page_perpage") {
for (i in seq_along(private$offset_iters)) {
if (self$progress) utils::setTxtProgressBar(pb, i)
off <- private$offset_args[[i]]
tmp[[i]] <- switch(
method,
get = self$http_req$get(path, query = ccp(c(query, off)), ...),
post = self$http_req$post(path, query = ccp(c(query, off)),
body = body, encode = encode, ...),
put = self$http_req$put(path, query = ccp(c(query, off)),
body = body, encode = encode, ...),
patch = self$http_req$patch(path, query = ccp(c(query, off)),
body = body, encode = encode, ...),
delete = self$http_req$delete(path, query = ccp(c(query, off)),
body = body, encode = encode, ...),
head = self$http_req$head(path, ...)
)
# cat("\n")
}
}
private$resps <- tmp
# message("OK\n")
}
)
)
# sttrim <- function(str) {
# gsub("^\\s+|\\s+$", "", str)
# }
# parse_links <- function(w) {
# if (is.null(w)) {
# NULL
# } else {
# if (inherits(w, "character")) {
# links <- sttrim(strsplit(w, ",")[[1]])
# lapply(links, each_link)
# } else {
# nms <- sapply(w, "[[", "name")
# tmp <- unlist(w[nms %in% "next"])
# grep("http", tmp, value = TRUE)
# }
# }
# }
# each_link <- function(z) {
# tmp <- sttrim(strsplit(z, ";")[[1]])
# nm <- gsub("\"|(rel)|=", "", tmp[2])
# url <- gsub("^<|>$", "", tmp[1])
# list(name = nm, url = url)
# }
|