diff --git a/DESCRIPTION b/DESCRIPTION index 9856fd4..8d08b20 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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. diff --git a/NAMESPACE b/NAMESPACE index d866e29..6963680 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ export(deregister) export(listFiles) +export(listRegisteredDirectories) export(query) export(register) export(retrieveDirectory) diff --git a/R/listRegisteredDirectories.R b/R/listRegisteredDirectories.R new file mode 100644 index 0000000..d16eb4d --- /dev/null +++ b/R/listRegisteredDirectories.R @@ -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) +} + diff --git a/R/startSewerRat.R b/R/startSewerRat.R index 210a37b..86d2b73 100644 --- a/R/startSewerRat.R +++ b/R/startSewerRat.R @@ -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))) } diff --git a/man/listRegisteredDirectories.Rd b/man/listRegisteredDirectories.Rd new file mode 100644 index 0000000..c20a6d8 --- /dev/null +++ b/man/listRegisteredDirectories.Rd @@ -0,0 +1,50 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/listRegisteredDirectories.R +\name{listRegisteredDirectories} +\alias{listRegisteredDirectories} +\title{List registered directories} +\usage{ +listRegisteredDirectories(url, user = NULL) +} +\arguments{ +\item{url}{String containing the URL of the SewerRat REST API.} + +\item{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.} +} +\value{ +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. +} +} +\description{ +List the directories that were registered in SewerRat. +} +\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) + +} +\author{ +Aaron Lun +} diff --git a/man/startSewerRat.Rd b/man/startSewerRat.Rd index 7d78ce0..62101bb 100644 --- a/man/startSewerRat.Rd +++ b/man/startSewerRat.Rd @@ -9,7 +9,7 @@ startSewerRat( db = tempfile(fileext = ".sqlite3"), port = NULL, wait = 1, - version = "1.0.6", + version = "1.0.9", overwrite = FALSE ) diff --git a/tests/testthat/test-list.R b/tests/testthat/test-list.R index 3c435e6..37bca8f 100644 --- a/tests/testthat/test-list.R +++ b/tests/testthat/test-list.R @@ -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)