-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
124 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) |