R/util.R

Defines functions check_product_id check_retailer_id param_boolean check_response_error check_response_json

Documented in check_response_error check_response_json param_boolean

#' Check that API response is JSON
#'
#' @param response A response object.
#'
#' @return NULL. Raises an error if the response is not JSON.
check_response_json <- function(response) {
  if (http_type(response) != "application/json") {
    stop("API did not return json.", call. = FALSE)
  }
}

#' Check API response for errors
#'
#' @param response A response object.
#'
#' @return NULL. Raises an error if response has an error code.
check_response_error <- function(response) {
  if (http_error(response)) {
    status <- status_code(response)
    error = glue("API request failed [{status}]")

    message <- content(response)$message
    if (!is.null(message)) {
      error = glue("{error}: {message}")
    }

    stop(error, call. = FALSE
    )
  }
}

#' Convert URL parameter into Boolean.
#'
#' @param bool A Boolean string (either "true" or "false").
#'
#' @return A Boolean value (either TRUE or FALSE).
param_boolean <- function(bool) {
  ifelse(bool, "true", "false")
}

check_retailer_id <- function(retailer_id) {
  if (!is.integer(retailer_id) && !is.numeric(retailer_id)) {
    stop("Retailer ID must be a number.", call. = FALSE)
  }
}

check_product_id <- function(product_id) {
  if (!is.integer(product_id) && !is.numeric(product_id)) {
    stop("Product ID must be a number.", call. = FALSE)
  }
}

Try the trundler package in your browser

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

trundler documentation built on July 29, 2020, 1:06 a.m.