Skip to content

Commit 79f0491

Browse files
committed
Adapt behaviour of type argument (closes #53)
1 parent 0411423 commit 79f0491

File tree

4 files changed

+44
-38
lines changed

4 files changed

+44
-38
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Imports:
1616
curl,
1717
dplyr (>= 1.1.4),
1818
httr2,
19+
lifecycle,
1920
magrittr,
2021
rlang,
2122
rstudioapi,

R/assets.R

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,8 @@
77
# nolint end
88
#'
99
#' @param workspace_uuid UUID of workspace
10-
#' @param type List of asset types to return. Default returns all types;
11-
#' "document", "folder" and "link".
12-
#'
13-
#' List must be empty (default, returns all asset types), or length 1 (e.g.
14-
#' `list("document")`). This is a temporary measure while a bug in the
15-
#' underlying API is outstanding (see
16-
#' [objr#53](https://github.com/ScotGovAnalysis/objr/issues/53)).
17-
#'
10+
#' @param type Asset type to filter results by. Either "document", "folder" or
11+
#' "link". Default returns all asset types.
1812
#' @inheritParams objr
1913
#' @inheritParams workspaces
2014
#'
@@ -25,35 +19,38 @@
2519
#' @export
2620

2721
assets <- function(workspace_uuid,
28-
type = list(),
22+
type = NULL,
2923
page = NULL,
3024
size = NULL,
3125
use_proxy = FALSE) {
3226

33-
check_list(type)
34-
35-
if (length(type) > 1) {
36-
cli::cli_abort(c(
37-
"x" = "{.arg type} must be a list of length 0 or 1, not {length(type)}.",
38-
"i" = paste(
39-
"There is currently a bug in the underlying API preventing",
40-
"users from selecting more than one asset type. See",
41-
"{.href [objr#53](https://github.com/ScotGovAnalysis/objr/issues/53)}",
42-
"for more information."
43-
),
44-
"i" = paste("To return all assets, use `type = list()` (default)."),
45-
"i" = paste("To return one asset type only, e.g. documents, use",
46-
"`type = list(\"document\")`. See `?assets` for all",
47-
"options.")
48-
))
27+
if (rlang::is_list(type)) {
28+
lifecycle::deprecate_warn(
29+
when = "0.2.0",
30+
what = "assets(type = 'must be a character string')",
31+
details = "All asset types are returned.",
32+
always = TRUE
33+
)
34+
type <- NULL
4935
}
5036

51-
type <- paste(toupper(type), collapse = "|")
37+
if (!is.null(type)) {
38+
if (!rlang::is_string(type)) {
39+
cli::cli_abort(
40+
"`type` must be a string, length 1."
41+
)
42+
}
43+
if (!type %in% c("document", "folder", "link")) {
44+
cli::cli_abort(
45+
"`type` must be one of {.str document}, {.str folder} or {.str link}."
46+
)
47+
}
48+
}
5249

5350
response <- objr(
5451
endpoint = "assets",
5552
url_query = list(workspaceUuid = workspace_uuid,
56-
type = type,
53+
type = toupper(type),
5754
page = page,
5855
size = size),
5956
use_proxy = use_proxy

tests/testthat/test-assets.R

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
11
# assets ----
22

3-
test_that("Error if `type` length > 1", {
3+
test_that("Error if `type` invalid", {
44
expect_error(assets(workspace_uuid = "test_workspace",
5-
type = list("document", "link")))
5+
type = c("document", "link")))
6+
expect_error(assets(workspace_uuid = "test_workspace",
7+
type = "x"))
68
})
79

810
without_internet({
911

1012
test_that("Valid request created", {
1113

1214
expect_GET(
13-
assets(workspace_uuid = "test_workspace", type = list()),
15+
assets(workspace_uuid = "test_workspace"),
1416
paste0("https://secure.objectiveconnect.co.uk/publicapi/1/assets?",
15-
"workspaceUuid=test_workspace&type=")
17+
"workspaceUuid=test_workspace")
1618
)
1719

1820
expect_GET(
1921
assets(workspace_uuid = "test_workspace",
20-
type = list("folder")),
22+
type = "folder"),
2123
paste0("https://secure.objectiveconnect.co.uk/publicapi/1/assets?",
2224
"workspaceUuid=test_workspace&type=FOLDER")
2325
)
2426

25-
expect_error(
26-
assets(workspace_uuid = "test_workspace",
27-
type = "folder")
27+
})
28+
29+
test_that("Deprecated `type` list format handled correctly", {
30+
31+
expect_GET(
32+
suppressWarnings(
33+
assets(workspace_uuid = "test_workspace",
34+
type = list("folder"))
35+
),
36+
paste0("https://secure.objectiveconnect.co.uk/publicapi/1/assets?",
37+
"workspaceUuid=test_workspace")
2838
)
2939

3040
})
@@ -43,15 +53,13 @@ with_mock_api({
4353
})
4454

4555
test_that("Results filtered by type", {
46-
4756
expect_equal(
4857
unique(
4958
assets(workspace_uuid = "test_workspace_uuid",
50-
type = list("folder"))$asset_type
59+
type = "folder")$asset_type
5160
),
5261
"FOLDER"
5362
)
54-
5563
})
5664

5765
})

0 commit comments

Comments
 (0)