Skip to content

Commit

Permalink
feat: use helperfunctions to authenticate
Browse files Browse the repository at this point in the history
  • Loading branch information
tin900 committed Jan 12, 2024
1 parent a411d75 commit a24606e
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 23 deletions.
47 changes: 41 additions & 6 deletions R/authenticate_PAT.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
#'
#' This function authenticates to a Tableau Server or Tableau Cloud using a personal access token (PAT).
#'
#' @param pat_name The name of the personal access token (PAT).
#' @param pat_secret The secret of the personal access token (PAT).
#' @param content_url The URL of the content to authenticate. For Tableau Server, this is typically the URL of the Tableau server followed by the site ID. For Tableau Cloud, this is usually the URL of the Tableau cloud workbook.
#' @param base_url The base URL of the Tableau server or Tableau cloud. For Tableau Server, this is usually the URL of the Tableau server. For Tableau Cloud, this is usually the URL of the Tableau cloud, and it must contain the pod name, such as 10az, 10ay, or us-east-1. For example, the base URL to sign in to a site in the 10ay pod would be: https://10ay.online.tableau.com.
#' @param api_version the api version to use. Default is 3.4
#' @param pat_name The name of the personal access token (PAT). Defaults to the `TABLEAU_PAT_NAME` environment variable.
#' @param pat_secret The secret of the personal access token (PAT). Defaults to the `TABLEAU_PAT_SECRET` environment variable.
#' @param content_url The URL of the content to authenticate. Defaults to the `TABLEAU_CONTENT_URL` environment variable.
#' @param base_url The base URL of the Tableau server or Tableau cloud. Defaults to the `TABLEAU_BASE_URL` environment variable.
#' @param api_version The API version to use. Default is 3.4.
#' @return A list containing the base URL, the access token, the site ID, and the user ID.
#' @export
#' @family Tableau REST API
authenticate_PAT <- function(pat_name, pat_secret, content_url, base_url, api_version = 3.4) {
authenticate_PAT <- function(pat_name = tableau_pat_name(), pat_secret = tableau_pat_secret(), content_url = tableau_content_url(), base_url = tableau_base_url(), api_version = 3.4) {
constructXML <- function(pat_name, pat_secret, content_url) {
credentials <- paste0(
"<credentials personalAccessTokenName=\"",
Expand Down Expand Up @@ -60,3 +60,38 @@ authenticate_PAT <- function(pat_name, pat_secret, content_url, base_url, api_ve

return(tableau)
}


#' Get the Tableau PAT name from the environment variable
#'
#' @return The Tableau PAT name stored in the TABLEAU_PAT_NAME environment variable.
tableau_pat_name <- function() {
pat_name <- Sys.getenv("TABLEAU_PAT_NAME")
if (pat_name == "") {
stop("TABLEAU_PAT_NAME environment variable is not set.")
}
return(pat_name)
}

#' Get the Tableau PAT secret from the environment variable
#'
#' @return The Tableau PAT secret stored in the TABLEAU_PAT_SECRET environment variable.
tableau_pat_secret <- function() {
pat_secret <- Sys.getenv("TABLEAU_PAT_SECRET")
if (pat_secret == "") {
stop("TABLEAU_PAT_SECRET environment variable is not set.")
}
return(pat_secret)
}

#' Get the Tableau content URL from the environment variable
#'
#' @return The Tableau content URL stored in the TABLEAU_CONTENT_URL environment variable.
tableau_content_url <- function() {
content_url <- Sys.getenv("TABLEAU_CONTENT_URL")
if (content_url == "") {
stop("TABLEAU_CONTENT_URL environment variable is not set.")
}
return(content_url)
}

41 changes: 37 additions & 4 deletions R/authenticate_server.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
#'
#' Authenticates the user on the Tableau server and retrieves the necessary authentication variables for running other Tableau REST API methods.
#'
#' @param username The username on the Tableau server.
#' @param password The password on the Tableau server.
#' @param base_url The base URL of the Tableau server.
#' @param username The username on the Tableau server. Defaults to the `TABLEAU_USERNAME` environment variable.
#' @param password The password on the Tableau server. Defaults to the `TABLEAU_PASSWORD` environment variable.
#' @param base_url The base URL of the Tableau server. Defaults to the `TABLEAU_BASE_URL` environment variable.
#' @param api_version The API version to use (default: 3.4).
#'
#' @return A list containing the authentication variables: base_url, token, site_id, and user_id.
#' @export
#'
#' @family Tableau REST API
authenticate_server <- function(username, password, base_url, api_version = 3.4) {
authenticate_server <- function(username = tableau_username(), password = tableau_password(), base_url = tableau_base_url(), api_version = 3.4) {
tableau <- list(base_url = base_url)

# Check if the base URL ends with a trailing slash and remove it if present
Expand Down Expand Up @@ -69,3 +69,36 @@ authenticate_server <- function(username, password, base_url, api_version = 3.4)

return(tableau)
}

#' Get the Tableau username from the environment variable
#'
#' @return The Tableau username stored in the TABLEAU_USERNAME environment variable.
tableau_username <- function() {
username <- Sys.getenv("TABLEAU_USERNAME")
if (username == "") {
stop("TABLEAU_USERNAME environment variable is not set.")
}
return(username)
}

#' Get the Tableau password from the environment variable
#'
#' @return The Tableau password stored in the TABLEAU_PASSWORD environment variable.
tableau_password <- function() {
password <- Sys.getenv("TABLEAU_PASSWORD")
if (password == "") {
stop("TABLEAU_PASSWORD environment variable is not set.")
}
return(password)
}

#' Get the Tableau base URL from the environment variable
#'
#' @return The Tableau base URL stored in the TABLEAU_BASE_URL environment variable.
tableau_base_url <- function() {
base_url <- Sys.getenv("TABLEAU_BASE_URL")
if (base_url == "") {
stop("TABLEAU_BASE_URL environment variable is not set.")
}
return(base_url)
}
6 changes: 6 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ reference:
- escape_special_chars
- row_to_name
- row_to_query
- tableau_base_url
- tableau_content_url
- tableau_password
- tableau_pat_name
- tableau_pat_secret
- tableau_username

- title: Local workbook methods
desc: Functions that edit local Tableau Workbooks using XML structure.
Expand Down
18 changes: 9 additions & 9 deletions man/authenticate_PAT.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions man/authenticate_server.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/tableau_base_url.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/tableau_content_url.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/tableau_password.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/tableau_pat_name.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/tableau_pat_secret.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/tableau_username.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a24606e

Please sign in to comment.