Skip to content

Commit

Permalink
feat: content_render() can take a variant_key argument to render …
Browse files Browse the repository at this point in the history
…non-default variants. (#289)
  • Loading branch information
toph-allen authored Sep 3, 2024
1 parent b63b8db commit bfefb00
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
13 changes: 9 additions & 4 deletions R/content.R
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,7 @@ get_content_permissions <- function(content, add_owner = TRUE) {
#' notebooks, R Markdown reports).
#'
#' @param content The content item you wish to render.
#' @param variant_key If a variant key is provided, render that variant. Otherwise, render the default variant.
#' @return A [VariantTask] object that can be used to track completion of the render.
#'
#' @examples
Expand All @@ -978,16 +979,20 @@ get_content_permissions <- function(content, add_owner = TRUE) {
#' }
#'
#' @export
content_render <- function(content) {
content_render <- function(content, variant_key = NULL) {
scoped_experimental_silence()
validate_R6_class(content, "Content")
if (!content$is_rendered) {
stop(glue::glue("Render not supported for application mode: {content$content$app_mode}. Did you mean content_restart()?"))
}
render_task <- content$default_variant$render()
render_task$task_id <- render_task$id
if (is.null(variant_key)) {
target_variant <- get_variant(content, "default")
} else {
target_variant <- get_variant(content, variant_key)
}
render_task <- target_variant$render()

ContentTask$new(connect = content$get_connect(), content = content$get_content(), task = render_task)
VariantTask$new(connect = content$connect, content = content$content, key = target_variant$key, task = render_task)
}

#' Restart a content item.
Expand Down
4 changes: 3 additions & 1 deletion man/content_render.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 20 additions & 3 deletions tests/testthat/__api__/applications/951bf3ad/variants.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,26 @@
"email_viewers": false,
"email_all": false,
"created_time": "2024-07-12T20:52:12.285315Z",
"rendering_id": 3056607,
"render_time": "2024-07-18T20:17:32.664077Z",
"render_duration": 29772822831,
"rendering_id": 3078821,
"render_time": "2024-08-02T20:11:35.180593Z",
"render_duration": 40625279137,
"visibility": "public",
"owner_id": 0
},
{
"id": 6666,
"app_id": 51871,
"key": "SECOND",
"bundle_id": 124935,
"is_default": false,
"name": "variant2",
"email_collaborators": false,
"email_viewers": false,
"email_all": false,
"created_time": "2024-07-12T20:52:12.285315Z",
"rendering_id": 3078821,
"render_time": "2024-08-02T20:11:35.180593Z",
"render_duration": 40625279137,
"visibility": "public",
"owner_id": 0
}
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/__api__/variants/6666/render-38c41e-POST.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"id": "variant2_task_id",
"user_id": 336,
"status": [

],
"result": null,
"finished": false,
"code": 0,
"error": "",
"last_status": 0
}
12 changes: 12 additions & 0 deletions tests/testthat/test-content.R
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,20 @@ with_mock_api({
render_task <- content_render(x)
expect_equal(render_task$task[["id"]], "v9XYo7OKkAQJPraI")
expect_equal(render_task$connect, client)
# TODO think about how to get variant key into response
# expect_equal(render_task$variant_key, "WrEKKa77")
})

test_that("content_render() can render a non-default variant", {
client <- Connect$new(server = "https://connect.example", api_key = "not-a-key")
x <- content_item(client, "951bf3ad-82d0-4bca-bba8-9b27e35c49fa")
render_task <- content_render(x, variant_key = "SECOND")
expect_equal(render_task$task[["id"]], "variant2_task_id")
expect_equal(render_task$connect, client)
# TODO think about how to get variant key into response
})


test_that("content_render() raises an error when called on interactive content", {
client <- Connect$new(server = "http://connect.example", api_key = "not-a-key")
x <- content_item(client, "8f37d6e0-3395-4a2c-aa6a-d7f2fe1babd0")
Expand Down

0 comments on commit bfefb00

Please sign in to comment.