Skip to content

Commit

Permalink
Merge commit '79fd17801bf8edfa8af4aa1ea62d9fe2394a393e'
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Aug 11, 2024
2 parents 2fe9cd7 + 79fd178 commit 3532011
Show file tree
Hide file tree
Showing 25 changed files with 118 additions and 124 deletions.
1 change: 0 additions & 1 deletion R/oauth-client.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

2 changes: 1 addition & 1 deletion R/oauth-flow.R
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
1 change: 0 additions & 1 deletion R/req-cookies.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,3 @@ req_cookie_preserve <- function(req, path) {
cookiefile = path
)
}

13 changes: 6 additions & 7 deletions R/req-perform-stream.R
Original file line number Diff line number Diff line change
@@ -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.
#'
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions R/req-perform.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion R/req-retries.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion R/resp-url.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,3 @@ resp_url_queries <- function(resp) {

url_parse(resp$url)$query
}

2 changes: 1 addition & 1 deletion R/resp.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)}")
Expand Down
4 changes: 2 additions & 2 deletions R/url.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion man/req_retry.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-curl.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-iterate.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-oauth-flow.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,3 @@ test_that("converts expires_in to numeric", {
body <- oauth_flow_parse(resp, "test")
expect_equal(body$expires_in, 20)
})

4 changes: 2 additions & 2 deletions tests/testthat/test-parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-req-error.R
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
})

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-req-mock.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-req-perform-stream.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-req-perform.R
Original file line number Diff line number Diff line change
Expand Up @@ -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") %>%
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-req-retries.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
})
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-req-template.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,3 @@ test_that("can use colon in uri style", {
x <- "x"
expect_equal(template_process("/:{x}:/"), "/:x:/")
})

2 changes: 1 addition & 1 deletion tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down
12 changes: 6 additions & 6 deletions vignettes/articles/oauth.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
```
Expand Down Expand Up @@ -200,21 +200,21 @@ 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()
```

We can authenticate this request with `req_oauth_auth_code()`, using the same arguments as our previous call to `oauth_flow_auth_code()`:

```{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()
```

Expand Down
Loading

0 comments on commit 3532011

Please sign in to comment.