Skip to content

Commit

Permalink
Merge branch 'main' into cookie-jar
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley authored Aug 23, 2023
2 parents 16ff3eb + f28ec8a commit ef66cbd
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 7 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ export(resp_raw)
export(resp_retry_after)
export(resp_status)
export(resp_status_desc)
export(resp_url)
export(resp_url_path)
export(resp_url_queries)
export(resp_url_query)
export(response)
export(secret_decrypt)
export(secret_encrypt)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
* New `req_cookie_file()` lets you use a file to share cookies across
requests (#223).

* 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
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
48 changes: 48 additions & 0 deletions R/resp-url.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#' Get URL/components from the response
#'
#' * `resp_url()` returns the complete url.
#' * `resp_url_path()` returns the path component.
#' * `resp_url_query()` returns a single query component.
#' * `resp_url_queries()` returns the query component as a named list.
#'
#' @inheritParams resp_header
#' @export
#' @examples
#' req <- request("https://httr2.r-lib.org?hello=world")
#'
#' resp <- req_perform(req)
#' resp %>% resp_url()
#' resp %>% resp_url_path()
#' resp %>% resp_url_query()
resp_url <- function(resp) {
check_response(resp)

resp$url
}

#' @export
#' @rdname resp_url
resp_url_path <- function(resp) {
check_response(resp)

url_parse(resp$url)$path
}

#' @export
#' @rdname resp_url
#' @param name Query parameter name.
#' @param default Default value to use if query parameter doesn't exist.
resp_url_query <- function(resp, name, default = NULL) {
check_response(resp)

resp_url_queries(resp)[[name]] %||% default
}

#' @export
#' @rdname resp_url
resp_url_queries <- function(resp) {
check_response(resp)

url_parse(resp$url)$query
}

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.

40 changes: 40 additions & 0 deletions man/resp_url.Rd

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

11 changes: 11 additions & 0 deletions tests/testthat/test-resp-url.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test_that("can extract url components from a response", {
resp <- req_perform(request_test("/get?a=1&b=2"))

expect_equal(resp_url(resp), paste0(example_url(), "get?a=1&b=2"))
expect_equal(resp_url_path(resp), "/get")
expect_equal(resp_url_queries(resp), list(a = "1", b = "2"))

expect_equal(resp_url_query(resp, "a"), "1")
expect_equal(resp_url_query(resp, "c"), NULL)
expect_equal(resp_url_query(resp, "c", "x"), "x")
})

0 comments on commit ef66cbd

Please sign in to comment.