R/fct_api_auth.R

Defines functions api_auth

Documented in api_auth

#' Authenticate with Tomorrow.io
#' 
#' @return `NULL`. Sets request configuration using related key defined in the user's R environment.
#' @seealso Check out the Tomorrow.io API to get started and create a token: 'https://docs.tomorrow.io/reference/welcome'
#' @export
api_auth <- function() {
  
  user_id = Sys.getenv("TOMORROWIO_KEY")
  
  if (nchar(user_id) == 0 | is.null(user_id)) {
    rlang::abort("Could not find a valid API key in the R environment. Please ensure that 'TOMORROWIO_KEY' is defined within the '.Renviron' file. (Edit your .Renviron with `usethis::edit_r_environ()`)")
  }
  
  ## Create an authentication header using an api key stored in the user's R environment file and set request config
  httr::add_headers(
    "apikey" = user_id,
    "content-type" = "application/json"
  ) %>%
    httr::set_config()
  
  return(invisible())
  
}
jasoncareyco95/R-SkyBox documentation built on Dec. 20, 2021, 9:07 p.m.