Skip to content

Commit

Permalink
refactor: update helper for box id (#264)
Browse files Browse the repository at this point in the history
* build helper and test

* bump version and add to NEWS

* switch to as_box_id()

* remove box_id(), {bit64} dependency

* add to exported functions

* add to more exported functions

* add to remaining exported functions

* add trailing newline

* fix missed conflict
  • Loading branch information
ijlyttle authored Feb 3, 2024
1 parent 94b66e8 commit 0da6358
Show file tree
Hide file tree
Showing 18 changed files with 135 additions and 25 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: boxr
Type: Package
Title: Interface for the 'Box.com API'
Version: 0.3.6.9012
Version: 0.3.6.9013
Authors@R: c(
person("Brendan", "Rocks", email = "[email protected]",
role = c("aut")),
Expand Down Expand Up @@ -34,7 +34,6 @@ Description: An R interface for the remote file hosting service 'Box'
License: MIT + file LICENSE
Imports:
assertthat,
bit64,
dplyr,
digest,
fs,
Expand All @@ -52,6 +51,7 @@ Imports:
lifecycle,
jsonlite,
jose,
cli,
withr
Suggests:
clipr (>= 0.3.0),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

## Internal

* update with internal helper for Box ids. (#90)

* update minimum version of rio package to reflect newer treatment of JSON files. (#261)

* remove unused internal function, removing dependency on httpuv package. (#259)
Expand Down
2 changes: 1 addition & 1 deletion R/boxr__internal_get.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ handle_file_id <- function(obj) {
file_id <- obj
}
# Return -- if valid
return(box_id(file_id))
return(as_box_id(file_id))
}


Expand Down
30 changes: 24 additions & 6 deletions R/boxr__internal_misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,33 @@ box_filename <- function(x) {
x
}

# ref: https://rlang.r-lib.org/reference/topic-error-call.html
as_box_id <- function(x, arg = rlang::caller_arg(x),
call = rlang::caller_env()) {

# a box_id is a string that contains only digits

# some defaults are NULL; we just pass these through
if (is.null(x)) {
return(NULL)
}

# Validate ids supplied
box_id <- function(x) {
if (!is.null(x) && any(is.na(bit64::as.integer64(x))))
stop("box.com API ids must be (coercible to) 64-bit integers")
if (!is.null(x))
return(as.character(bit64::as.integer64(x)))
id <- as.character(x)

has_only_digits <- stringr::str_detect(id, "^\\d+$")

if (!all(has_only_digits)) {
cli::cli_abort(
message = "{.arg {arg}} must contain only digits",
class = "boxr_id",
call = call
)
}

id
}


# helper to identify void values
is_void <- function(x) {
is.null(x) ||
Expand Down
13 changes: 10 additions & 3 deletions R/boxr__internal_update_upload.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#' @keywords internal
#'
box_upload_new <- function(dir_id, file, pb = FALSE) {

dir_id <- as_box_id(dir_id)

httr::RETRY(
"POST",
"https://upload.box.com/api/2.0/files/content",
Expand All @@ -29,7 +32,7 @@ box_upload_new <- function(dir_id, file, pb = FALSE) {
list(
attributes =
paste0(
'{"name": "', basename(file), '", "parent": {"id":"', box_id(dir_id)
'{"name": "', basename(file), '", "parent": {"id":"', dir_id
,'"}}'
),
file = httr::upload_file(file)
Expand All @@ -41,9 +44,13 @@ box_upload_new <- function(dir_id, file, pb = FALSE) {
#' @rdname box_upload_new
#' @keywords internal
box_update_file <- function(file_id, file, dir_id, pb = FALSE) {

dir_id <- as_box_id(dir_id)
file_id <- as_box_id(file_id)

httr::RETRY(
"POST",
paste0("https://upload.box.com/api/2.0/files/", box_id(file_id),
paste0("https://upload.box.com/api/2.0/files/", file_id,
"/content"),
get_token(),
encode = "multipart",
Expand All @@ -53,7 +60,7 @@ box_update_file <- function(file_id, file, dir_id, pb = FALSE) {
list(
attributes =
paste0(
'{"name": "', basename(file), '", "parent": {"id":"', box_id(dir_id)
'{"name": "', basename(file), '", "parent": {"id":"', dir_id
,'"}}'
),
file = httr::upload_file(file)
Expand Down
2 changes: 2 additions & 0 deletions R/boxr_add_description.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#'
#' @export
box_add_description <- function(file_id, description) {

file_id <- as_box_id(file_id)
file_id <- handle_file_id(file_id)

req <- httr::RETRY(
Expand Down
15 changes: 14 additions & 1 deletion R/boxr_collab.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ box_collab_create <- function(dir_id = NULL, user_id = NULL,
file_id = NULL, group_id = NULL, login = NULL,
role = "editor", can_view_path = FALSE) {

dir_id <- as_box_id(dir_id)
user_id <- as_box_id(user_id)
file_id <- as_box_id(file_id)
group_id <- as_box_id(group_id)

# if login is provided, ignore user_id
if (!is_void(login)) {
user_id <- NULL
Expand Down Expand Up @@ -177,6 +182,9 @@ box_dir_invite <- function(dir_id, user_id, login = NULL, role = "viewer",
can_view_path = FALSE) {
.Deprecated("box_collab_create")

dir_id <- as_box_id(dir_id)
user_id <- as_box_id(user_id)

# if login is provided, ignore user_id
if (!is_void(login)) {
user_id <- NULL
Expand Down Expand Up @@ -218,7 +226,10 @@ box_dir_invite <- function(dir_id, user_id, login = NULL, role = "viewer",
#' @export
#'
box_collab_get <- function(dir_id = NULL, file_id = NULL) {


dir_id <- as_box_id(dir_id)
file_id <- as_box_id(file_id)

# detect item type for API call
item_id <- dir_id %|0|% file_id

Expand Down Expand Up @@ -275,6 +286,8 @@ box_collab_get <- function(dir_id = NULL, file_id = NULL) {
#'
box_collab_delete <- function(collab_id) {

collab_id <- as_box_id(collab_id)

url <- glue::glue("https://api.box.com/2.0/collaborations/{collab_id}")

resp <- httr::RETRY(
Expand Down
3 changes: 3 additions & 0 deletions R/boxr_comment.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ box_comment_create <- function(file_id = NULL, message, comment_id = NULL) {
# https://developer.box.com/reference/post-comments/

checkAuth()
file_id <- as_box_id(file_id)

item <- comment_item_helper(file_id, comment_id)

Expand Down Expand Up @@ -76,6 +77,8 @@ box_comment_create <- function(file_id = NULL, message, comment_id = NULL) {
#'
box_comment_get <- function(file_id) {

file_id <- as_box_id(file_id)

resp <- httr::RETRY(
"GET",
glue::glue("https://api.box.com/2.0/files/{file_id}/comments"),
Expand Down
18 changes: 18 additions & 0 deletions R/boxr_delete_restore.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@
#'
#' @export
box_delete_file <- function(file_id) {

file_id <- as_box_id(file_id)

boxDeleteFile(file_id)
invisible(NULL)
}

#' @rdname box_delete_file
#' @export
box_restore_file <- function(file_id) {

file_id <- as_box_id(file_id)

req <- httr::RETRY(
"POST",
paste0(
Expand All @@ -54,6 +60,9 @@ box_restore_file <- function(file_id) {
#' @rdname box_delete_file
#' @export
box_delete_folder <- function(dir_id) {

dir_id <- as_box_id(dir_id)

boxDeleteFolder(dir_id)
invisible(NULL)
}
Expand All @@ -62,6 +71,9 @@ box_delete_folder <- function(dir_id) {
#' @rdname box_delete_file
#' @export
box_restore_folder <- function(dir_id) {

dir_id <- as_box_id(dir_id)

req <- httr::RETRY(
"POST",
paste0(
Expand Down Expand Up @@ -91,6 +103,9 @@ box_restore_folder <- function(dir_id) {

#' @keywords internal
boxDeleteFile <- function(file_id) {

file_id <- as_box_id(file_id)

req <- httr::RETRY(
"DELETE",
paste0(
Expand All @@ -113,6 +128,9 @@ boxDeleteFile <- function(file_id) {

#' @keywords internal
boxDeleteFolder <- function(dir_id) {

dir_id <- as_box_id(dir_id)

req <- httr::RETRY(
"DELETE",
paste0(
Expand Down
3 changes: 3 additions & 0 deletions R/boxr_dir_verbs.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@
#' @export
box_fetch <- function(dir_id = box_getwd(), local_dir = getwd(),
recursive = TRUE, overwrite = FALSE, delete = FALSE) {

checkAuth()
dir_id <- as_box_id(dir_id)

t1 <- Sys.time()

Expand Down Expand Up @@ -164,6 +166,7 @@ box_push <- function(dir_id = box_getwd(), local_dir = getwd(),
ignore_dots = TRUE, overwrite = FALSE, delete = FALSE) {

checkAuth()
dir_id <- as_box_id(dir_id)

t1 <- Sys.time()

Expand Down
8 changes: 7 additions & 1 deletion R/boxr_file_versions.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
#' @export
#'
box_version_history <- function(file_id) {


file_id <- as_box_id(file_id)

content <- box_version_api(file_id)

if (is_void(content)) {
Expand Down Expand Up @@ -61,6 +63,8 @@ box_version_history <- function(file_id) {
#'
box_previous_versions <- function(file_id) {

file_id <- as_box_id(file_id)

lifecycle::deprecate_soft(
"3.6.0",
what = "boxr::box_previous_versions()",
Expand All @@ -72,6 +76,8 @@ box_previous_versions <- function(file_id) {
# internal function to support superseding
prev_versions <- function(file_id) {

file_id <- as_box_id(file_id)

entries <- box_version_api(file_id)

# The box API isn't very helpful if there are no previous versions. If this
Expand Down
25 changes: 18 additions & 7 deletions R/boxr_misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#' @export
box_ls <- function(dir_id = box_getwd(), limit = 100, max = Inf, fields = NULL) {

dir_id <- as_box_id(dir_id)

if (limit > 1000) {
warning("The maximum limit is 1000; box_ls is using 1000.")
limit <- 1000
Expand All @@ -28,7 +30,7 @@ box_ls <- function(dir_id = box_getwd(), limit = 100, max = Inf, fields = NULL)
url_root <- "https://api.box.com/2.0"

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

fields_all <-
Expand Down Expand Up @@ -127,14 +129,15 @@ box_pagination <- function(url, max){
#' @export
#'
box_setwd <- function(dir_id) {

checkAuth()


checkAuth()
dir_id <- as_box_id(dir_id)

req <- httr::RETRY(
"GET",
paste0(
"https://api.box.com/2.0/folders/",
box_id(dir_id)
dir_id
),
get_token(),
terminate_on = box_terminal_http_codes()
Expand Down Expand Up @@ -258,22 +261,26 @@ boxr_options <- function() {
box_dir_create <- function(dir_name, parent_dir_id = box_getwd()) {

checkAuth()
parent_dir_id <- as_box_id(parent_dir_id)

add_folder_ref_class(httr::content(
boxDirCreate(dir_name, box_id(parent_dir_id))
boxDirCreate(dir_name, parent_dir_id)
))
}

#' @keywords internal
boxDirCreate <- function(dir_name, parent_dir_id = box_getwd()) {

parent_dir_id <- as_box_id(parent_dir_id)

httr::RETRY(
"POST",
"https://api.box.com/2.0/folders/",
get_token(),
encode = "multipart",
body =
paste0(
'{"name":"', dir_name, '", "parent": {"id": "', box_id(parent_dir_id),
'{"name":"', dir_name, '", "parent": {"id": "', parent_dir_id,
'"}}'
),
terminate_on = box_terminal_http_codes()
Expand All @@ -299,6 +306,10 @@ boxDirCreate <- function(dir_name, parent_dir_id = box_getwd()) {
#' @export
#'
box_browse <- function(dir_id = NULL, file_id = NULL) {

dir_id <- as_box_id(dir_id)
file_id <- as_box_id(file_id)

item <- collab_item_helper(dir_id, file_id)
utils::browseURL(glue::glue("https://app.box.com/{item$type}/{item$id}"))
}
Loading

0 comments on commit 0da6358

Please sign in to comment.