Skip to content

Commit 4f829eb

Browse files
committed
return "connect_user"/"connect_users" objects
get_user now returns list of `"connect_user"` objects instead of a data frame. as.data.frame and as_tibble methods have been added.
1 parent 8e8f85d commit 4f829eb

File tree

6 files changed

+52
-9
lines changed

6 files changed

+52
-9
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ S3method(api_build,op_base_connect)
77
S3method(api_build,op_head)
88
S3method(as.data.frame,connect_integration_list)
99
S3method(as.data.frame,connect_list_hits)
10+
S3method(as.data.frame,connect_users)
1011
S3method(as.data.frame,tbl_connect)
1112
S3method(as_tibble,connect_integration_list)
1213
S3method(as_tibble,connect_list_hits)
14+
S3method(as_tibble,connect_users)
1315
S3method(connect_vars,op_base)
1416
S3method(connect_vars,op_single)
1517
S3method(connect_vars,tbl_connect)

R/connect.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ Connect <- R6::R6Class(
497497
#' @description Get user details.
498498
#' @param guid The user GUID.
499499
user = function(guid) {
500-
self$GET(v1_url("users", guid))
500+
prepend_class(self$GET(v1_url("users", guid)), "connect_user")
501501
},
502502

503503
#' @description Get users.
@@ -527,7 +527,11 @@ Connect <- R6::R6Class(
527527
user_role = user_role,
528528
account_status = account_status
529529
)
530-
self$GET(path, query = query)
530+
res <- self$GET(path, query = query)
531+
if (!is.null(res$results)) {
532+
res$results <- lapply(res$results, prepend_class, "connect_user")
533+
}
534+
res
531535
},
532536

533537
#' @description Get remote users.

R/get.R

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
#' value (boolean OR). When `NULL` (the default), results are not filtered.
1414

1515
#'
16-
#' @return
17-
#' A tibble with the following columns:
16+
#' @return For `get_users`, a list of objects of type `"connect_user"` which
17+
#' contain the following information:
1818
#'
1919
#' * `email`: The user's email
2020
#' * `username`: The user's username
@@ -33,6 +33,8 @@
3333
#' * `locked`: Whether or not the user is locked
3434
#' * `guid`: The user's GUID, or unique identifier, in UUID RFC4122 format
3535
#'
36+
#' For `get_user`, a single `"connect_user"` object.
37+
#'
3638
#' @details
3739
#' Please see https://docs.posit.co/connect/api/#get-/v1/users for more information.
3840
#'
@@ -49,6 +51,10 @@
4951
#'
5052
#' # Get all users who are administrators or publishers
5153
#' get_users(client, user_role = c("administrator", "publisher"))
54+
#'
55+
#' # Convert to a data frame
56+
#' users_list <- get_users(client)
57+
#' as.data.frame(users_list)
5258
#' }
5359
#'
5460
#' @export
@@ -72,10 +78,23 @@ get_users <- function(
7278
),
7379
limit = limit
7480
)
81+
return(prepend_class(res, "connect_users"))
82+
}
7583

76-
out <- parse_connectapi_typed(res, connectapi_ptypes$users)
84+
#' @param guid user GUID
85+
#' @rdname get_users
86+
get_user <- function(src, guid) {
87+
src$user(guid)
88+
}
7789

78-
return(out)
90+
#' @export
91+
as.data.frame.connect_users <- function(x, ...) {
92+
parse_connectapi_typed(x, connectapi_ptypes$users)
93+
}
94+
95+
#' @export
96+
as_tibble.connect_users <- function(x, ...) {
97+
parse_connectapi_typed(x, connectapi_ptypes$users)
7998
}
8099

81100
#' Get information about content on the Posit Connect server

R/utils.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,9 @@ message_if_not_testing <- function(...) {
256256
message(...)
257257
}
258258
}
259+
260+
# Prepends a new class to a given objec
261+
prepend_class <- function(x, class) {
262+
class(x) <- c(class, class(x))
263+
x
264+
}

man/get_users.Rd

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-users.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ with_mock_api({
22
test_that("we can retrieve the users list", {
33
con <- Connect$new(server = "https://connect.example", api_key = "fake")
44
users <- get_users(con)
5-
expect_s3_class(users, "data.frame")
6-
expect_equal(nrow(users), 3)
5+
expect_s3_class(users, "connect_users")
6+
expect_equal(length(users), 3)
77
})
88

99
test_that("we can retrieve a user by id", {

0 commit comments

Comments
 (0)