Skip to content

Commit ea0a140

Browse files
authored
feat: add get_packages() and get_content_packages() functions (#374)
1 parent c3ef91e commit ea0a140

19 files changed

+426
-3
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export(filter_tag_tree_id)
7272
export(get_audit_logs)
7373
export(get_bundles)
7474
export(get_content)
75+
export(get_content_packages)
7576
export(get_content_permissions)
7677
export(get_content_tags)
7778
export(get_environment)
@@ -87,6 +88,7 @@ export(get_log)
8788
export(get_my_permission)
8889
export(get_oauth_content_credentials)
8990
export(get_oauth_credentials)
91+
export(get_packages)
9092
export(get_procs)
9193
export(get_runtime_caches)
9294
export(get_runtimes)

NEWS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# connectapi (development version)
22

3+
## New features
4+
5+
- New `get_packages()` function to get a data frame of all packages on a Connect
6+
server. (#374)
7+
- New `get_content_packages()` function to get a data frame of all package
8+
dependencies for a content item. (#374)
9+
310
# connectapi 0.6.0
411

512
## New features

R/connect.R

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ Connect <- R6::R6Class(
815815
schedules = function(start = Sys.time(), end = Sys.time() + 60 * 60 * 24 * 7, detailed = FALSE) {
816816
warn_experimental("schedules")
817817
url <- v1_url("experimental", "schedules")
818-
query_params <- rlang::list2(
818+
query_params <- list(
819819
detailed = tolower(detailed),
820820
start = datetime_to_rfc3339(start),
821821
end = datetime_to_rfc3339(end)
@@ -824,6 +824,22 @@ Connect <- R6::R6Class(
824824
return(res[["schedules"]])
825825
},
826826

827+
# packages --------------------------------------------------
828+
829+
#' @description Get packages.
830+
#' @param name The package name to filter by.
831+
#' @param page_number The page number.
832+
#' @param page_size Page size, max 500.
833+
packages = function(name = NULL, page_number = 1, page_size = 500) {
834+
url <- v1_url("packages")
835+
query_params <- list(
836+
name = name,
837+
page_number = page_number,
838+
page_size = page_size
839+
)
840+
self$GET(url, query = query_params)
841+
},
842+
827843
# misc utilities --------------------------------------------
828844

829845
#' @description Get documentation.

R/content.R

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ Content <- R6::R6Class(
297297
)
298298
)
299299
},
300-
#' @description Adjust Git repository.
300+
#' @description Adjust Git repository
301301
#' @param repository Git repository URL
302302
#' @param branch Git repository branch
303303
#' @param subdirectory Git repository directory
@@ -312,6 +312,10 @@ Content <- R6::R6Class(
312312
)
313313
)
314314
},
315+
#' @description Get package dependencies
316+
packages = function() {
317+
self$connect$GET(v1_url("content", self$content$guid, "packages"))
318+
},
315319
#' @description Print this object.
316320
#' @param ... Unused.
317321
print = function(...) {
@@ -1286,3 +1290,31 @@ content_restart <- function(content) {
12861290
# nolint end
12871291
invisible(NULL)
12881292
}
1293+
1294+
#' Package dependencies for a content item
1295+
#'
1296+
#' @description Get a data frame of package dependencies used by a content item.
1297+
#'
1298+
#' @param content A content item
1299+
#'
1300+
#' @return A data frame with the following columns:
1301+
#'
1302+
#' - `language` : Language ecosystem the package belongs to (`r` or `python`)
1303+
#' - `name`: The package name
1304+
#' - `version`: The package version
1305+
#' - `hash`: For R packages, the package `DESCRIPTION` hash
1306+
#'
1307+
#' @examples
1308+
#' \dontrun{
1309+
#' client <- connect()
1310+
#' item <- content_item(client, "951bf3ad-82d0-4bca-bba8-9b27e35c49fa")
1311+
#' packages <- get_content_packages(item)
1312+
#' }
1313+
#'
1314+
#' @family packages functions
1315+
#' @export
1316+
get_content_packages <- function(content) {
1317+
error_if_less_than(content$connect$version, "2025.01.0")
1318+
res <- content$packages()
1319+
parse_connectapi_typed(res, connectapi_ptypes$content_packages)
1320+
}

R/get.R

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,72 @@ get_runtimes <- function(client, runtimes = NULL) {
742742
})
743743
}
744744

745+
#' All package dependencies on the server
746+
#'
747+
#' @description Get a data frame of all package dependencies used by content
748+
#' items on the server.
749+
#'
750+
#' The `page_size` and `limit` parameters are optional but may be useful during
751+
#' development, when you're iterating on some code that uses this function.
752+
#' Behind the scenes, Connect returns packages in pages, and with large or
753+
#' long-running servers, this can take a while. Setting a `limit` causes
754+
#' Connect to stop early, giving you incomplete data, but faster.
755+
#'
756+
#' @param src A `Connect` client object.
757+
#' @param name Optional package name to filter by. Python package are normalized
758+
#' during matching; R package names must match exactly.
759+
#' @param page_size Optional, max 500. Integer specifying page size for API
760+
#' paging.
761+
#' @param limit Optionally specify the maximum number of records to return.
762+
#'
763+
#' @return A data frame with the following columns:
764+
#'
765+
#' - `language`: Language ecosystem the package belongs to (`r` or `python`)
766+
#' - `language_version`: Version of R or Python used by the content
767+
#' - `name`: Package name
768+
#' - `version`: Package version
769+
#' - `hash`: Package description hash for R packages
770+
#' - `bundle_id`: Identifier for the bundle that depends on this package
771+
#' - `content_id`: Numeric identifier for the content that depends on this
772+
#' package
773+
#' - `content_guid`: The unique identifier of the content item that depends on
774+
#' this package
775+
#'
776+
#' @examples
777+
#' \dontrun{
778+
#' client <- connect()
779+
#' packages <- get_packages(client)
780+
#' }
781+
#'
782+
#' @family packages functions
783+
#' @export
784+
get_packages <- function(src, name = NULL, page_size = 500, limit = Inf) {
785+
validate_R6_class(src, "Connect")
786+
error_if_less_than(src$version, "2024.11.0")
787+
res <- page_offset(
788+
src,
789+
src$packages(
790+
name = name,
791+
page_size = page_size
792+
),
793+
limit = limit
794+
)
795+
out <- parse_connectapi_typed(res, connectapi_ptypes$packages)
796+
797+
# Connect is standardizing on using `content_id` and `content_guid`.
798+
# Handle that name change now in a forward-compatible way.
799+
if ("app_id" %in% names(out)) {
800+
out$content_id <- out$app_id
801+
out$app_id <- NULL
802+
}
803+
if ("app_guid" %in% names(out)) {
804+
out$content_guid <- out$app_guid
805+
out$app_guid <- NULL
806+
}
807+
808+
out
809+
}
810+
745811
#' Get all vanity URLs
746812
#'
747813
#' Get a table of all vanity URLs on the server. Requires administrator

R/ptype.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,5 +246,21 @@ connectapi_ptypes <- list(
246246
source = NA_character_,
247247
timestamp = NA_datetime_,
248248
data = NA_character_
249+
),
250+
packages = tibble::tibble(
251+
language = NA_character_,
252+
language_version = NA_character_,
253+
name = NA_character_,
254+
version = NA_character_,
255+
hash = NA_character_,
256+
bundle_id = NA_character_,
257+
app_id = NA_character_,
258+
app_guid = NA_character_,
259+
),
260+
content_packages = tibble::tibble(
261+
language = NA_character_,
262+
name = NA_character_,
263+
version = NA_character_,
264+
hash = NA_character_
249265
)
250266
)

man/Content.Rd

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

man/ContentTask.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/EnvironmentR6.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/PositConnect.Rd

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)