Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
some more work
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed Oct 8, 2024
1 parent d0bed4f commit aeb86b2
Show file tree
Hide file tree
Showing 14 changed files with 555 additions and 9 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
^starchart\.Rproj$
^\.Rproj\.user$
^LICENSE\.md$
^man/universe_query\.Rd$
9 changes: 7 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ Authors@R:
Description: Client to access the 'R-Universe' 'API'.
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
Roxygen: list(markdown = TRUE, roclets = c("collate", "rd", "namespace", "devtag::dev_roclet"))
RoxygenNote: 7.3.2
Depends:
Depends:
R (>= 4.2.0)
Imports:
cli,
httr2
URL: https://github.com/ropenscilabs/starchart
BugReports: https://github.com/ropenscilabs/starchart/issues
Config/Needs/build: moodymudskipper/devtag
Suggests:
httptest2,
testthat (>= 3.0.0)
Config/testthat/edition: 3
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Generated by roxygen2: do not edit by hand

export(universe_ls)
export(universe_packages)
27 changes: 27 additions & 0 deletions R/universe.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' @examplesIf interactive()
#' universe_ls("jeroen")
#' universe_ls("ropensci")
#' @family universe
universe_ls <- function(universe) {
if (!is.character(universe) || length(universe) != 1) {
cli::cli_abort("{.arg universe} must be a character of length 1.")
Expand All @@ -20,3 +21,29 @@ universe_ls <- function(universe) {
) |>
unlist()
}


#' Info on all packages in an universe
#'
#' @param universe Name of the universe (character of length 1)
#' @param limit Number of results to return (integer of length 1)
#'
#' @return A list with information on all packages in the universe.
#' @export
#'
#' @examplesIf interactive()
#' universe_packages("jeroen")
#' universe_packages("ropensci")
#' @family universe
universe_packages <- function(universe, limit = 100) {
if (!is.character(universe) || length(universe) != 1) {
cli::cli_abort("{.arg universe} must be a character of length 1.")
}
# TODO assert that universe is an universe

universe_query(
universe_url = sprintf("https://%s.r-universe.dev", universe),
path = "packages",
query_params = list(limit = limit)
)
}
28 changes: 21 additions & 7 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
universe_query <- function(universe_url, path) {
httr2::request(universe_url) |>
httr2::req_url_path("api") |>
httr2::req_url_path_append(path) |>
httr2::req_user_agent("starchart R package") |>
httr2::req_perform() |>
httr2::resp_body_json()
#' Query the API of a single universe
#'
#' @param universe_url URL to the universe like ""https://jeroen.r-universe.dev"
#' @param path Path to append after api like "ls"
#' @param query_params Named list of query parameters
#'
#' @return The JSON from the API as a list
#' @dev
#'
universe_query <- function(universe_url, path, query_params = NULL) {
request <- httr2::request(universe_url) |>
httr2::req_url_path("api") |>
httr2::req_url_path_append(path) |>
httr2::req_user_agent("starchart R package")

if (!is.null(query_params)) {
request <- httr2::req_url_query(request, !!!query_params)
}

httr2::req_perform(request) |>
httr2::resp_body_json()
}
8 changes: 8 additions & 0 deletions man/starchart-package.Rd

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

5 changes: 5 additions & 0 deletions man/universe_ls.Rd

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

30 changes: 30 additions & 0 deletions man/universe_packages.Rd

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

22 changes: 22 additions & 0 deletions man/universe_query.Rd

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

12 changes: 12 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

library(testthat)
library(starchart)

test_check("starchart")
1 change: 1 addition & 0 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
library(httptest2)
15 changes: 15 additions & 0 deletions tests/testthat/test-universe.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
test_that("universe_ls() works", {
httptest2::with_mock_dir("univ-ls", {
packages <- universe_ls("maelle")
})
expect_type(packages, "character")
expect_gt(length(packages), 0)
})

test_that("universe_packages() works", {
httptest2::with_mock_dir("univ-packages", {
packages <- universe_packages("maelle", limit = 1)
})
expect_type(packages, "list")
expect_gt(length(packages), 0)
})
6 changes: 6 additions & 0 deletions tests/testthat/univ-ls/maelle.r-universe.dev/api/ls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
"cransays",
"glitter",
"roblog",
"testthat"
]
Loading

0 comments on commit aeb86b2

Please sign in to comment.