Skip to content

Commit b79b311

Browse files
feat: add functions to get_runtime_caches() and delete_runtime_caches() (#318)
Co-authored-by: Barret Schloerke <[email protected]>
1 parent cd8291a commit b79b311

13 files changed

+254
-2
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export(dashboard_url)
5757
export(dashboard_url_chr)
5858
export(delete_bundle)
5959
export(delete_image)
60+
export(delete_runtime_cache)
6061
export(delete_tag)
6162
export(delete_thumbnail)
6263
export(delete_vanity_url)
@@ -83,6 +84,7 @@ export(get_jobs)
8384
export(get_my_permission)
8485
export(get_oauth_credentials)
8586
export(get_procs)
87+
export(get_runtime_caches)
8688
export(get_tag_data)
8789
export(get_tags)
8890
export(get_thumbnail)

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
- `groups_create_remote()` now has an `exact` parameter. Setting `exact` causes
1111
the function to consider only exact group name matches when searching for
1212
remote groups and checking for existing local groups. (#216)
13+
- New functions to let you view and delete runtime caches on a Connect server:
14+
`get_runtime_caches()`, `delete_runtime_cache()` (#312)
15+
1316

1417
## Lifecycle changes
1518

@@ -26,6 +29,8 @@
2629
- Upgrade `pkgdown` to bootstrap 5 to enable search (@fh-mthomson, #275)
2730
- The `get_timezones()` function now uses the `v1/timezones` endpoint if
2831
available. (#300)
32+
- `connect$DELETE()` now respects the `parser` argument rather than always using
33+
`NULL`.
2934

3035
# connectapi 0.3.0
3136

R/connect.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ Connect <- R6::R6Class(
196196
#' will be returned. Otherwise, the argument is forwarded to
197197
#' `httr::content(res, as = parser)`.
198198
DELETE = function(path, ..., url = self$api_url(path), parser = NULL) {
199-
self$request("DELETE", url, parser = NULL, ...)
199+
self$request("DELETE", url, parser = parser, ...)
200200
},
201201

202202
#' @description Perform an HTTP PATCH request of the named API path.

R/runtime-caches.R

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#' Get runtime caches
2+
#'
3+
#' View the runtime caches on a Connect server.
4+
#' Requires Administrator privileges.
5+
#'
6+
#' @param client A `Connect` object.
7+
#'
8+
#' @returns A tibble of runtime caches on the server, showing `language`,
9+
#' `version` and `image_name`. For Connect servers not using off-host execution,
10+
#' `image_name` is `"Local"`.
11+
#'
12+
#' @examples
13+
#' \dontrun{
14+
#' client <- connect()
15+
#' get_runtime_caches(client)
16+
#' }
17+
#'
18+
#' @family server management functions
19+
#' @seealso [delete_runtime_cache()]
20+
#' @export
21+
get_runtime_caches <- function(client) {
22+
validate_R6_class(client, "Connect")
23+
res <- client$GET(v1_url("system/caches/runtime"))
24+
purrr::map_dfr(res$caches, ~.x)
25+
}
26+
27+
#' Delete a runtime cache
28+
#'
29+
#' Delete a runtime cache from a Connect server.
30+
#' Requires Administrator privileges.
31+
#'
32+
#' @param client A `Connect` object.
33+
#' @param language The language of the cache, either "R" or "Python".
34+
#' @param version The version of the cache, e.g. "4.3.3".
35+
#' @param image_name Optional. The name of the off-host execution image for
36+
#' the cache, or "Local" (the default) for native execution caches.
37+
#' @param dry_run Optional, default `FALSE`. If true, perform a dry run of
38+
#' the deletion.
39+
#'
40+
#' @returns A `Task` object representing the deletion task. If `dry_run` is
41+
#' `TRUE`, returns `NULL` or throws an error if the deletion would fail.
42+
#'
43+
#' @examples
44+
#' \dontrun{
45+
#' client <- connect()
46+
#' task <- delete_runtime_cache(client, "R", "4.3.3")
47+
#' poll_task(task)
48+
#' }
49+
#'
50+
#' @family server management functions
51+
#' @seealso [get_runtime_caches()]
52+
#' @export
53+
delete_runtime_cache <- function(client, language, version, image_name = "Local", dry_run = FALSE) {
54+
res <- client$DELETE(
55+
path = v1_url("system/caches/runtime"),
56+
body = list(
57+
language = language,
58+
version = version,
59+
image_name = image_name,
60+
dry_run = dry_run
61+
),
62+
encode = "json",
63+
parser = "parsed"
64+
)
65+
66+
if (dry_run == TRUE) {
67+
message(glue::glue(
68+
"Runtime cache deletion dry run did not encounter any errors ",
69+
'(language = "{language}", version = "{version}", image_name = "{image_name}")'
70+
))
71+
return(invisible(NULL))
72+
}
73+
74+
task_data <- client$GET(v1_url("tasks", res$task_id))
75+
Task$new(client, task_data)
76+
}

_pkgdown.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ reference:
6969

7070
- title: "Other"
7171
desc: >
72-
Oher functions
72+
Other functions
7373
contents:
7474
- promote
7575
- connect
@@ -81,6 +81,7 @@ reference:
8181
- create_random_name
8282
- dashboard_url
8383
- dashboard_url_chr
84+
- delete_runtime_cache
8485

8586

8687
- title: "R6 classes"

man/delete_runtime_cache.Rd

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

man/get_runtime_caches.Rd

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"language": "Python",
3+
"version": "3.11.3",
4+
"image_name": "Local",
5+
"task_id": "eKm0RFxyzIKR3jnI"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"language": "Python",
3+
"version": "3.11.3",
4+
"image_name": "Local",
5+
"task_id": null
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
structure(
2+
list(
3+
request <- list(url = "__api__/v1/system/caches/runtime"),
4+
url = "__api__/v1/system/caches/runtime", status_code = 404L,
5+
content = charToRaw("{\"code\":4,\"error\":\"Cache does not exist\",\"payload\":null}")
6+
),
7+
class = "response"
8+
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"caches": [
3+
{
4+
"language": "R",
5+
"version": "4.3.0",
6+
"image_name": "Local"
7+
},
8+
{
9+
"language": "Python",
10+
"version": "3.11.3",
11+
"image_name": "Local"
12+
}
13+
]
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"id": "eKm0RFxyzIKR3jnI",
3+
"output": [
4+
"Deleting runtime cache..."
5+
],
6+
"result": null,
7+
"finished": false,
8+
"code": 0,
9+
"error": "",
10+
"last": 1
11+
}

tests/testthat/test-runtime-caches.R

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
with_mock_api({
2+
client <- connect(server = "https://connect.example", api_key = "fake")
3+
4+
test_that("get_runtime_caches() gets runtime caches", {
5+
caches <- get_runtime_caches(client)
6+
expect_equal(
7+
get_runtime_caches(client),
8+
tibble::tibble(
9+
language = c("R", "Python"),
10+
version = c("4.3.0", "3.11.3"),
11+
image_name = c("Local", "Local")
12+
)
13+
)
14+
})
15+
16+
test_that("delete_runtime_cache() prints message and returns NULL when dry_run == TRUE", {
17+
expect_message(
18+
res <- delete_runtime_cache(client, "Python", "3.11.3", dry_run = TRUE),
19+
paste0(
20+
"Runtime cache deletion dry run did not encounter any errors ",
21+
'\\(language = "Python", version = "3.11.3", image_name = "Local"\\)'
22+
)
23+
)
24+
expect_null(res)
25+
})
26+
27+
test_that("delete_runtime_cache() returns a task object when dry_run == FALSE", {
28+
res <- delete_runtime_cache(client, "Python", "3.11.3")
29+
expect_true(validate_R6_class(res, "Task"))
30+
expect_equal(res$task$id, "eKm0RFxyzIKR3jnI")
31+
})
32+
33+
test_that("delete_runtime_cache() on a nonexistent cache throws a 404 error", {
34+
expect_error(
35+
delete_runtime_cache(client, "Python", "3.11.2"),
36+
"request failed with Client error: \\(404\\) Not Found"
37+
)
38+
})
39+
})

0 commit comments

Comments
 (0)