R/query.R

Defines functions make_query has_name has_namez encode

# adapted from https://github.com/hadley/httr
encode <- function(x) {
  if (inherits(x, "AsIs"))
    return(x)
  curl::curl_escape(x)
}

has_namez <- function(x) {
  length(Filter(nzchar, names(x))) == length(x)
}

# adapted from https://github.com/hadley/httr
has_name <- function(x) {
  nms <- names(x)
  if (is.null(nms)) return(rep(FALSE, length(x)))
  !is.na(nms) & nms != ""
}

# adapted from https://github.com/hadley/httr
make_query <- function(x) {
  if (length(x) == 0) {
    return("")
  }
  if (!all(has_name(x))) {
    stop("All components of query must be named", call. = FALSE)
  }
  stopifnot(is.list(x))
  x <- ccp(x)
  names <- curl::curl_escape(names(x))
  values <- vapply(x, encode, character(1))
  paste0(names, "=", values, collapse = "&")
}

Try the crul package in your browser

Any scripts or data that you put into this service are public.

crul documentation built on May 31, 2023, 5:42 p.m.