Skip to content

Commit

Permalink
Support more filters when listing registered directories.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Oct 28, 2024
1 parent 9644019 commit ca389ce
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 15 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.10
Date: 2024-10-18
Version: 0.2.11
Date: 2024-10-27
Title: Client for the SewerRat API
Description:
Search metadata files across a shared filesystem via the SewerRat API.
Expand Down
31 changes: 23 additions & 8 deletions R/listRegisteredDirectories.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
#'
#' @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 not \code{NULL}, results are filtered to directories registered by this user.
#' If \code{TRUE}, this is set to the current user.
#' @param contains String containing an absolute path.
#' If not \code{NULL}, results are filtered to directories that contain this path.
#' @param prefix String containing an absolute path or a prefix thereof.
#' If not \code{NULL}, results are filtered to directories that start with this string.
#'
#' @author Aaron Lun
#'
Expand Down Expand Up @@ -38,15 +42,26 @@
#'
#' @export
#' @import httr2
listRegisteredDirectories <- function(url, user=NULL) {
url <- paste0(url, "/registered")

if (isTRUE(user)) {
user <- Sys.info()["user"]
}
listRegisteredDirectories <- function(url, user=NULL, contains=NULL, prefix=NULL) {
query <- character(0)
if (!is.null(user) && !isFALSE(user)) {
url <- paste0(url, "?user=", user)
if (isTRUE(user)) {
user <- Sys.info()["user"]
}
query <- c(query, paste0("user=", user))
}
if (!is.null(contains)) {
query <- c(query, paste0("contains_path=", URLencode(contains, reserved=TRUE)))
}
if (!is.null(prefix)) {
query <- c(query, paste0("path_prefix=", URLencode(prefix, reserved=TRUE)))
}

url <- paste0(url, "/registered")
if (length(query)) {
url <- paste0(url, "?", paste(query, collapse="&"))
}

req <- request(url)
req <- handle_error(req)
res <- req_perform(req)
Expand Down
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.11", overwrite = FALSE) {
startSewerRat <- function(db=tempfile(fileext=".sqlite3"), port=NULL, wait = 1, version = "1.0.13", overwrite = FALSE) {
if (!is.null(running$active)) {
return(list(new=FALSE, port=running$port, url=assemble_url(running$port)))
}
Expand Down
10 changes: 8 additions & 2 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.

20 changes: 19 additions & 1 deletion tests/testthat/test-list.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,30 @@ test_that("listRegisteredDirectories works as expected", {
}
expect_true(found)

# Forcing filtering.
# Filtering by user.
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)

# Filter by contains.
filtered <- listRegisteredDirectories(info$url, contains=file.path(mydir, "diet"))
expect_identical(all, filtered)

filtered <- listRegisteredDirectories(info$url, contains=tempfile())
expect_identical(length(filtered), 0L)

# Filter by prefix.
filtered <- listRegisteredDirectories(info$url, prefix=dirname(mydir))
expect_identical(all, filtered)

filtered <- listRegisteredDirectories(info$url, prefix=paste0(dirname(mydir), "-asdasdad"))
expect_identical(length(filtered), 0L)

# Multiple filters work.
filtered <- listRegisteredDirectories(info$url, prefix=dirname(mydir), user=TRUE, contains=file.path(mydir, "diet"))
expect_identical(all, filtered)
})

deregister(mydir, url=info$url)

0 comments on commit ca389ce

Please sign in to comment.