Skip to content

Commit

Permalink
Merge branch 'main' into resp_url
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley authored Aug 23, 2023
2 parents 6c0e89c + 21a2a32 commit 9eca783
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 10 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
* New `resp_url()`, `resp_url_path()`, `resp_url_queries()` and
`resp_url_query()` to extract various part of the response url (#57).

* Progress bars displayed while waiting for some time to pass are now
more informative (#206).

* `url_build()` automatically adds leading `/` to `path` if missing (#276).

* Cached responses now combine the headers of the new response with the headers
of the cached response. In particular, this fixes `resp_body_json/xml/html()`
on cached responses (@mgirlich, #277).
Expand Down
6 changes: 5 additions & 1 deletion R/req-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
#' [HTTP caching](https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching).
#'
#' @inheritParams req_perform
#' @param path Path to cache directory
#' @param path Path to cache directory.
#'
#' httr2 doesn't provide helpers to manage the cache, but if you want to
#' empty it, you can use something like
#' `unlink(dir(cache_path, full.names = TRUE))`.
#' @param use_on_error If the request errors, and there's a cache response,
#' should `req_perform()` return that instead of generating an error?
#' @param debug When `TRUE` will emit useful messages telling you about
Expand Down
2 changes: 1 addition & 1 deletion R/req-perform.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ req_perform <- function(

delay <- 0
while(tries < max_tries && Sys.time() < deadline) {
sys_sleep(delay)
sys_sleep(delay, "for retry backoff")
n <- n + 1

resp <- tryCatch(
Expand Down
2 changes: 1 addition & 1 deletion R/req-throttle.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ req_throttle <- function(req, rate, realm = NULL) {
wait <- delay - (unix_time() - last)
}

sys_sleep(wait)
sys_sleep(wait, "for throttling delay")
throttle_touch(realm)
wait
}
Expand Down
4 changes: 4 additions & 0 deletions R/url.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ url_build <- function(url) {
authority <- NULL
}

if (!is.null(url$path) && !startsWith(url$path, "/")) {
url$path <- paste0("/", url$path)
}

prefix <- function(prefix, x) if (!is.null(x)) paste0(prefix, x)
paste0(
url$scheme, if (!is.null(url$scheme)) ":",
Expand Down
6 changes: 3 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ modify_list <- function(.x, ..., error_call = caller_env()) {
}


sys_sleep <- function(seconds, fps = 10, error_call = caller_env()) {
check_number_decimal(seconds, call = error_call)
sys_sleep <- function(seconds, task, fps = 10) {
check_number_decimal(seconds)

if (seconds == 0) {
return(invisible())
Expand All @@ -51,7 +51,7 @@ sys_sleep <- function(seconds, fps = 10, error_call = caller_env()) {
signal("", class = "httr2_sleep", seconds = seconds)

cli::cli_progress_bar(
format = "Waiting {round(seconds)}s to retry {cli::pb_bar}",
format = "Waiting {round(seconds)}s {task} {cli::pb_bar}",
total = seconds * fps
)

Expand Down
6 changes: 5 additions & 1 deletion man/req_cache.Rd

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

6 changes: 3 additions & 3 deletions tests/testthat/test-req-url.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ test_that("can append multiple components", {

test_that("can handle empty path", {
req <- request("http://example.com/x")
expect_equal(req_url_path(req)$url, "http://example.com")
expect_equal(req_url_path(req)$url, "http://example.com/")
expect_equal(req_url_path_append(req)$url, "http://example.com/x")
expect_equal(req_url_path(req, NULL)$url, "http://example.com")
expect_equal(req_url_path(req, NULL)$url, "http://example.com/")
expect_equal(req_url_path_append(req, NULL)$url, "http://example.com/x")

expect_equal(req_url_path(req, "")$url, "http://example.com")
expect_equal(req_url_path(req, "")$url, "http://example.com/")
expect_equal(req_url_path_append(req, "")$url, "http://example.com/x")
})

Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-url.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ test_that("can print all url details", {
)
})

test_that("ensures path always starts with /", {
expect_equal(
url_modify("https://example.com/abc", path = "def"),
"https://example.com/def"
)
})

# query -------------------------------------------------------------------

test_that("missing query values become empty strings", {
Expand Down

0 comments on commit 9eca783

Please sign in to comment.