Skip to content

Commit

Permalink
feat: assert that universe exists + list all universes for users
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed Oct 22, 2024
1 parent 492a550 commit 1c09129
Show file tree
Hide file tree
Showing 14 changed files with 216 additions and 94 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(add)
export(all_universes)
export(global_search)
export(r_version)
export(remove)
Expand All @@ -9,3 +10,4 @@ export(universe_all_packages)
export(universe_ls)
export(universe_one_package)
export(universe_search)
importFrom(rlang,`%||%`)
1 change: 1 addition & 0 deletions R/starchart-package.R → R/universe-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"_PACKAGE"

## usethis namespace: start
#' @importFrom rlang `%||%`
## usethis namespace: end
NULL
64 changes: 60 additions & 4 deletions R/universe.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' @family universe
universe_ls <- function(universe) {
assert_character(universe, "universe")
# TODO assert that universe is an universe
assert_universe(universe)

universe_query(
universe_url = sprintf("https://%s.r-universe.dev", universe),
Expand All @@ -34,7 +34,7 @@ universe_ls <- function(universe) {
#' @family universe
universe_all_packages <- function(universe, limit = 100L) {
assert_character(universe, "universe")
# TODO assert that universe is an universe
assert_universe(universe)

universe_query(
universe_url = sprintf("https://%s.r-universe.dev", universe),
Expand All @@ -57,8 +57,8 @@ universe_all_packages <- function(universe, limit = 100L) {
#' @family universe
universe_one_package <- function(universe, package) {
assert_character(universe, "universe")
assert_universe(universe)
assert_character(package, "package")
# TODO assert that universe is an universe

if (!package %in% universe_ls(universe)) {
cli::cli_abort("Can't find package {package} in universe {universe}.")
Expand Down Expand Up @@ -87,12 +87,68 @@ universe_one_package <- function(universe, package) {
#' @family universe
universe_search <- function(universe, query, limit = 100L) {
assert_character(universe, "universe")
assert_universe(universe)
assert_character(query, "query")
# TODO assert that universe is an universe

universe_query(
universe_url = sprintf("https://%s.r-universe.dev", universe),
path = "search",
query_params = list(q = query, limit = limit)
)
}

#' All universes
#'
#' @param type Type of universe to query: "all",
#' only organizations ("universe"),
#' personal accounts ("maintainer").
#'
#' @return A character vector of all universes.
#' @export
#'
#' @examplesIf interactive()
#' head(all_universes())
all_universes <- function(type = c("all", "universe", "maintainer")) {
type <- type %||% "all"
type <- rlang::arg_match(type)

req_cache_path <- file.path(tools::R_user_dir("universe", "cache"), "httr2")

everyone <- httr2::request("https://r-universe.dev/stats/everyone") |>
httr2::req_cache(req_cache_path) |>
httr2::req_perform() |>
httr2::resp_body_json()

if (type == "all") {
c(
unlist(everyone[["maintainers"]]),
unlist(everyone[["universes"]])
)
} else {
unlist(everyone[[type]])

Check warning on line 128 in R/universe.R

View check run for this annotation

Codecov / codecov/patch

R/universe.R#L128

Added line #L128 was not covered by tests
}

}

# https://github.com/ropensci/universe/issues/3
assert_universe <- function(universe, call = rlang::caller_env()) {

universe_url <- sprintf("https://%s.r-universe.dev", universe)
universe_status <- httr2::request(universe_url) |>
httr2::req_error(is_error = \(resp) FALSE) |>
httr2::req_perform() |>
httr2::resp_status()


if (universe_status == 404L) {
cli::cli_abort(
c(
"Can't find {.arg universe} {.val {universe}} in existing universes.",
i = "Maybe you made a typo?"
),
call = call
)
}

invisible(TRUE)
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ reference:
- global_search
- title: Endpoints for single universes
contents:
- all_universes
- universe_ls
- universe_all_packages
- universe_one_package
Expand Down
24 changes: 24 additions & 0 deletions man/all_universes.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/universe-package.Rd

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

9 changes: 9 additions & 0 deletions tests/testthat/_snaps/universe.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@
Error in `universe_one_package()`:
! Can't find package shiny in universe jeroen.

# assert_universe() works

Code
universe_all_packages("thisisnotanuniversenameok")
Condition
Error in `universe_all_packages()`:
! Can't find `universe` "thisisnotanuniversenameok" in existing universes.
i Maybe you made a typo?

12 changes: 12 additions & 0 deletions tests/testthat/test-universe.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,22 @@ test_that("universe_one_package() errors well", {
})
})

test_that("assert_universe() works", {
expect_snapshot(error = TRUE, {
universe_all_packages("thisisnotanuniversenameok")
})
})

test_that("universe_search() works", {
httptest2::with_mock_dir("univ-search", {
packages <- universe_search("ropensci", query = "needs:gert", limit = 1L)
})
expect_type(packages, "list")
expect_gt(length(packages), 0L)
})

test_that("all_universes() works", {
skip_if_offline()
expect_error(all_universes("bla"), "must be")
expect_type(all_universes(), "character")
})
1 change: 1 addition & 0 deletions tests/testthat/univ-ls/maelle.r-universe.dev.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Welcome to the maelle universe!
1 change: 1 addition & 0 deletions tests/testthat/univ-package/jeroen.r-universe.dev.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Welcome to the jeroen universe!
Loading

0 comments on commit 1c09129

Please sign in to comment.