Skip to content

Commit

Permalink
Merge pull request #51 from ijlyttle/master
Browse files Browse the repository at this point in the history
Pagination, thanks to Ian Lyttle!
  • Loading branch information
brendan-r committed Feb 5, 2017
2 parents b2eb7e9 + a758634 commit 866f03b
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 12 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Imports:
Suggests:
testthat,
knitr,
rmarkdown
rmarkdown,
purrr
VignetteBuilder: knitr
RoxygenNote: 5.0.1
71 changes: 61 additions & 10 deletions R/boxr_misc.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#' Obtain a data.frame describing the contents of a box.com folder
#'
#' @param dir_id The box.com id for the folder that you'd like to query
#' @param limit Maximum number of entries to retrieve per query-page
#' @param max Maximum number of entries to retrieve in total
#'
#' @return A data.frame describing the contents of the the folder specified by
#' \code{dir_id}. Non recursive.
Expand All @@ -13,26 +15,75 @@
#' examining the contents of local directories.
#'
#' @export
box_ls <- function(dir_id = box_getwd()) {
box_ls <- function(dir_id = box_getwd(), limit = 100, max = Inf) {

# maybe some logic here to check that limit <= 1000

checkAuth()

req <- httr::GET(
paste0(
"https://api.box.com/2.0/folders/",
box_id(dir_id),
"/items?fields=modified_at,content_modified_at,name,id,type,sha1,size,",
"owned_by,path_collection,description&limit=1000"
),
httr::config(token = getOption("boxr.token"))
url_root <- "https://api.box.com/2.0"

url <- httr::parse_url(
paste(url_root, "folders", box_id(dir_id), "items", sep = "/")
)

fields <- c("modified_at" ,"content_modified_at", "name", "id", "type",
"sha1" ,"size", "owned_by", "path_collection", "description")

url$query <- list(
fields = paste(fields, collapse = ","),
limit = limit
)

out <- box_pagination(url, max = max)

out <- httr::content(req)$entries
class(out) <- "boxr_object_list"
return(out)
}


#' @keywords internal
box_pagination <- function(url, max = 200) {

out <- list()
next_page <- TRUE
page <- 1
n_so_far <- 0

while (next_page) {

limit <- url$query$limit

url$query$offset <- (page - 1) * limit

req <- httr::GET(
url,
httr::config(token = getOption("boxr.token"))
)

if (req$status_code == 404) {
message("box.com indicates that no results were found")
return()
}

httr::stop_for_status(req)

resp <- httr::content(req)
n_req <- length(resp$entries)
n_so_far <- n_so_far + n_req
total <- resp$total_count

if (!n_so_far < total | n_so_far >= max)
next_page <- FALSE

out <- c(out, resp$entries)
page <- page + 1
}

return(out)
}


#' Get/Set Default box.com directory/folder
#'
#' @description Providing analgous functionality for the jbase \bold{\code{R}}
Expand Down
6 changes: 5 additions & 1 deletion man/box_ls.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions tests/testthat/test_12_ls.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
context("Listing remote files")

# function to create a file in a local directory
#
# @param x numeric, number of the file
# @param dir character, name of the directory
#
fn_create <- function(x, dir){

name <- paste0("file_", formatC(x, width = 4, flag = "0"), ".txt")
filename <- file.path(dir_ls, name)

writeLines("test file", filename)
}

# given a boxr_object_list, return a vector of names
#
# @param b boxr_object_list
#
get_filenames <- function(b){
b %>%
as.data.frame() %>%
dplyr::arrange(name) %>%
`[[`("name")
}

test_that("file listing works", {

skip_on_cran()
boxr:::skip_on_travis()

# (Re)create local dir structure
boxr:::create_test_dir()

# create ls directory
dir_ls <- file.path("test_dir", "ls")
dir.create(dir_ls)

# create files in the ls directory
purrr::walk(seq_len(10), fn_create, dir_ls)

files_local <- list.files(dir_ls)

# push the local directory
box_push(0, "test_dir", overwrite = TRUE, delete = TRUE)

# get the id of the remote ld directory
id_ls <-
box_ls(0) %>%
as.data.frame() %>%
dplyr::filter(name == "ls") %>%
dplyr::select(id) %>%
`[[`(1) %>%
as.numeric()

# get the listing without pagination
files_box <-
box_ls(id_ls) %>%
get_filenames()

# get the listing with pagination
files_box_page <-
box_ls(id_ls, limit = 5) %>%
get_filenames()

# test
expect_identical(files_box, files_local)
expect_identical(files_box_page, files_local)

})

0 comments on commit 866f03b

Please sign in to comment.