R/validations.R

Defines functions check_for_api_error is_blank is_na is_null is_url

is_url <- function(url) {
  regex <-
    "http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+"
  is_url <-
    grepl(regex, url)
  if (!is_url)
    stop(error_is_not_a_url)
}

is_null <- function(parameters) {
  any(
    unlist(
      lapply(parameters, is.null)
    )
  )
}

is_na <- function(parameters) {
  any(
    unlist(
      lapply(parameters, is.na)
    )
  )
}

is_blank <- function(parameters) {
  any(
    unlist(
      lapply(parameters, function(x) x == "")
    )
  )
}

check_for_api_error <- function(api_response) {
  response <- httr::content(api_response)

  if (api_response$status_code %in% 200:204) {
    response <-
      list(
        status = "success",
        response = response
      )
    return(response)
  }

  error <- response$error
  response <-
    list(
      status = "error",
      error_code = error$code,
      error_summary = error$summary,
      error_detail = error$detail
    )
  return(response)
}
tom-ioki/tableauR documentation built on Dec. 31, 2020, 8:43 a.m.