Skip to content

Commit

Permalink
add logical to prefetch cloud urls
Browse files Browse the repository at this point in the history
  • Loading branch information
dblodgett-usgs committed May 17, 2022
1 parent ea895fb commit c6950c3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
22 changes: 21 additions & 1 deletion R/item_list_files.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#'
#' @template manipulate_item
#' @param recursive (logical) List files recursively. Default: \code{FALSE}
#' @param fetch_cloud_urls (logical) fetch a tokenized cloud download URLs? Default: \code{TRUE}
#' This option will take slightly longer but the `url` attribute of the returned list will
#' work for direct file downloads or use with pther applications and libraries.
#'
#' @return
#' A data.frame with columns fname, size, url, and facet.
Expand Down Expand Up @@ -47,7 +50,8 @@
#' item_list_files(id = '56562348e4b071e7ea53e09d', recursive = FALSE) # default
#' item_list_files(id = '56562348e4b071e7ea53e09d', recursive = TRUE)
#' }
item_list_files = function(sb_id, recursive = FALSE, ..., session=current_session()){
item_list_files = function(sb_id, recursive = FALSE, fetch_cloud_urls = TRUE, ...,
session=current_session()){

session_val(session)

Expand Down Expand Up @@ -113,6 +117,22 @@ item_list_files = function(sb_id, recursive = FALSE, ..., session=current_sessio
files[[i]]$title, "")
cloud[[i]]$useForPreview <- ifelse(!is.null(files[[i]]$useForPreview),
files[[i]]$useForPreview, FALSE)

if(fetch_cloud_urls) {

if(!exists("gql")) gql <- httr::handle(url = pkg.env$graphql_url)

message("retrieving S3 URL")

out[i, 'url'] = get_cloud_download_url(
data.frame(cuid = cloud[[i]]$cuid,
key = cloud[[i]]$key,
title = cloud[[i]]$title,
useForPreview = cloud[[i]]$useForPreview),
gql)[[1]]$getS3DownloadUrl$downloadUri[1]

}

}
}

Expand Down
12 changes: 11 additions & 1 deletion man/item_list_files.Rd

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

3 changes: 3 additions & 0 deletions tests/testthat/test-auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ test_that("item creation, identifiers, and file upload works", {

expect_true(attr(files, "cloud")[[1]]$cuid != "")

expect_true(grepl("https://prod-is-usgs-sb-prod-content.s3.us-west-2.amazonaws.com",
files$url))

unlink(dl_files)

mess <- capture_messages(dl_files <- item_file_download(item, dest_dir = dir_name))
Expand Down

0 comments on commit c6950c3

Please sign in to comment.