-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Store url as a string * Replace _set suffix with url_ prefix * Test and document
- Loading branch information
Showing
9 changed files
with
141 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,60 @@ | ||
|
||
req_query_set <- function(req, ...) { | ||
query <- list2(...) | ||
req$url$query <- utils::modifyList(req$url$query, query) | ||
#' Modify the request URL | ||
#' | ||
#' @description | ||
#' * `req_url()` replaces the entire url | ||
#' * `req_url_query()` modifies the components of the query | ||
#' * `req_url_path()` modifies the path | ||
#' * `req_url_path_append()` adds to the path | ||
#' | ||
#' @inheritParams req_fetch | ||
#' @param url New URL; completely replaces existing. | ||
#' @return A modified HTTP [req]uest. | ||
#' @export | ||
#' @examples | ||
#' req <- req("http://example.com") | ||
#' | ||
#' # Change url components | ||
#' req %>% | ||
#' req_url_path_append("a") %>% | ||
#' req_url_path_append("b") %>% | ||
#' req_url_path_append("search.html") %>% | ||
#' req_url_query(q = "the cool ice") | ||
#' | ||
#' # Change complete url | ||
#' req %>% | ||
#' req_url("http://google.com") | ||
req_url <- function(req, url) { | ||
if (inherits(url, "url")) { | ||
# Temporary fudging | ||
url <- httr::build_url(url) | ||
} | ||
req$url <- url | ||
req | ||
} | ||
|
||
|
||
req_url_get <- function(req) { | ||
httr::build_url(req$url) | ||
#' @export | ||
#' @rdname req_url | ||
#' @param ... Name-value pairs that provide query parameters. | ||
req_url_query <- function(req, ...) { | ||
url <- httr::parse_url(req$url) | ||
url$query <- modify_list(url$query, ...) | ||
req_url(req, url) | ||
} | ||
|
||
#' @export | ||
#' @rdname req_url | ||
#' @param path Path to replace or append to existing path. | ||
req_url_path <- function(req, path) { | ||
url <- httr::parse_url(req$url) | ||
url$path <- path | ||
|
||
req_url_set <- function(req, url) { | ||
req$url <- httr::parse_url(url) | ||
req | ||
req_url(req, url) | ||
} | ||
|
||
req_path_set <- function(req, path) { | ||
req$url$path <- path | ||
req | ||
} | ||
|
||
req_path_append <- function(req, path) { | ||
req$url$path <- paste0(req$url$path, "/", path) | ||
req | ||
#' @export | ||
#' @rdname req_url | ||
req_url_path_append <- function(req, path) { | ||
url <- httr::parse_url(req$url) | ||
url$path <- paste0(url$path, "/", path) | ||
req_url(req, url) | ||
} |
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
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
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,14 @@ | ||
test_that("can modify url in various ways", { | ||
req <- req("http://example.com") | ||
|
||
expect_equal(req_url(req, "http://foo.com:10")$url, "http://foo.com:10") | ||
|
||
expect_equal(req_url_path(req, "index.html")$url, "http://example.com/index.html") | ||
req2 <- req %>% | ||
req_url_path_append("a") %>% | ||
req_url_path_append("index.html") | ||
expect_equal(req2$url, "http://example.com/a/index.html") | ||
|
||
req2 <- req %>% req_url_query(a = 1, b = 2) | ||
expect_equal(req2$url, "http://example.com/?a=1&b=2") | ||
}) |