R/auth.R

Defines functions accunews_key accunews_auth

Documented in accunews_key

#' Setup
#' 
#' Setup your session, all subsequent calls will be done using the API key.
#'
#' @param key Your API key, freely available at \url{https://www.accunewsapp.com/api/v1/docs/#authentication}.
#' 
#' @note You can specify \code{ACCUNEWS_API_KEY} as environment variable, likely in your \code{.Renviron} file.
#' 
#' @examples
#' \dontrun{
#' accunews_key("xXXxxXxXxXXx")  
#' }
#' 
#' @import purrr
#' @import httr
#' @import dplyr
#' @import assertthat
#' @import cli
#' @import crayon
#' 
#' @name authenticate
#' @export
accunews_key <- function(key){
  assert_that(!missing(key), msg = "Missing key")
  Sys.setenv(ACCUNEWS_API_KEY = key)
}

accunews_auth <- function(email, password){
  url <- parse_url(BASE_URL)
  url$path <- c(BASE_PATH, "auth")
  url <- build_url(url)

  response <- httr::POST(url, encode = "json", content_type_json(), body = list(email = email, password = password))
  stop_for_status(response)
  content <- content(response)
  accunews_key(content[["apiKey"]])
  invisible(content)
}
news-r/accunews documentation built on Feb. 27, 2020, 3:40 p.m.