diff --git a/R/oauth-client.R b/R/oauth-client.R index 97302e69..adb7f2ae 100644 --- a/R/oauth-client.R +++ b/R/oauth-client.R @@ -240,4 +240,3 @@ oauth_client_get_token <- function(client, resp <- oauth_flow_fetch(req, "client$token_url", error_call = error_call) exec(oauth_token, !!!resp) } - diff --git a/R/oauth-flow.R b/R/oauth-flow.R index b9a89770..068ae876 100644 --- a/R/oauth-flow.R +++ b/R/oauth-flow.R @@ -1,5 +1,5 @@ oauth_flow_fetch <- function(req, source, error_call = caller_env()) { - req <- req_error(req, is_error = ~ FALSE) + req <- req_error(req, is_error = ~FALSE) resp <- req_perform(req, error_call = current_call()) oauth_flow_parse(resp, source, error_call = error_call) diff --git a/R/req-cookies.R b/R/req-cookies.R index 598c6a9f..29291f4b 100644 --- a/R/req-cookies.R +++ b/R/req-cookies.R @@ -35,4 +35,3 @@ req_cookie_preserve <- function(req, path) { cookiefile = path ) } - diff --git a/R/req-perform-stream.R b/R/req-perform-stream.R index 647fb263..22bd2e76 100644 --- a/R/req-perform-stream.R +++ b/R/req-perform-stream.R @@ -1,12 +1,11 @@ - #' Perform a request and handle data as it streams back -#' +#' #' @description #' After preparing a request, call `req_perform_stream()` to perform the request #' and handle the result with a streaming callback. This is useful for #' streaming HTTP APIs where potentially the stream never ends. -#' -#' The `callback` will only be called if the result is successful. If you need +#' +#' The `callback` will only be called if the result is successful. If you need #' to stream an error response, you can use [req_error()] to suppress error #' handling so that the body is streamed to you. #' @@ -73,10 +72,10 @@ req_perform_stream <- function(req, continue <- TRUE incomplete <- TRUE buf <- raw() - - while(continue && isIncomplete(stream) && Sys.time() < stop_time) { + + while (continue && isIncomplete(stream) && Sys.time() < stop_time) { buf <- c(buf, readBin(stream, raw(), buffer_kb * 1024)) - + if (length(buf) > 0) { cut <- cut_points(buf) n <- length(cut) diff --git a/R/req-perform.R b/R/req-perform.R index 61cebdbc..05961e30 100644 --- a/R/req-perform.R +++ b/R/req-perform.R @@ -30,7 +30,7 @@ #' `options(httr2_progress = FALSE)`. #' #' @param req A [request]. -#' @param path Optionally, path to save body of the response. This is useful +#' @param path Optionally, path to save body of the response. This is useful #' for large responses since it avoids storing the response in memory. #' @param mock A mocking function. If supplied, this function is called #' with the request. It should return either `NULL` (if it doesn't want to @@ -105,7 +105,7 @@ req_perform <- function( throttle_delay(req) delay <- 0 - while(tries < max_tries && Sys.time() < deadline) { + while (tries < max_tries && Sys.time() < deadline) { sys_sleep(delay, "for retry backoff") n <- n + 1 diff --git a/R/req-retries.R b/R/req-retries.R index 47bfd5fb..eead4f93 100644 --- a/R/req-retries.R +++ b/R/req-retries.R @@ -56,7 +56,7 @@ #' #' # use a constant 10s delay after every failure #' request("http://example.com") |> -#' req_retry(backoff = ~ 10) +#' req_retry(backoff = ~10) #' #' # When rate-limited, GitHub's API returns a 403 with #' # `X-RateLimit-Remaining: 0` and an Unix time stored in the diff --git a/R/resp-url.R b/R/resp-url.R index fd3544fb..42c67850 100644 --- a/R/resp-url.R +++ b/R/resp-url.R @@ -47,4 +47,3 @@ resp_url_queries <- function(resp) { url_parse(resp$url)$query } - diff --git a/R/resp.R b/R/resp.R index 2d59787f..dc191cfb 100644 --- a/R/resp.R +++ b/R/resp.R @@ -102,7 +102,7 @@ new_response <- function(method, #' @export -print.httr2_response <- function(x,...) { +print.httr2_response <- function(x, ...) { cli::cli_text("{.cls {class(x)}}") cli::cli_text("{.strong {x$method}} {x$url}") cli::cli_text("{.field Status}: {x$status_code} {resp_status_desc(x)}") diff --git a/R/url.R b/R/url.R index ebb8b822..b4f3cf0c 100644 --- a/R/url.R +++ b/R/url.R @@ -190,11 +190,11 @@ format_query_param <- function(x, unclass(x) } else { x <- format(x, scientific = FALSE, trim = TRUE, justify = "none") - curl::curl_escape(x) + curl::curl_escape(x) } } check_query_param <- function(x, name, multi = FALSE, error_call = caller_env()) { - if (inherits(x, "AsIs") ) { + if (inherits(x, "AsIs")) { if (multi) { ok <- is.character(x) expected <- "a character vector" diff --git a/R/utils.R b/R/utils.R index 0c36d291..ac707da4 100644 --- a/R/utils.R +++ b/R/utils.R @@ -67,7 +67,7 @@ sys_sleep <- function(seconds, total = seconds * fps ) - while({left <- start + seconds - cur_time(); left > 0}) { + while ({left <- start + seconds - cur_time(); left > 0}) { Sys.sleep(min(1 / fps, left)) cli::cli_progress_update(set = (seconds - left) * fps) } diff --git a/man/req_retry.Rd b/man/req_retry.Rd index 70205e8c..d9d830dd 100644 --- a/man/req_retry.Rd +++ b/man/req_retry.Rd @@ -74,7 +74,7 @@ request("http://google.com") |> # use a constant 10s delay after every failure request("http://example.com") |> - req_retry(backoff = ~ 10) + req_retry(backoff = ~10) # When rate-limited, GitHub's API returns a 403 with # `X-RateLimit-Remaining: 0` and an Unix time stored in the diff --git a/tests/testthat/test-curl.R b/tests/testthat/test-curl.R index b49967ca..b3302991 100644 --- a/tests/testthat/test-curl.R +++ b/tests/testthat/test-curl.R @@ -31,7 +31,7 @@ test_that("captures key components of call", { ) # Captures flags - expect_equal(curl_args("curl 'http://example.com' --verbose")$`--verbose`, TRUE) + expect_equal(curl_args("curl 'http://example.com' --verbose")$`--verbose`, TRUE) }) test_that("can handle line breaks", { diff --git a/tests/testthat/test-iterate.R b/tests/testthat/test-iterate.R index 4bbf5516..266ba43c 100644 --- a/tests/testthat/test-iterate.R +++ b/tests/testthat/test-iterate.R @@ -80,7 +80,7 @@ test_that("can choose to return on failure", { test_that("checks its inputs", { req <- request_test() - expect_snapshot(error = TRUE,{ + expect_snapshot(error = TRUE, { req_perform_iterative(1) req_perform_iterative(req, function(x, y) x + y) req_perform_iterative(req, function(resp, req) {}, path = 1) diff --git a/tests/testthat/test-oauth-flow.R b/tests/testthat/test-oauth-flow.R index cbaf6b21..0bce31e4 100644 --- a/tests/testthat/test-oauth-flow.R +++ b/tests/testthat/test-oauth-flow.R @@ -37,4 +37,3 @@ test_that("converts expires_in to numeric", { body <- oauth_flow_parse(resp, "test") expect_equal(body$expires_in, 20) }) - diff --git a/tests/testthat/test-parse.R b/tests/testthat/test-parse.R index 323bd906..ab42b9a2 100644 --- a/tests/testthat/test-parse.R +++ b/tests/testthat/test-parse.R @@ -26,8 +26,8 @@ test_that("can parse media type", { test_that("can parse authenticate header", { header <- paste0( 'Bearer realm="example",', - 'error="invalid_token",',' - error_description="The access token expired"' + 'error="invalid_token",', + 'error_description="The access token expired"' ) out <- parse_www_authenticate(header) expect_equal(out$scheme, "Bearer") diff --git a/tests/testthat/test-req-error.R b/tests/testthat/test-req-error.R index 31a5ba6b..2eefcc0a 100644 --- a/tests/testthat/test-req-error.R +++ b/tests/testthat/test-req-error.R @@ -12,7 +12,7 @@ test_that("can customise error info", { req <- request_test() expect_equal(error_body(req, response(404)), NULL) - req <- req %>% req_error(body = ~ "Hi!") + req <- req %>% req_error(body = ~"Hi!") expect_equal(error_body(req, response(404)), "Hi!") }) diff --git a/tests/testthat/test-req-mock.R b/tests/testthat/test-req-mock.R index e72a59b7..457f7882 100644 --- a/tests/testthat/test-req-mock.R +++ b/tests/testthat/test-req-mock.R @@ -2,7 +2,7 @@ test_that("can override requests through mocking", { resp <- response() req <- request("https://google.com") - expect_equal(with_mocked_responses(~ resp, req_perform(req)), resp) + expect_equal(with_mocked_responses(~resp, req_perform(req)), resp) local_mocked_responses(function(req) resp) expect_equal(req_perform(req), resp) diff --git a/tests/testthat/test-req-perform-stream.R b/tests/testthat/test-req-perform-stream.R index 8bf39822..082961f4 100644 --- a/tests/testthat/test-req-perform-stream.R +++ b/tests/testthat/test-req-perform-stream.R @@ -16,7 +16,7 @@ test_that("returns empty body; sets last request & response", { }) test_that("HTTP errors become R errors", { - req <- request_test("/status/404") + req <- request_test("/status/404") expect_error( req_perform_stream(req, function(x) TRUE), class = "httr2_http_404" @@ -29,8 +29,8 @@ test_that("HTTP errors become R errors", { test_that("can override error handling", { req <- request_test("/base64/:value", value = "YWJj") %>% - req_error(is_error = function(resp) TRUE) - + req_error(is_error = function(resp) TRUE) + expect_error( req %>% req_perform_stream(function(x) NULL), class = "httr2_http_200" @@ -94,7 +94,7 @@ test_that("eventually terminates even if never rounded", { test_that("req_perform_stream checks its inputs", { req <- request_test("/stream-bytes/1024") callback <- function(x) NULL - + expect_snapshot(error = TRUE, { req_perform_stream(1) req_perform_stream(req, 1) diff --git a/tests/testthat/test-req-perform.R b/tests/testthat/test-req-perform.R index 13c9645b..a3fceca6 100644 --- a/tests/testthat/test-req-perform.R +++ b/tests/testthat/test-req-perform.R @@ -56,7 +56,7 @@ test_that("persistent HTTP errors only get single attempt", { test_that("repeated transient errors still fail", { req <- request_test("/status/:status", status = 429) %>% - req_retry(max_tries = 3, backoff = ~ 0) + req_retry(max_tries = 3, backoff = ~0) cnd <- req_perform(req) %>% expect_error(class = "httr2_http_429") %>% diff --git a/tests/testthat/test-req-retries.R b/tests/testthat/test-req-retries.R index 216052cf..3f255445 100644 --- a/tests/testthat/test-req-retries.R +++ b/tests/testthat/test-req-retries.R @@ -34,7 +34,7 @@ test_that("can override default backoff", { expect_equal(retry_backoff(req, 5), 26.9) expect_equal(retry_backoff(req, 10), 60) - req <- req_retry(req, backoff = ~ 10) + req <- req_retry(req, backoff = ~10) expect_equal(retry_backoff(req, 1), 10) expect_equal(retry_backoff(req, 5), 10) expect_equal(retry_backoff(req, 10), 10) @@ -51,7 +51,7 @@ test_that("can override default retry wait", { test_that("missing retry-after uses backoff", { req <- request_test() - req <- req_retry(req, backoff = ~ 10) + req <- req_retry(req, backoff = ~10) expect_equal(retry_after(req, response(429), 1), 10) }) diff --git a/tests/testthat/test-req-template.R b/tests/testthat/test-req-template.R index 219cc3ae..892970f0 100644 --- a/tests/testthat/test-req-template.R +++ b/tests/testthat/test-req-template.R @@ -59,4 +59,3 @@ test_that("can use colon in uri style", { x <- "x" expect_equal(template_process("/:{x}:/"), "/:x:/") }) - diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index ad2ab51b..aeb46a1f 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -13,7 +13,7 @@ test_that("replacement affects all components with name", { x <- list(a = 1, a = 2) expect_equal(modify_list(x, a = NULL), list()) expect_equal(modify_list(x, a = 3), list(a = 3)) - expect_equal(modify_list(x, a = 3, a = 4), list(a = 3, a =4)) + expect_equal(modify_list(x, a = 3, a = 4), list(a = 3, a = 4)) }) test_that("respects httr2 verbosity option", { diff --git a/vignettes/articles/oauth.Rmd b/vignettes/articles/oauth.Rmd index fd696f7f..0f061b63 100644 --- a/vignettes/articles/oauth.Rmd +++ b/vignettes/articles/oauth.Rmd @@ -163,7 +163,7 @@ For example, you could get a token to access the GitHub API (using the client de ```{r, eval = FALSE} token <- oauth_flow_auth_code( - client = client, + client = client, auth_url = "https://github.com/login/oauth/authorize" ) ``` @@ -200,7 +200,7 @@ If we make a request to this endpoint without authentication, we'll get an error ```{r} #| error: true req <- request("https://api.github.com/user") -req |> +req |> req_perform() ``` @@ -208,13 +208,13 @@ We can authenticate this request with `req_oauth_auth_code()`, using the same ar ```{r} #| eval: false -req |> +req |> req_oauth_auth_code( client = github_client(), auth_url = "https://github.com/login/oauth/authorize" - ) |> - req_perform() |> - resp_body_json() |> + ) |> + req_perform() |> + resp_body_json() |> str() ``` diff --git a/vignettes/articles/wrapping-apis.Rmd b/vignettes/articles/wrapping-apis.Rmd index a0547848..4de09097 100644 --- a/vignettes/articles/wrapping-apis.Rmd +++ b/vignettes/articles/wrapping-apis.Rmd @@ -36,15 +36,15 @@ library(httr2) ```{r, include = FALSE} # Seems to return 500s from time-to-time, so avoid any problems # by only evaluating other chunks if a simple request succeeds. -faker_status_images <- request("https://fakerapi.it/api/v1") |> - req_url_path_append("images") |> - req_error(is_error = ~ FALSE) |> - req_perform() |> +faker_status_images <- request("https://fakerapi.it/api/v1") |> + req_url_path_append("images") |> + req_error(is_error = ~FALSE) |> + req_perform() |> resp_status() -faker_status_persons <- request("https://fakerapi.it/api/v1") |> - req_url_path_append("persons") |> - req_error(is_error = ~ FALSE) |> - req_perform() |> +faker_status_persons <- request("https://fakerapi.it/api/v1") |> + req_url_path_append("persons") |> + req_error(is_error = ~FALSE) |> + req_perform() |> resp_status() faker_ok <- faker_status_images < 400 && faker_status_persons < 400 @@ -56,11 +56,11 @@ Before we start writing the sort of functions that you might put in a package, w ```{r, eval = faker_ok} # We start by creating a request that uses the base API url req <- request("https://fakerapi.it/api/v1") -resp <- req |> +resp <- req |> # Then we add on the images path - req_url_path_append("images") |> + req_url_path_append("images") |> # Add query parameters _width and _quantity - req_url_query(`_width` = 380, `_quantity` = 1) |> + req_url_query(`_width` = 380, `_quantity` = 1) |> req_perform() # The result comes back as JSON @@ -73,8 +73,8 @@ It's always worth a little early experimentation to see if we get any useful inf The httr2 defaults get in your way here, because if you retrieve an unsuccessful HTTP response, you automatically get an error that prevents you from further inspecting the body: ```{r, error = TRUE, eval = faker_ok} -req |> - req_url_path_append("invalid") |> +req |> + req_url_path_append("invalid") |> req_perform() ``` @@ -102,7 +102,7 @@ You can do this with the `req_user_agent()` function: ```{r, eval = faker_ok} req |> - req_user_agent("my_package_name (http://my.package.web.site)") |> + req_user_agent("my_package_name (http://my.package.web.site)") |> req_dry_run() ``` @@ -130,12 +130,12 @@ faker <- function(resource, ..., quantity = 1, locale = "en_US", seed = NULL) { seed = seed ) names(params) <- paste0("_", names(params)) - - request("https://fakerapi.it/api/v1") |> - req_url_path_append(resource) |> - req_url_query(!!!params) |> - req_user_agent("my_package_name (http://my.package.web.site)") |> - req_perform() |> + + request("https://fakerapi.it/api/v1") |> + req_url_path_append(resource) |> + req_url_query(!!!params) |> + req_user_agent("my_package_name (http://my.package.web.site)") |> + req_perform() |> resp_body_json() } @@ -176,7 +176,7 @@ faker_person <- function(gender = NULL, birthday_start = NULL, birthday_end = NU quantity = quantity, locale = locale, seed = seed - ) + ) } str(faker_person("male")) ``` @@ -203,7 +203,7 @@ faker_person <- function(gender = NULL, birthday_start = NULL, birthday_end = NU } birthday_end <- format(birthday_end, "%Y-%m-%d") } - + json <- faker( "persons", gender = gender, @@ -212,8 +212,8 @@ faker_person <- function(gender = NULL, birthday_start = NULL, birthday_end = NU quantity = quantity, locale = locale, seed = seed - ) - + ) + tibble::tibble( firstname = map_chr(json$data, "firstname"), lastname = map_chr(json$data, "lastname"), @@ -270,7 +270,7 @@ In this section, I'll show you how to store that key so that you (and your autom httr2 is built around the notion that this key should live in an environment variable. So the first step is to make your package key available on your local development machine by adding a line to your your user-level `.Renviron` (which you can easily open with `usethis::edit_r_environ()`): -``` +``` YOURPACKAGE_KEY=key_you_generated_with_secret_make_key ``` @@ -317,7 +317,7 @@ So if you want to share your package on CRAN or make it easy for others to contr ## NYTimes Books API ```{r include = FALSE} -has_auth <- secret_has_key("HTTR2_KEY") && +has_auth <- secret_has_key("HTTR2_KEY") && # Don't run in R CMD check on GHA because it hits NYTimes rate limits !identical(Sys.getenv("GITHUB_WORKFLOW"), "R-CMD-check") knitr::opts_chunk$set(eval = has_auth) @@ -353,9 +353,9 @@ And indeed, you'll see that in the examples below --- this is bad practice for a Now let's perform a test request and look at the response: ```{r} -resp <- request("https://api.nytimes.com/svc/books/v3") |> - req_url_path_append("/reviews.json") |> - req_url_query(`api-key` = my_key, isbn = 9780307476463) |> +resp <- request("https://api.nytimes.com/svc/books/v3") |> + req_url_path_append("/reviews.json") |> + req_url_query(`api-key` = my_key, isbn = 9780307476463) |> req_perform() resp ``` @@ -363,8 +363,8 @@ resp Like most modern APIs, this one returns the results as JSON: ```{r} -resp |> - resp_body_json() |> +resp |> + resp_body_json() |> str() ``` @@ -376,9 +376,9 @@ What happens if there's an error? For example, if we deliberately supply an invalid key: ```{r, error = TRUE} -resp <- request("https://api.nytimes.com/svc/books/v3") |> - req_url_path_append("/reviews.json") |> - req_url_query(`api-key` = "invalid", isbn = 9780307476463) |> +resp <- request("https://api.nytimes.com/svc/books/v3") |> + req_url_path_append("/reviews.json") |> + req_url_query(`api-key` = "invalid", isbn = 9780307476463) |> req_perform() ``` @@ -405,10 +405,10 @@ nytimes_error_body <- function(resp) { resp |> resp_body_json() |> _$fault |> _$faultstring } -resp <- request("https://api.nytimes.com/svc/books/v3") |> - req_url_path_append("/reviews.json") |> - req_url_query(`api-key` = "invalid", isbn = 9780307476463) |> - req_error(body = nytimes_error_body) |> +resp <- request("https://api.nytimes.com/svc/books/v3") |> + req_url_path_append("/reviews.json") |> + req_url_query(`api-key` = "invalid", isbn = 9780307476463) |> + req_error(body = nytimes_error_body) |> req_perform() ``` @@ -427,9 +427,9 @@ That means we can't use `req_retry()`, which automatically waits the amount of t Instead, we'll use `req_throttle()` to ensure we don't make more than 10 requests every 60 seconds: ```{r} -req <- request("https://api.nytimes.com/svc/books/v3") |> - req_url_path_append("/reviews.json") |> - req_url_query(`api-key` = "invalid", isbn = 9780307476463) |> +req <- request("https://api.nytimes.com/svc/books/v3") |> + req_url_path_append("/reviews.json") |> + req_url_query(`api-key` = "invalid", isbn = 9780307476463) |> req_throttle(10 / 60) ``` @@ -437,9 +437,9 @@ By default, `req_throttle()` shares the limit across all requests made to the ho Since the docs suggest the rate limit applies per API, you might want to use the `realm` argument to be a bit more specific: ```{r} -req <- request("https://api.nytimes.com/svc/books/v3") |> - req_url_path_append("/reviews.json") |> - req_url_query(`api-key` = "invalid", isbn = 9780307476463) |> +req <- request("https://api.nytimes.com/svc/books/v3") |> + req_url_path_append("/reviews.json") |> + req_url_query(`api-key` = "invalid", isbn = 9780307476463) |> req_throttle(10 / 60, realm = "https://api.nytimes.com/svc/books") ``` @@ -449,12 +449,12 @@ Putting together all the pieces above yields a function something like this: ```{r} nytimes_books <- function(api_key, path, ...) { - request("https://api.nytimes.com/svc/books/v3") |> - req_url_path_append(path) |> - req_url_query(..., `api-key` = api_key) |> - req_error(body = nytimes_error_body) |> - req_throttle(10 / 60, realm = "https://api.nytimes.com/svc/books") |> - req_perform() |> + request("https://api.nytimes.com/svc/books/v3") |> + req_url_path_append(path) |> + req_url_query(..., `api-key` = api_key) |> + req_error(body = nytimes_error_body) |> + req_throttle(10 / 60, realm = "https://api.nytimes.com/svc/books") |> + req_perform() |> resp_body_json() } @@ -517,11 +517,11 @@ get_api_key <- function() { if (!identical(key, "")) { return(key) } - + if (is_testing()) { return(testing_key()) } else { - stop("No API key found, please supply with `api_key` argument or with NYTIMES_KEY env var") + stop("No API key found, please supply with `api_key` argument or with NYTIMES_KEY env var") } } @@ -559,7 +559,7 @@ It's also a good idea to give every token a descriptive name, that reminds you o To authenticate a request with the token, we need to put it in the `Authorization` header with a ["token" prefix](https://docs.github.com/en/rest/overview/resources-in-the-rest-api#authentication): ```{r} -req <- request("https://api.github.com/gists") |> +req <- request("https://api.github.com/gists") |> req_headers(Authorization = paste("token", token)) req |> req_perform() @@ -585,9 +585,9 @@ You'll typically need to tackle your error handling iteratively, improving your While GitHub does [document its errors](https://docs.github.com/en/rest/overview/resources-in-the-rest-api#client-errors), I'm sufficiently distrustful that I still want to construct a deliberately malformed query and see what happens: ```{r, error = TRUE} -resp <- request("https://api.github.com/gists") |> - req_url_query(since = "abcdef") |> - req_headers(Authorization = paste("token", token)) |> +resp <- request("https://api.github.com/gists") |> + req_url_query(since = "abcdef") |> + req_headers(Authorization = paste("token", token)) |> req_perform() ``` @@ -605,7 +605,7 @@ I'll proceed anyway, writing a function that extracts the data and formats it fo ```{r} gist_error_body <- function(resp) { body <- resp_body_json(resp) - + message <- body$message if (!is.null(body$documentation_url)) { message <- c(message, paste0("See docs at <", body$documentation_url, ">")) @@ -618,10 +618,10 @@ gist_error_body(resp) Now I can pass this function to the `body` argument of `req_error()` and it will be automatically included in the error when a request fails: ```{r, error = TRUE} -request("https://api.github.com/gists") |> - req_url_query(since = "yesterday") |> - req_headers(Authorization = paste("token", token)) |> - req_error(body = gist_error_body) |> +request("https://api.github.com/gists") |> + req_url_query(since = "yesterday") |> + req_headers(Authorization = paste("token", token)) |> + req_error(body = gist_error_body) |> req_perform() ``` @@ -633,7 +633,7 @@ While we're thinking about errors, it's useful to look at what happens if the re Luckily, GitHub consistently uses response headers to provide information about the remaining [rate limits](https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting). ```{r} -resp <- req |> req_perform() +resp <- req |> req_perform() resp |> resp_headers("ratelimit") ``` @@ -644,7 +644,7 @@ For GitHub, when the rate limit is [exceeded](https://docs.github.com/en/rest/ov ```{r} gist_is_transient <- function(resp) { - resp_status(resp) == 403 && + resp_status(resp) == 403 && resp_header(resp, "X-RateLimit-Remaining") == "0" } gist_is_transient(resp) @@ -681,9 +681,9 @@ Let's wrap up everything we've learned so far into a single function that create ```{r} req_gist <- function(token) { - request("https://api.github.com/gists") |> - req_headers(Authorization = paste("token", token)) |> - req_error(body = gist_error_body) |> + request("https://api.github.com/gists") |> + req_headers(Authorization = paste("token", token)) |> + req_error(body = gist_error_body) |> req_retry( is_transient = gist_is_transient, after = gist_after @@ -691,7 +691,7 @@ req_gist <- function(token) { } # Check it works: -req_gist(token) |> +req_gist(token) |> req_perform() ``` @@ -703,7 +703,7 @@ To [create a gist](https://docs.github.com/en/rest/reference/gists#create-a-gist httr2 provides one function that does both of these things: `req_body_json()`: ```{r} -req <- req_gist(token) |> +req <- req_gist(token) |> req_body_json(list( description = "This is my cool gist!", files = list(test.R = list(content = "print('Hi!')")), @@ -732,9 +732,9 @@ To do so, I need to again send JSON encoded data, but this time I need to use th So after adding the data to request, I use `req_method()` to override the default method: ```{r} -req <- req_gist(token) |> - req_url_path_append(id) |> - req_body_json(list(description = "This is a simple gist")) |> +req <- req_gist(token) |> + req_url_path_append(id) |> + req_body_json(list(description = "This is a simple gist")) |> req_method("PATCH") req |> req_dry_run() ``` @@ -744,8 +744,8 @@ req |> req_dry_run() Deleting a gist is similar, except we don't send any data, we just need to adjust the default method from `GET` to `DELETE`. ```{r} -req <- req_gist(token) |> - req_url_path_append(id) |> +req <- req_gist(token) |> + req_url_path_append(id) |> req_method("DELETE") req |> req_dry_run() req |> req_perform() diff --git a/vignettes/httr2.Rmd b/vignettes/httr2.Rmd index b2e678d1..fdc43fec 100644 --- a/vignettes/httr2.Rmd +++ b/vignettes/httr2.Rmd @@ -71,10 +71,10 @@ The headers in this request were automatically added by httr2, but you can overr ```{r} req |> req_headers( - Name = "Hadley", - `Shoe-Size` = "11", + Name = "Hadley", + `Shoe-Size` = "11", Accept = "application/json" - ) |> + ) |> req_dry_run() ``` @@ -87,7 +87,7 @@ Here we'll use `req_body_json()` to add some data encoded as JSON: ```{r} req |> - req_body_json(list(x = 1, y = "a")) |> + req_body_json(list(x = 1, y = "a")) |> req_dry_run() ``` @@ -107,7 +107,7 @@ For example, `req_body_form()` uses the encoding used when you submit a form fro ```{r} req |> - req_body_form(x = "1", y = "a") |> + req_body_form(x = "1", y = "a") |> req_dry_run() ``` @@ -115,7 +115,7 @@ And `req_body_multipart()` uses the multipart encoding which is particularly imp ```{r} req |> - req_body_multipart(x = "1", y = "a") |> + req_body_multipart(x = "1", y = "a") |> req_dry_run() ``` @@ -175,12 +175,12 @@ Responses with status codes 4xx and 5xx are HTTP errors. httr2 automatically turns these into R errors: ```{r, error = TRUE} -request(example_url()) |> - req_url_path("/status/404") |> +request(example_url()) |> + req_url_path("/status/404") |> req_perform() -request(example_url()) |> - req_url_path("/status/500") |> +request(example_url()) |> + req_url_path("/status/500") |> req_perform() ```