Skip to content

Commit

Permalink
fix #142 with box_current_version()
Browse files Browse the repository at this point in the history
  • Loading branch information
nathancday committed Sep 6, 2020
1 parent 14d2ae6 commit 2982c23
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 36 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ export(box_add_description)
export(box_auth)
export(box_auth_on_attach)
export(box_auth_service)
<<<<<<< HEAD
export(box_collab_create)
export(box_collab_delete)
export(box_collab_get)
=======
export(box_current_version)
>>>>>>> fix #142 with box_current_version()
export(box_delete_file)
export(box_delete_folder)
export(box_dir_create)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
- `box_collab_create()` and `box_collab_get()` each return the (list-based) response from the Box API.
If you prefer to work with data frames, these return-objects each have `as.data.frame()` and `as_tibble()` methods.

* new function `box_current_version()` is similar to to get the current version of a file.

* uses `httr::RETRY()` for API requests to handle momentary issues with network connectivity. Thanks @jameslamb and @chircollab!

## Bug Fixes
Expand Down
77 changes: 54 additions & 23 deletions R/boxr_file_versions.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#' Get details of previous versions of a Box file
#' Get details about versions of a Box file
#'
#' Box explicitly versions files; this function returns a
#' Box uses file versioning, but the API does not explicitly provide version numbers.
#' These functions use `modified_date` as a proxy and allow programatic version access
#' in `box_dl()` and `box_read()`
#'
#' * `box_previous_version()` returns a
#' `data.frame` containing information on a file's previous
#' versions on Box. No information about the current version of the file is
#' returned. If the version of a file is one, then NULL is returned invisibly
#' versions on Box, the column `file_version_id` can be passed to `version_id`
#' in `box_dl()` and `box_read()`.
#' If the version of a file is one, then NULL is returned invisibly
#' along with a helpful message.
#'
#' The returned `data.frame` contains a variable, `file_version_id`,
#' which you can use with [box_dl()].
#' * `box_current_version()` returns a `integer`, starting from 1, which can be passed
#' to `version_no = ` in `box_dl()` and `box_read()`.
#'
#' @inheritParams box_dl
#'
Expand All @@ -20,30 +25,21 @@
#'
#' <https://developers.box.com/docs/#files-view-versions-of-a-file>
#'
#' @seealso [box_dl()]
#' @seealso [box_dl(), box_read()]
#'
#' @export
#'
box_previous_versions <- function(file_id) {
checkAuth()

req <- httr::RETRY(
"GET",
paste0(
"https://api.box.com/2.0/files/",
file_id, "/versions"
),
get_token(),
terminate_on = box_terminal_http_codes()
)

# The box API isn't very helpful if there are no previous versions. If this

req <- box_version_api(file_id)

# The box API isn't very helpful if there are no previous versions. If this
# is the case, let the user know and exit.
if (is_void(httr::content(req)[["entries"]])) {
if (is_void(req[["entries"]])) {
message("No previous versions for this file found.")
return(invisible(NULL))
}

# Munge it into a data.frame
d <- suppressWarnings(
purrr::map_df(
Expand All @@ -67,7 +63,7 @@ box_previous_versions <- function(file_id) {
# loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooool
#
# The best you can do is probably modified at
message("Versions inferred from file modification dates. The box.com API ",
message("Version ordering inferred from file modification dates. The box.com API ",
"does not provide explicit version information.")

d <- d[order(d$modified_at),]
Expand All @@ -83,3 +79,38 @@ box_previous_versions <- function(file_id) {

d
}

#'
#' @rdname box_previous_versions
#' @export
box_current_version <- function(file_id) {

file_id <- 682127082014 # ver 3
file_id <- 682162782067 # ver1

req <- box_version_api(file_id)

ver <- req[["total_count"]] + 1

message("Box file ", file_id, "is version ", ver)

ver
}
#' Wrap and resuse
#' at_internal
box_version_api <- function(file_id) {
checkAuth()

req <- httr::RETRY(
"GET",
paste0(
"https://api.box.com/2.0/files/",
file_id, "/versions"
),
get_token(),
terminate_on = box_terminal_http_codes()
)

httr::content(req)
}

25 changes: 17 additions & 8 deletions man/box_previous_versions.Rd

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

10 changes: 5 additions & 5 deletions tests/testthat/test_08_versions.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ test_that("Versions work", {

expect_message(box_previous_versions(v_file_id), "No previous versions")

expect_message(box_current_version(v_file_id), "version 1")

# Upload subsequent versions
for (v in 2:n_versions) {
writeLines(contents[v], tf)
Expand All @@ -36,15 +38,16 @@ test_that("Versions work", {
paste0("Attempting to upload new version \\(V", v, "\\)")
)


# Do they have the right class?
expect_is(ul, "boxr_file_reference")

# Has the file_id remained constant?
expect_equal(ul$id, v_file_id)

# Is the version being incremented?
expect_equal(box_current_version(v_file_id), v)
}


# Downloading
# Are there n_versions-1 previous versions of the file?
v_df <- box_previous_versions(v_file_id)
Expand Down Expand Up @@ -74,7 +77,4 @@ test_that("Versions work", {
# Does the remote file have the right contents?
expect_true(readLines(dl) == contents[v])
}

})


0 comments on commit 2982c23

Please sign in to comment.