R/api_tools.R

Defines functions api_call_wrapper

library(httr)

api_call_wrapper <- function(api_url, ..., method = "POST") {
  result <- tryCatch({
    if (method == "POST") {
      response <- POST(api_url, ...)
    } else if (method == "GET") {
      response <- GET(api_url, ...)
    } else {
      stop("Invalid HTTP method: ", method)
      return(NULL)
    }

    if (status_code(response) >= 200 && status_code(response) < 300) {
      return(response)
    } else {
      stop("API request failed with status code: ", status_code(response))
      return(NULL)
    }
  }, error = function(e) {
    warning("Failed to fetch data from API: ", e$message)
    return(NULL)
  })

  return(result)
}

Try the dhlabR package in your browser

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

dhlabR documentation built on Sept. 11, 2024, 9:12 p.m.