R/auth.R

Defines functions print_auth get_auth set_auth

Documented in get_auth print_auth set_auth

#' @title set access token
#'
#' @description
#' Set access token (hash key) using kis app key and kis app secret.
#'
#' @return An access token (access token is stored in environment variable
#' at the same time)
#'
#' @examples
#' # set authentification
#' \dontrun{
#' set_auth()
#' }
#'
#' @export
set_auth <- function() {
  is_paper <- is_paper_trading()
  if (!is_paper) {
    access_token <- Sys.getenv("KIS_ACCESS_TOKEN")
    access_token_expired <- Sys.getenv("KIS_ACCESS_TOKEN_EXPIRED")
  } else {
    access_token <- Sys.getenv("KIS_PAPER_ACCESS_TOKEN")
    access_token_expired <- Sys.getenv("KIS_PAPER_ACCESS_TOKEN_EXPIRED")
  }
  if (access_token == "") {
    generate_auth()
  } else {
    if (access_token_expired == "") {
      generate_auth()
    } else if ((as.POSIXct(access_token_expired) - Sys.time()) < 0) {
      generate_auth()
    } else {
      return(access_token)
    }
  }
}

##' @rdname set_auth
##' @export
get_auth <- function() set_auth()

##' @rdname set_auth
##' @export
print_auth <- function() {
  Sys.getenv("KIS_ACCESS_TOKEN")
}

Try the kisopenapi package in your browser

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

kisopenapi documentation built on May 29, 2024, 5:26 a.m.