Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: content_render() can take a variant_key argument to render non-default variants. #289

Merged
merged 5 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions R/content.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#'
#' @family R6 classes
#' @export
Content <- R6::R6Class(

Check warning on line 7 in R/content.R

View workflow job for this annotation

GitHub Actions / lint

file=R/content.R,line=7,col=1,[object_name_linter] Variable and function name style should match snake_case or symbols.
"Content",
public = list(
#' @field connect An R6 Connect object
Expand Down Expand Up @@ -45,7 +45,7 @@
#' @param bundle_id The bundle identifer.
#' @param filename Where to write the result.
#' @param overwrite Overwrite an existing filename.
bundle_download = function(bundle_id, filename = tempfile(pattern = "bundle", fileext = ".tar.gz"), overwrite = FALSE) {

Check warning on line 48 in R/content.R

View workflow job for this annotation

GitHub Actions / lint

file=R/content.R,line=48,col=101,[line_length_linter] Lines should not be more than 100 characters. This line is 124 characters.
url <- v1_url("content", self$get_content()$guid, "bundles", bundle_id, "download")
self$get_connect()$GET(url, httr::write_disk(filename, overwrite = overwrite), parser = "raw")
return(filename)
Expand Down Expand Up @@ -967,6 +967,7 @@
#' 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 @@
#' }
#'
#' @export
content_render <- function(content) {
content_render <- function(content, variant_key = NULL) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do this, you don't need the if below.

Suggested change
content_render <- function(content, variant_key = NULL) {
content_render <- function(content, variant_key = "default") {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I realize, but I included the if statement because I would rather have NULL represent the "no variant key provided" case than "default" — that feels more… idiomatically expected to me.

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
Loading