R/auth.R

Defines functions auth_key auth_exist auth_check_work auth_check auth_set

Documented in auth_check auth_set

#' Set API key for auth.
#'
#' @param apikey sendgrid api key. If key has value, just use as api key. If without param, function call interactive prompt input.
#' @importFrom keyring key_set
#' @export
#' @return None
auth_set <- function(apikey) {
  if (missing(apikey)) {
    usethis::ui_todo(
      "Your API Key is at {usethis::ui_value('https://app.sendgrid.com/settings/api_keys')}"
    )

    Sys.sleep(1)

    keyring::key_set(service = "apikey",
                     username = "sendgridr")
  } else {
    keyring::key_set_with_value(service = "apikey",
                                username = "sendgridr",
                                password = apikey)
  }
}

#' Check API key for auth.
#'
#' @return TRUE/FALSE check work fine return TRUE.
#' @importFrom usethis ui_info
#' @export
auth_check <- function() {
  if (!auth_exist()) {
    usethis::ui_info("Api key is unset")
    return(FALSE)
  }

  return(auth_check_work())
}

#' @importFrom httr GET add_headers status_code
auth_check_work <- function() {
  tar <- "https://api.sendgrid.com/v3/api_keys"
  ahd <-
    httr::add_headers("Authorization" = paste0("Bearer ", auth_key()),
                      "content-type" = "application/json")
  chk <- httr::status_code(httr::GET(tar, ahd))
  return(chk == 200)
}

#' @importFrom keyring key_get
auth_exist <- function() {
  chk <- try(auth_key(), silent = T)
  return(!inherits(chk, "try-error"))
}

#' @importFrom keyring key_get
auth_key <- function() {
  keyring::key_get(service = "apikey",
                   username = "sendgridr")
}

Try the sendgridr package in your browser

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

sendgridr documentation built on Nov. 10, 2022, 5:15 p.m.