-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a listing function with associated tests.
- Loading branch information
Showing
4 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#' List files in a registered directory | ||
#' | ||
#' List files in a registered directory or a subdirectory thereof. | ||
#' This will call the REST API if the caller is not on the same filesystem. | ||
#' | ||
#' @param path String containing the path to the registered (sub)directory. | ||
#' @param url String containing the URL to the SewerRat REST API. | ||
#' | ||
#' @author Aaron Lun | ||
#' | ||
#' @return Character vector of relative paths of files in \code{path}. | ||
#' | ||
#' @examples | ||
#' # Starting up an example SewerRat service: | ||
#' info <- startSewerRat() | ||
#' | ||
#' # Mocking up a directory of stuff to query. | ||
#' mydir <- tempfile() | ||
#' dir.create(mydir) | ||
#' write(file=file.path(mydir, "metadata.json"), '{ "first": "Aaron", "last": "Lun" }') | ||
#' dir.create(file.path(mydir, "diet")) | ||
#' write(file=file.path(mydir, "diet", "metadata.json"), | ||
#' '{ "meal": "lunch", "ingredients": "water" }') | ||
#' | ||
#' # Registering it: | ||
#' register(mydir, "metadata.json", url=info$url) | ||
#' | ||
#' # List files: | ||
#' listFiles(mydir, url=info$url) | ||
#' listFiles(paste0(mydir, "/diet"), url=info$url) | ||
#' | ||
#' # Forcing remote access. | ||
#' listFiles(mydir, url=info$url, forceRemote=TRUE) | ||
#' | ||
#' @export | ||
#' @import httr2 | ||
listFiles <- function(path, url, forceRemote=FALSE) { | ||
if (!forceRemote && file.exists(path)) { | ||
list.files(path, recursive=TRUE, all.files=TRUE) | ||
} else { | ||
req <- request(paste0(url, "/list?path=", URLencode(path, reserved=TRUE), "&recursive=true")) | ||
req <- handle_error(req) | ||
res <- req_perform(req) | ||
unlist(resp_body_json(res)) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Test the listing function. | ||
# library(testthat); library(SewerRat); source("test-list.R") | ||
|
||
# Starting up an example SewerRat service: | ||
info <- startSewerRat() | ||
|
||
# Mocking up a directory of stuff to query. | ||
mydir <- tempfile() | ||
dir.create(mydir) | ||
write(file=file.path(mydir, "metadata.json"), '{ "first": "Aaron", "last": "Lun" }') | ||
dir.create(file.path(mydir, "diet")) | ||
write(file=file.path(mydir, "diet", "metadata.json"), | ||
'{ "meal": "lunch", "ingredients": "water" }') | ||
|
||
# Registering it: | ||
register(mydir, "metadata.json", url=info$url) | ||
|
||
test_that("listing works as expected", { | ||
expect_identical(sort(listFiles(mydir, url=info$url)), sort(c("diet/metadata.json", "metadata.json"))) | ||
expect_identical(sort(listFiles(paste0(mydir, "/diet"), url=info$url)), "metadata.json") | ||
|
||
# Forcing remote access. | ||
expect_identical(sort(listFiles(mydir, url=info$url, forceRemote=TRUE)), sort(c("diet/metadata.json", "metadata.json"))) | ||
}) | ||
|
||
deregister(mydir, url=info$url) |