Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it easier to generate repeated query parameters #352

Merged
merged 4 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# httr2 (development version)

* `req_url_query()` now allows you to set query parameters to a vector of
values, in which case the name will be repeated, i.e.
`req_url_query(req, a = 1)` will generate `?a=1&a=2` (#350).
hadley marked this conversation as resolved.
Show resolved Hide resolved

* The httr2 examples now only run on R 4.2 and later so that we can use
the base pipe and lambda syntax (#345).

Expand Down
29 changes: 27 additions & 2 deletions R/req-url.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' @param url New URL; completely replaces existing.
#' @param ... For `req_url_query()`: <[`dynamic-dots`][rlang::dyn-dots]>
#' Name-value pairs that provide query parameters. Each value must be either
#' a length-1 atomic vector (which is automatically escaped) or `NULL` (which
#' an atomic vector (which is automatically escaped) or `NULL` (which
hadley marked this conversation as resolved.
Show resolved Hide resolved
#' is silently dropped). If you want to opt out of escaping, wrap strings in
#' `I()`.
#'
Expand All @@ -32,6 +32,10 @@
#' req |>
#' req_url("http://google.com")
#'
#' # Vectors automatically generate repeated parameter names:
#' req |>
#' req_url_query(id = 100:105)
#'
#' # If you have query parameters in a list, use !!!
#' params <- list(a = "1", b = "2")
#' req |>
Expand All @@ -49,8 +53,29 @@ req_url <- function(req, url) {
req_url_query <- function(.req, ...) {
check_request(.req)

dots <- list2(...)

type_ok <- map_lgl(dots, function(x) is_atomic(x) || is.null(x))
if (any(!type_ok)) {
cli::cli_abort(
"All elements of {.code ...} must be either an atomic vector or NULL."
)
}

expanded <- map(dots, function(x) {
if (is.null(x)) {
list(NULL)
} else {
map(seq_along(x), function(i) x[i])
}
})
expanded <- stats::setNames(
unlist(expanded, recursive = FALSE, use.names = FALSE),
rep(names(dots), lengths(expanded))
)

url <- url_parse(.req$url)
url$query <- modify_list(url$query, ...)
url$query <- modify_list(url$query, !!!expanded)

req_url(.req, url_build(url))
}
Expand Down
6 changes: 5 additions & 1 deletion man/req_url.Rd

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

12 changes: 2 additions & 10 deletions tests/testthat/_snaps/req-url.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
# query components must be length 1

Code
req <- request("http://example.com/")
req %>% req_url_query(a = mean)
Condition
Error in `url_build()`:
! Query parameters must be length 1 atomic vectors.
* Problems: "a".
Code
req %>% req_url_query(a = letters)
Condition
Error in `url_build()`:
! Query parameters must be length 1 atomic vectors.
* Problems: "a".
Error in `req_url_query()`:
! All elements of `...` must be either an atomic vector or NULL.

8 changes: 3 additions & 5 deletions tests/testthat/test-req-url.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ test_that("can set query params", {

expect_equal(req_url_query(req, a = 1, a = 2)$url, "http://example.com/?a=1&a=2")
expect_equal(req_url_query(req, !!!list(a = 1, a = 2))$url, "http://example.com/?a=1&a=2")
expect_equal(req_url_query(req, a = 1:2)$url, "http://example.com/?a=1&a=2")
})

test_that("empty query doesn't affect url", {
Expand All @@ -72,9 +73,6 @@ test_that("can opt-out of query escaping", {
})

test_that("query components must be length 1", {
expect_snapshot(error = TRUE, {
req <- request("http://example.com/")
req %>% req_url_query(a = mean)
req %>% req_url_query(a = letters)
})
req <- request("http://example.com/")
expect_snapshot(req %>% req_url_query(a = mean), error = TRUE)
})