Skip to content

Commit 7da9dda

Browse files
committed
Merge commit '6b085735f37a34130c5c27213faec78825ebc86e'
#Conflicts: # NEWS.md
2 parents 3ffa126 + 6b08573 commit 7da9dda

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+630
-379
lines changed

NEWS.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@
88
token has expired, then httr2 will now re-run the entire flow to get
99
you a new token (#349).
1010

11+
* `resp_body_json()` and `resp_body_xml()` now caches the parsed values so
12+
that you can use them repeatedly without worrying about the performance cost.
13+
14+
* `req_url_query()` gains a `.multi` parameter that controls what happens when
15+
you supply multiple values in a vector. The default will continue to error
16+
but you can use `.multi = "comma"` to separate with commas, `"pipe"` to
17+
separate with `|`, and `"explode"` to generate one parameter for each
18+
value (e.g. `?a=1&a=2`) (#350).
19+
20+
* The httr2 examples now only run on R 4.2 and later so that we can use
21+
the base pipe and lambda syntax (#345).
22+
23+
* `curl_translate()` now uses the base pipe.
24+
1125
* OAuth docs have been clarified to encourage the use of `req_oauth_*()`,
1226
not `oauth_*()` (#330). This includes a new `vignette("oauth")` which
1327
gives many more details about how OAuth works and how to use it with

R/curl.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ curl_translate <- function(cmd, simplify_headers = TRUE) {
7575
}
7676
steps <- add_curl_step(steps, "req_perform", main_args = perform_args, keep_if_empty = TRUE)
7777

78-
out <- paste0(steps, collapse = " %>% \n ")
78+
out <- paste0(steps, collapse = paste0(pipe(), "\n "))
7979
out <- paste0(out, "\n")
8080

8181
if (clip) {
@@ -86,6 +86,10 @@ curl_translate <- function(cmd, simplify_headers = TRUE) {
8686
structure(out, class = "httr2_cmd")
8787
}
8888

89+
pipe <- function() {
90+
if (getRversion() >= "4.1.0") " |> " else " %>% "
91+
}
92+
8993
#' @export
9094
print.httr2_cmd <- function(x, ...) {
9195
cat(x)

R/multi-req.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@
3838
#' # Requesting these 4 pages one at a time would take 2 seconds:
3939
#' request_base <- request(example_url())
4040
#' reqs <- list(
41-
#' request_base %>% req_url_path("/delay/0.5"),
42-
#' request_base %>% req_url_path("/delay/0.5"),
43-
#' request_base %>% req_url_path("/delay/0.5"),
44-
#' request_base %>% req_url_path("/delay/0.5")
41+
#' request_base |> req_url_path("/delay/0.5"),
42+
#' request_base |> req_url_path("/delay/0.5"),
43+
#' request_base |> req_url_path("/delay/0.5"),
44+
#' request_base |> req_url_path("/delay/0.5")
4545
#' )
4646
#' # But it's much faster if you request in parallel
4747
#' system.time(resps <- req_perform_parallel(reqs))
4848
#'
4949
#' reqs <- list(
50-
#' request_base %>% req_url_path("/status/200"),
51-
#' request_base %>% req_url_path("/status/400"),
50+
#' request_base |> req_url_path("/status/200"),
51+
#' request_base |> req_url_path("/status/400"),
5252
#' request("FAILURE")
5353
#' )
5454
#' # req_perform_parallel() will always succeed

R/oauth-flow-auth-code.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
#' )
9292
#' }
9393
#'
94-
#' request("https://api.github.com/user") %>%
94+
#' request("https://api.github.com/user") |>
9595
#' req_auth_github()
9696
req_oauth_auth_code <- function(req,
9797
client,

R/oauth-flow-client-credentials.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#' )
2222
#' }
2323
#'
24-
#' request("https://example.com") %>%
24+
#' request("https://example.com") |>
2525
#' req_auth()
2626
req_oauth_client_credentials <- function(req,
2727
client,

R/oauth-flow-device.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#' )
2323
#' }
2424
#'
25-
#' request("https://api.github.com/user") %>%
25+
#' request("https://api.github.com/user") |>
2626
#' req_auth_github()
2727
req_oauth_device <- function(req,
2828
client,

R/oauth-flow-jwt.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#' )
3232
#' }
3333
#'
34-
#' request("https://example.com") %>%
34+
#' request("https://example.com") |>
3535
#' req_auth()
3636
req_oauth_bearer_jwt <- function(req,
3737
client,

R/oauth-flow-password.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#' )
2222
#' }
2323
#' if (interactive()) {
24-
#' request("https://example.com") %>%
24+
#' request("https://example.com") |>
2525
#' req_auth()
2626
#' }
2727
req_oauth_password <- function(req,

R/oauth-flow-refresh.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#' @examples
3030
#' client <- oauth_client("example", "https://example.com/get_token")
3131
#' req <- request("https://example.com")
32-
#' req %>% req_oauth_refresh(client)
32+
#' req |> req_oauth_refresh(client)
3333
req_oauth_refresh <- function(req,
3434
client,
3535
refresh_token = Sys.getenv("HTTR2_REFRESH_TOKEN"),

R/paginate.R

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@
4848
#' @examples
4949
#' page_size <- 40
5050
#'
51-
#' request(example_url()) %>%
52-
#' req_url_path("/iris") %>%
53-
#' req_url_query(limit = page_size) %>%
51+
#' request(example_url()) |>
52+
#' req_url_path("/iris") |>
53+
#' req_url_query(limit = page_size) |>
5454
#' req_paginate_page_index(
5555
#' page_index = function(req, page) {
56-
#' req %>% req_url_query(page_index = page)
56+
#' req |> req_url_query(page_index = page)
5757
#' },
5858
#' parse_resp = function(resp) {
5959
#' parsed <- resp_body_json(resp)
@@ -131,12 +131,12 @@ req_paginate <- function(req,
131131
#' @examples
132132
#' page_size <- 40
133133
#'
134-
#' req_flowers <- request(example_url()) %>%
135-
#' req_url_path("/iris") %>%
136-
#' req_url_query(limit = page_size) %>%
134+
#' req_flowers <- request(example_url()) |>
135+
#' req_url_path("/iris") |>
136+
#' req_url_query(limit = page_size) |>
137137
#' req_paginate_page_index(
138138
#' page_index = function(req, page) {
139-
#' req %>% req_url_query(page_index = page)
139+
#' req |> req_url_query(page_index = page)
140140
#' },
141141
#' parse_resp = function(resp) {
142142
#' parsed <- resp_body_json(resp)
@@ -225,17 +225,15 @@ req_perform_iteratively <- function(req,
225225
#' @return Generates the next request in an iterative request,
226226
#' or `NULL` if there are no more pages to return.
227227
#' @examples
228-
#' req_flowers <- request(example_url()) %>%
229-
#' req_url_path("/iris") %>%
230-
#' req_url_query(limit = 40) %>%
228+
#' req_flowers <- request(example_url()) |>
229+
#' req_url_path("/iris") |>
230+
#' req_url_query(limit = 40) |>
231231
#' req_paginate_page_index(
232-
#' page_index = function(req, page) {
233-
#' req %>% req_url_query(page_index = page)
234-
#' }
232+
#' page_index = \(req, page) req |> req_url_query(page_index = page)
235233
#' )
236234
#' req_flowers$url
237235
#'
238-
#' resp <- req_flowers %>% req_perform()
236+
#' resp <- req_flowers |> req_perform()
239237
#' next_req <- iterate_next_request(req_flowers, resp)
240238
#' next_req$url
241239
iterate_next_request <- function(req, parsed) {

R/progress.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
#' or downloaded.
99
#' @export
1010
#' @examples
11-
#' req <- request("https://r4ds.s3.us-west-2.amazonaws.com/seattle-library-checkouts.csv") %>%
11+
#' req <- request("https://r4ds.s3.us-west-2.amazonaws.com/seattle-library-checkouts.csv") |>
1212
#' req_progress()
1313
#'
1414
#' \dontrun{
1515
#' path <- tempfile()
16-
#' req %>% req_perform(path = path)
16+
#' req |> req_perform(path = path)
1717
#' }
1818
req_progress <- function(req, type = c("down", "up")) {
1919
type <- arg_match(type)

R/req-auth.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
#' @returns A modified HTTP [request].
1313
#' @export
1414
#' @examples
15-
#' req <- request("http://example.com") %>% req_auth_basic("hadley", "SECRET")
15+
#' req <- request("http://example.com") |> req_auth_basic("hadley", "SECRET")
1616
#' req
17-
#' req %>% req_dry_run()
17+
#' req |> req_dry_run()
1818
#'
1919
#' # httr2 does its best to redact the Authorization header so that you don't
2020
#' # accidentally reveal confidential data. Use `redact_headers` to reveal it:
2121
#' print(req, redact_headers = FALSE)
22-
#' req %>% req_dry_run(redact_headers = FALSE)
22+
#' req |> req_dry_run(redact_headers = FALSE)
2323
#'
2424
#' # We do this because the authorization header is not encrypted and the
2525
#' # so password can easily be discovered:
@@ -48,7 +48,7 @@ req_auth_basic <- function(req, username, password = NULL) {
4848
#' @returns A modified HTTP [request].
4949
#' @export
5050
#' @examples
51-
#' req <- request("http://example.com") %>% req_auth_bearer_token("sdaljsdf093lkfs")
51+
#' req <- request("http://example.com") |> req_auth_bearer_token("sdaljsdf093lkfs")
5252
#' req
5353
#'
5454
#' # httr2 does its best to redact the Authorization header so that you don't

R/req-body.R

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,36 @@
1616
#' a `Content-Type` header.
1717
#' @returns A modified HTTP [request].
1818
#' @examples
19-
#' req <- request(example_url()) %>%
19+
#' req <- request(example_url()) |>
2020
#' req_url_path("/post")
2121
#'
2222
#' # Most APIs expect small amounts of data in either form or json encoded:
23-
#' req %>%
24-
#' req_body_form(x = "A simple text string") %>%
23+
#' req |>
24+
#' req_body_form(x = "A simple text string") |>
2525
#' req_dry_run()
2626
#'
27-
#' req %>%
28-
#' req_body_json(list(x = "A simple text string")) %>%
27+
#' req |>
28+
#' req_body_json(list(x = "A simple text string")) |>
2929
#' req_dry_run()
3030
#'
3131
#' # For total control over the body, send a string or raw vector
32-
#' req %>%
33-
#' req_body_raw("A simple text string") %>%
32+
#' req |>
33+
#' req_body_raw("A simple text string") |>
3434
#' req_dry_run()
3535
#'
3636
#' # There are two main ways that APIs expect entire files
3737
#' path <- tempfile()
3838
#' writeLines(letters[1:6], path)
3939
#'
4040
#' # You can send a single file as the body:
41-
#' req %>%
42-
#' req_body_file(path) %>%
41+
#' req |>
42+
#' req_body_file(path) |>
4343
#' req_dry_run()
4444
#'
4545
#' # You can send multiple files, or a mix of files and data
4646
#' # with multipart encoding
47-
#' req %>%
48-
#' req_body_multipart(a = curl::form_file(path), b = "some data") %>%
47+
#' req |>
48+
#' req_body_multipart(a = curl::form_file(path), b = "some data") |>
4949
#' req_dry_run()
5050
#' @name req_body
5151
#' @aliases NULL

R/req-cache.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@
4444
#' "master/inst/extdata/penguins.csv"
4545
#' )
4646
#' # Here I set debug = TRUE so you can see what's happening
47-
#' req <- request(url) %>% req_cache(tempdir(), debug = TRUE)
47+
#' req <- request(url) |> req_cache(tempdir(), debug = TRUE)
4848
#'
4949
#' # First request downloads the data
50-
#' resp <- req %>% req_perform()
50+
#' resp <- req |> req_perform()
5151
#'
5252
#' # Second request retrieves it from the cache
53-
#' resp <- req %>% req_perform()
53+
#' resp <- req |> req_perform()
5454
req_cache <- function(req,
5555
path,
5656
use_on_error = FALSE,

R/req-cookies.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010
#' @export
1111
#' @examples
1212
#' path <- tempfile()
13-
#' httpbin <- request(example_url()) %>%
13+
#' httpbin <- request(example_url()) |>
1414
#' req_cookie_preserve(path)
1515
#'
1616
#' # Manually set two cookies
17-
#' httpbin %>%
18-
#' req_template("/cookies/set/:name/:value", name = "chocolate", value = "chip") %>%
19-
#' req_perform() %>%
17+
#' httpbin |>
18+
#' req_template("/cookies/set/:name/:value", name = "chocolate", value = "chip") |>
19+
#' req_perform() |>
2020
#' resp_body_json()
2121
#'
22-
#' httpbin %>%
23-
#' req_template("/cookies/set/:name/:value", name = "oatmeal", value = "raisin") %>%
24-
#' req_perform() %>%
22+
#' httpbin |>
23+
#' req_template("/cookies/set/:name/:value", name = "oatmeal", value = "raisin") |>
24+
#' req_perform() |>
2525
#' resp_body_json()
2626
#'
2727
#' # The cookie path has a straightforward format

R/req-error.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#'
2727
#' ```
2828
#' tryCatch(
29-
#' req %>% req_perform() %>% resp_body_json(),
29+
#' req |> req_perform() |> resp_body_json(),
3030
#' httr2_http_404 = function(cnd) NULL
3131
#' )
3232
#' ```
@@ -36,7 +36,7 @@
3636
#'
3737
#' ```R
3838
#' withCallingHandlers(
39-
#' req %>% req_perform() %>% resp_body_json(),
39+
#' req |> req_perform() |> resp_body_json(),
4040
#' httr2_http_404 = function(cnd) {
4141
#' rlang::abort("Couldn't find user", parent = cnd)
4242
#' }
@@ -59,29 +59,29 @@
5959
#' @examples
6060
#' # Performing this request usually generates an error because httr2
6161
#' # converts HTTP errors into R errors:
62-
#' req <- request(example_url()) %>%
62+
#' req <- request(example_url()) |>
6363
#' req_url_path("/status/404")
64-
#' try(req %>% req_perform())
64+
#' try(req |> req_perform())
6565
#' # You can still retrieve it with last_response()
6666
#' last_response()
6767
#'
6868
#' # But you might want to suppress this behaviour:
69-
#' resp <- req %>%
70-
#' req_error(is_error = function(resp) FALSE) %>%
69+
#' resp <- req |>
70+
#' req_error(is_error = \(resp) FALSE) |>
7171
#' req_perform()
7272
#' resp
7373
#'
7474
#' # Or perhaps you're working with a server that routinely uses the
7575
#' # wrong HTTP error codes only 500s are really errors
76-
#' request("http://example.com") %>%
77-
#' req_error(is_error = function(resp) resp_status(resp) == 500)
76+
#' request("http://example.com") |>
77+
#' req_error(is_error = \(resp) resp_status(resp) == 500)
7878
#'
7979
#' # Most typically you'll use req_error() to add additional information
8080
#' # extracted from the response body (or sometimes header):
8181
#' error_body <- function(resp) {
8282
#' resp_body_json(resp)$error
8383
#' }
84-
#' request("http://example.com") %>%
84+
#' request("http://example.com") |>
8585
#' req_error(body = error_body)
8686
#' # Learn more in https://httr2.r-lib.org/articles/wrapping-apis.html
8787
req_error <- function(req,

0 commit comments

Comments
 (0)