R/dim_login.R

Defines functions dim_login

Documented in dim_login

#'Creates a token in the Global environment to be use in querying Dimensions API.
#'@param username A string containing the account username.
#'@param password A string containing the account password.
#'@param authep A string containing the authorization endpoint URL address.
#'By default 'https://app.dimensions.ai/api/auth.json'.
#'@return An API token object. This is assigned directly to the Global environment.
#'@examples > dim_login(username = 'name@email.com', password = 'yourpassword')

dim_login <- function(username, password, authep = 'https://app.dimensions.ai/api/auth.json'){

  #Create login list item
  login <- list(username = username,
                password = password,
                submit = 'Login!')

  #Endpoint check
  ep_check <- RCurl::url.exists(url = authep)

  #If the endpoint is not a valid URL (ep_check == TRUE) then issue warning and stop.
  if(ep_check == TRUE){
    warning(error_messages['404'])
    stop()}#close if

  #Response item
  response <- httr::POST(authep,
                         body = login,
                         encode = 'json')

  status = response$status_code

  #If status indicates unsuccessful
  if(status != 200){
    #And the status code is defined in our list of error messages, produce corresponding message.
    if(paste(status) %in% names(error_messages)){
      message(error_messages[paste(status)])} #close if
    #Otherwise, flag the status for reporting to support
    else{warning(paste('Please contact Dimensions support and reference error:', httr::content(response)$error[1]))}
  }#Close if

  #If status code indicates success, say so and save token to .GlobalEnv
  else {
    message('Login successful!')
    assign('dim_token', httr::content(response)$token, .GlobalEnv)} #Close else

} #Close function
cheneypinata/dslr documentation built on Jan. 6, 2022, 11:27 p.m.