Skip to content

Commit

Permalink
Added bindings to list registered directories.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Sep 14, 2024
1 parent 6f24668 commit d3e4e1e
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 4 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: SewerRat
Version: 0.2.7
Date: 2024-08-22
Version: 0.2.8
Date: 2024-09-14
Title: Client for the SewerRat API
Description:
Search metadata files across a shared filesystem via the SewerRat API.
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export(deregister)
export(listFiles)
export(listRegisteredDirectories)
export(query)
export(register)
export(retrieveDirectory)
Expand Down
55 changes: 55 additions & 0 deletions R/listRegisteredDirectories.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#' List registered directories
#'
#' List the directories that were registered in SewerRat.
#'
#' @param url String containing the URL of the SewerRat REST API.
#' @param user String containing the name of a user.
#' If supplied, results are filtered to directories registered by this user.
#' If \code{TRUE}, this is set to the current user.
#'
#' @author Aaron Lun
#'
#' @return List of named lists, where each inner list corresponds to a registered directory and contains:
#' \itemize{
#' \item \code{path}, string containing a path to the directory.
#' \item \code{user}, string containing the name of the user who registered this directory.
#' \item \code{time}, numeric scalar specifying the the Unix epoch time when this directory was registered.
#' \item \code{names}, a character vector containing the base names of the JSON files to be indexed.
#' }
#'
#' @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 the registered directories:
#' listRegisteredDirectories(url=info$url)
#' listRegisteredDirectories(url=info$url, user=TRUE)
#'
#' @export
#' @import httr2
listRegisteredDirectories <- function(url, user=NULL) {
url <- paste0(url, "/registered")

if (isTRUE(user)) {
user <- Sys.info()["user"]
}
if (!is.null(user) && !isFALSE(user)) {
url <- paste0(url, "?user=", user)
}
req <- request(url)
req <- handle_error(req)
res <- req_perform(req)
resp_body_json(res)
}

2 changes: 1 addition & 1 deletion R/startSewerRat.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#' startSewerRat() # initialize a new instance.
#'
#' @export
startSewerRat <- function(db=tempfile(fileext=".sqlite3"), port=NULL, wait = 1, version = "1.0.6", overwrite = FALSE) {
startSewerRat <- function(db=tempfile(fileext=".sqlite3"), port=NULL, wait = 1, version = "1.0.9", overwrite = FALSE) {
if (!is.null(running$active)) {
return(list(new=FALSE, port=running$port, url=assemble_url(running$port)))
}
Expand Down
50 changes: 50 additions & 0 deletions man/listRegisteredDirectories.Rd

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

2 changes: 1 addition & 1 deletion man/startSewerRat.Rd

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

21 changes: 21 additions & 0 deletions tests/testthat/test-list.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,25 @@ test_that("listing works as expected", {
expect_identical(sort(listFiles(mydir, url=info$url, forceRemote=TRUE)), sort(c("diet/metadata.json", "metadata.json")))
})

test_that("listRegisteredDirectories works as expected", {
all <- listRegisteredDirectories(info$url)
expect_true(length(all) > 0L)

found <- FALSE
for (x in all) {
if (x$path == mydir) {
found <- TRUE
expect_identical(x$names, list("metadata.json"))
}
}
expect_true(found)

# Forcing filtering.
filtered <- listRegisteredDirectories(info$url, user=TRUE)
expect_identical(all, filtered)

filtered <- listRegisteredDirectories(info$url, user=paste0(all[[1]]$user, "asdasdasdasdasd"))
expect_identical(length(filtered), 0L)
})

deregister(mydir, url=info$url)

0 comments on commit d3e4e1e

Please sign in to comment.