Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update helper for box id #264

Merged
merged 10 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
file_id <- obj
}
# Return -- if valid
return(box_id(file_id))
return(as_box_id(file_id))

Check warning on line 17 in R/boxr__internal_get.R

View check run for this annotation

Codecov / codecov/patch

R/boxr__internal_get.R#L17

Added line #L17 was not covered by tests
}


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 @@
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)

Check warning on line 30 in R/boxr__internal_misc.R

View check run for this annotation

Codecov / codecov/patch

R/boxr__internal_misc.R#L30

Added line #L30 was not covered by tests
}

# 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)

Check warning on line 22 in R/boxr__internal_update_upload.R

View check run for this annotation

Codecov / codecov/patch

R/boxr__internal_update_upload.R#L22

Added line #L22 was not covered by tests

httr::RETRY(
"POST",
"https://upload.box.com/api/2.0/files/content",
Expand All @@ -29,7 +32,7 @@
list(
attributes =
paste0(
'{"name": "', basename(file), '", "parent": {"id":"', box_id(dir_id)
'{"name": "', basename(file), '", "parent": {"id":"', dir_id

Check warning on line 35 in R/boxr__internal_update_upload.R

View check run for this annotation

Codecov / codecov/patch

R/boxr__internal_update_upload.R#L35

Added line #L35 was not covered by tests
,'"}}'
),
file = httr::upload_file(file)
Expand All @@ -41,9 +44,13 @@
#' @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)

Check warning on line 49 in R/boxr__internal_update_upload.R

View check run for this annotation

Codecov / codecov/patch

R/boxr__internal_update_upload.R#L48-L49

Added lines #L48 - L49 were not covered by tests

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,

Check warning on line 53 in R/boxr__internal_update_upload.R

View check run for this annotation

Codecov / codecov/patch

R/boxr__internal_update_upload.R#L53

Added line #L53 was not covered by tests
"/content"),
get_token(),
encode = "multipart",
Expand All @@ -53,7 +60,7 @@
list(
attributes =
paste0(
'{"name": "', basename(file), '", "parent": {"id":"', box_id(dir_id)
'{"name": "', basename(file), '", "parent": {"id":"', dir_id

Check warning on line 63 in R/boxr__internal_update_upload.R

View check run for this annotation

Codecov / codecov/patch

R/boxr__internal_update_upload.R#L63

Added line #L63 was not covered by tests
,'"}}'
),
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)

Check warning on line 16 in R/boxr_add_description.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_add_description.R#L16

Added line #L16 was not covered by tests
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 @@
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)

Check warning on line 72 in R/boxr_collab.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_collab.R#L69-L72

Added lines #L69 - L72 were not covered by tests

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

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

Check warning on line 186 in R/boxr_collab.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_collab.R#L185-L186

Added lines #L185 - L186 were not covered by tests

# if login is provided, ignore user_id
if (!is_void(login)) {
user_id <- NULL
Expand Down Expand Up @@ -218,7 +226,10 @@
#' @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)

Check warning on line 231 in R/boxr_collab.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_collab.R#L230-L231

Added lines #L230 - L231 were not covered by tests

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

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

collab_id <- as_box_id(collab_id)

Check warning on line 289 in R/boxr_collab.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_collab.R#L289

Added line #L289 was not covered by tests

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 @@
# https://developer.box.com/reference/post-comments/

checkAuth()
file_id <- as_box_id(file_id)

Check warning on line 44 in R/boxr_comment.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_comment.R#L44

Added line #L44 was not covered by tests

item <- comment_item_helper(file_id, comment_id)

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

file_id <- as_box_id(file_id)

Check warning on line 80 in R/boxr_comment.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_comment.R#L80

Added line #L80 was not covered by tests

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)

Check warning on line 28 in R/boxr_delete_restore.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_delete_restore.R#L28

Added line #L28 was not covered by tests

boxDeleteFile(file_id)
invisible(NULL)
}

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

file_id <- as_box_id(file_id)

Check warning on line 38 in R/boxr_delete_restore.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_delete_restore.R#L38

Added line #L38 was not covered by tests

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

dir_id <- as_box_id(dir_id)

Check warning on line 64 in R/boxr_delete_restore.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_delete_restore.R#L64

Added line #L64 was not covered by tests

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

dir_id <- as_box_id(dir_id)

Check warning on line 75 in R/boxr_delete_restore.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_delete_restore.R#L75

Added line #L75 was not covered by tests

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

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

file_id <- as_box_id(file_id)

Check warning on line 107 in R/boxr_delete_restore.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_delete_restore.R#L107

Added line #L107 was not covered by tests

req <- httr::RETRY(
"DELETE",
paste0(
Expand All @@ -113,6 +128,9 @@

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

dir_id <- as_box_id(dir_id)

Check warning on line 132 in R/boxr_delete_restore.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_delete_restore.R#L132

Added line #L132 was not covered by tests

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)

Check warning on line 68 in R/boxr_dir_verbs.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_dir_verbs.R#L68

Added line #L68 was not covered by tests

t1 <- Sys.time()

Expand Down Expand Up @@ -164,6 +166,7 @@
ignore_dots = TRUE, overwrite = FALSE, delete = FALSE) {

checkAuth()
dir_id <- as_box_id(dir_id)

Check warning on line 169 in R/boxr_dir_verbs.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_dir_verbs.R#L169

Added line #L169 was not covered by tests

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)

Check warning on line 37 in R/boxr_file_versions.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_file_versions.R#L37

Added line #L37 was not covered by tests

content <- box_version_api(file_id)

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

file_id <- as_box_id(file_id)

Check warning on line 66 in R/boxr_file_versions.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_file_versions.R#L66

Added line #L66 was not covered by tests

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

file_id <- as_box_id(file_id)

Check warning on line 79 in R/boxr_file_versions.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_file_versions.R#L79

Added line #L79 was not covered by tests

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)

Check warning on line 21 in R/boxr_misc.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_misc.R#L21

Added line #L21 was not covered by tests

if (limit > 1000) {
warning("The maximum limit is 1000; box_ls is using 1000.")
limit <- 1000
Expand All @@ -28,7 +30,7 @@
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 = "/")

Check warning on line 33 in R/boxr_misc.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_misc.R#L33

Added line #L33 was not covered by tests
)

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

checkAuth()


checkAuth()
dir_id <- as_box_id(dir_id)

Check warning on line 134 in R/boxr_misc.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_misc.R#L133-L134

Added lines #L133 - L134 were not covered by tests

req <- httr::RETRY(
"GET",
paste0(
"https://api.box.com/2.0/folders/",
box_id(dir_id)
dir_id

Check warning on line 140 in R/boxr_misc.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_misc.R#L140

Added line #L140 was not covered by tests
),
get_token(),
terminate_on = box_terminal_http_codes()
Expand Down Expand Up @@ -258,22 +261,26 @@
box_dir_create <- function(dir_name, parent_dir_id = box_getwd()) {

checkAuth()
parent_dir_id <- as_box_id(parent_dir_id)

Check warning on line 264 in R/boxr_misc.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_misc.R#L264

Added line #L264 was not covered by tests

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

Check warning on line 267 in R/boxr_misc.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_misc.R#L267

Added line #L267 was not covered by tests
))
}

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

parent_dir_id <- as_box_id(parent_dir_id)

Check warning on line 274 in R/boxr_misc.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_misc.R#L274

Added line #L274 was not covered by tests

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,

Check warning on line 283 in R/boxr_misc.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_misc.R#L283

Added line #L283 was not covered by tests
'"}}'
),
terminate_on = box_terminal_http_codes()
Expand All @@ -299,6 +306,10 @@
#' @export
#'
box_browse <- function(dir_id = NULL, file_id = NULL) {

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

Check warning on line 311 in R/boxr_misc.R

View check run for this annotation

Codecov / codecov/patch

R/boxr_misc.R#L310-L311

Added lines #L310 - L311 were not covered by tests

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