Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation for url_build() #550

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions R/url.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
#' * `url_parse()` returns a URL: a S3 list with class `httr2_url`
#' and elements `scheme`, `hostname`, `port`, `path`, `fragment`, `query`,
#' `username`, `password`.
#'
#' @details
#' `url_build()` accepts a named list. Valid element names are `scheme`, `hostname`, `port`, `path`, `fragment`, `query`,
#' `username`, `password`.
#'
#' @export
#' @examples
#' url_parse("http://google.com/")
Expand Down Expand Up @@ -112,6 +117,49 @@
#' @export
#' @rdname url_parse
url_build <- function(url) {

valid_url_parts <- c(
"query",
"username",
"password",
"hostname",
"port",
"authority",
"path",
"scheme",
"fragment"
)


if (!rlang::is_list(url)) {
cli::cli_abort("Expected {.cls} found {obj_type_friendly(url)}")

Check warning on line 135 in R/url.R

View check run for this annotation

Codecov / codecov/patch

R/url.R#L135

Added line #L135 was not covered by tests
}

if (!rlang::is_named(url)) {
cli::cli_abort(
c(
"{.arg url} must be a named list",
i = "url elements can be any of {.val {valid_url_parts}}"
)
)

Check warning on line 144 in R/url.R

View check run for this annotation

Codecov / codecov/patch

R/url.R#L139-L144

Added lines #L139 - L144 were not covered by tests
}

# extract names from the url list
url_names <- rlang::names2(url)

# identify which ones are invalid
invalid_elements <- !url_names %in% valid_url_parts

# check that the elements are named appropriately
if (anyNA(invalid_elements)) {
cli::cli_abort(
c(
"Invalid url elements in {.arg url}",
i = "Found {.val {url_names[invalid_elements]}} but expected one of {.arg {valid_url_parts}}"
)
)

Check warning on line 160 in R/url.R

View check run for this annotation

Codecov / codecov/patch

R/url.R#L155-L160

Added lines #L155 - L160 were not covered by tests
}

if (!is.null(url$query)) {
query <- query_build(url$query)
} else {
Expand Down
4 changes: 4 additions & 0 deletions man/url_parse.Rd

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

Loading