Skip to content

Instantly share code, notes, and snippets.

@karthik
Created December 6, 2012 18:49
Show Gist options
  • Save karthik/4227024 to your computer and use it in GitHub Desktop.
Save karthik/4227024 to your computer and use it in GitHub Desktop.
A function to test API status and return a logical TRUE if it responds.
# This is required for evalWithTimeout
suppressPackageStartupMessages(library(R.utils))
#'Function to test whether an API is down and if so, throw an error.
#'
#' @param test_fn The function call to test
#' @param tlimit = 120 A timeout in seconds
#' @return logical
#' @examples \dontrun{
#' api_status(eol_search('Salix'))
#'}
api_status <- function(test_fn, tlimit = 120) {
results <- tryCatch(expr = evalWithTimeout(test_fn, timeout = tlimit), TimeoutException = function(ex) "TimedOut")
status <- ifelse(is(results, "TimedOut"), FALSE, TRUE)
status
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment