Skip to content

Commit

Permalink
Merge branch 'main' into mthomson/pkgdown_search
Browse files Browse the repository at this point in the history
  • Loading branch information
toph-allen authored Oct 8, 2024
2 parents 927f4c2 + ccf4f1e commit 3453eff
Show file tree
Hide file tree
Showing 103 changed files with 1,622 additions and 181 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Imports:
fs,
glue,
httr,
mime,
jsonlite,
lifecycle,
magrittr,
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export(dashboard_url_chr)
export(delete_bundle)
export(delete_image)
export(delete_tag)
export(delete_thumbnail)
export(delete_vanity_url)
export(deploy)
export(deploy_current)
Expand All @@ -84,6 +85,7 @@ export(get_oauth_credentials)
export(get_procs)
export(get_tag_data)
export(get_tags)
export(get_thumbnail)
export(get_timezones)
export(get_usage_shiny)
export(get_usage_static)
Expand All @@ -97,6 +99,7 @@ export(get_variant_schedule)
export(get_variants)
export(groups_create_remote)
export(has_image)
export(has_thumbnail)
export(page_cursor)
export(page_offset)
export(poll_task)
Expand Down Expand Up @@ -127,6 +130,7 @@ export(set_schedule_semimonth)
export(set_schedule_week)
export(set_schedule_weekday)
export(set_schedule_year)
export(set_thumbnail)
export(set_vanity_url)
export(swap_vanity_url)
export(tbl_connect)
Expand Down
19 changes: 19 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
# connectapi (development version)

# Unreleased

## New features

- New functions `set_thumbnail()`, `get_thumbnail()`, `delete_thumbnail()` and
`has_thumbnail()` let you interact with content thumbnails, replacing older
`*_image()` functions. (#294, #304)

## Lifecycle changes

### Newly deprecated

- `set_image_path()`, `set_image_url()`, and `set_image_webshot()` have been
deprecated and will be removed in a future update. They have been replaced by
`set_thumbnail()`, which works both with local file paths and remote URLs to
images. Likewise, `has_image()` and `delete_image()` have been deprecated in
favor of `has_thumbnail()` and `delete_thumbnail()`. (#294, #304)

## Minor improvements and fixes

- Upgrade `pkgdown` to bootstrap 5 to enable search (@fh-mthomson, #275)

# connectapi 0.3.0
Expand Down
8 changes: 7 additions & 1 deletion R/connect.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,13 @@ Connect <- R6::R6Class(
#' @description Build a URL relative to the API root
#' @param ... path segments
api_url = function(...) {
paste(self$server, "__api__", ..., sep = "/")
self$server_url("__api__", ...)
},

#' @description Build a URL relative to the server root
#' @param ... path segments
server_url = function(...) {
paste(self$server, ..., sep = "/")
},

#' @description General wrapper around `httr` verbs
Expand Down
166 changes: 0 additions & 166 deletions R/deploy.R
Original file line number Diff line number Diff line change
Expand Up @@ -399,172 +399,6 @@ deploy_current <- function(content) {
return(ContentTask$new(connect = content$get_connect(), content = content, task = res))
}


#' Get the Content Image
#'
#' \lifecycle{experimental}
#' `get_image` saves the content image to the given path (default: temp file).
#' `delete_image` removes the image (optionally saving to the given path)
#' `has_image` returns whether the content has an image
#'
#' @param content A content object
#' @param path optional. The path to the image on disk
#'
#' @rdname get_image
#' @family content functions
#' @export
get_image <- function(content, path = NULL) {
warn_experimental("get_image")
validate_R6_class(content, "Content")
guid <- content$get_content()$guid

con <- content$get_connect()

res <- con$GET(
unversioned_url("applications", guid, "image"),
parser = NULL
)

if (httr::status_code(res) == 204) {
return(NA)
}

# guess file extension
if (is.null(path)) {
ct <- httr::headers(res)$`content-type`
if (grepl("^image/", ct)) {
# just strip off 'image/'
ext <- substr(ct, 7, nchar(ct))
path <- fs::file_temp(pattern = "content_image_", ext = ext)
} else {
# try png
warning(glue::glue("Could not infer file extension from content type: {ct}. Using '.png'"))
path <- fs::file_temp(pattern = "content_image_", ext = ".png")
}
}

writeBin(httr::content(res, as = "raw"), path)

return(fs::as_fs_path(path))
}

#' @rdname get_image
#' @export
delete_image <- function(content, path = NULL) {
warn_experimental("delete_image")
validate_R6_class(content, "Content")
guid <- content$get_content()$guid

con <- content$get_connect()

if (!is.null(path)) {
scoped_experimental_silence()
get_image(content, path)
}

res <- con$DELETE(unversioned_url("applications", guid, "image"))

return(content)
}

#' @rdname get_image
#' @export
has_image <- function(content) {
warn_experimental("has_image")
validate_R6_class(content, "Content")
guid <- content$get_content()$guid

con <- content$get_connect()

res <- con$GET(unversioned_url("applications", guid, "image"), parser = NULL)

httr::status_code(res) != 204
}

#' Set the Content Image
#'
#' \lifecycle{experimental}
#'
#' Set the Content Image using a variety of methods.
#'
#' NOTE: `set_image_webshot()` requires [webshot2::webshot()], but currently
#' skips and warns for any content that requires authentication until the
#' `webshot2` package supports authentication.
#'
#' @param content A content object
#' @param path The path to an image on disk
#' @param url The url for an image
#' @param ... Additional arguments passed on to [webshot2::webshot()]
#'
#' @rdname set_image
#' @family content functions
#' @export
set_image_path <- function(content, path) {
warn_experimental("set_image_path")
validate_R6_class(content, "Content")
guid <- content$get_content()$guid

con <- content$get_connect()

res <- con$POST(
path = unversioned_url("applications", guid, "image"),
body = httr::upload_file(path)
)

# return the input (in case it inherits more than just Content)
content
}

#' @rdname set_image
#' @export
set_image_url <- function(content, url) {
warn_experimental("set_image_url")
validate_R6_class(content, "Content")
parsed_url <- httr::parse_url(url)
imgfile <- fs::file_temp(pattern = "image", ext = fs::path_ext(parsed_url[["path"]]))
httr::GET(url, httr::write_disk(imgfile))

set_image_path(content = content, path = imgfile)
}

#' @rdname set_image
#' @export
set_image_webshot <- function(content, ...) {
warn_experimental("set_image_webshot")
validate_R6_class(content, "Content")
imgfile <- fs::file_temp(pattern = "webshot", ext = ".png")

rlang::check_installed("webshot2", "to take screenshots of applications")
content_details <- content$get_content_remote()

# check if it is possible to take the webshot
if (content_details$access_type != "all") {
warning(glue::glue(
"WARNING: unable to take webshot for content ",
"'{content_details$guid}' because authentication is not possible yet. ",
"Set access_type='all' to proceed."
))
return(content)
}

# default args
args <- rlang::list2(...)

if (!"cliprect" %in% names(args)) {
args["cliprect"] <- "viewport"
}


rlang::inject(webshot2::webshot(
url = content_details$content_url,
file = imgfile,
!!!args
))

set_image_path(content = content, path = imgfile)
}


#' Set the Vanity URL
#'
#' Sets the Vanity URL for a piece of content.
Expand Down
Loading

0 comments on commit 3453eff

Please sign in to comment.