R/import_to_workspace.R

Defines functions import_to_workspace

Documented in import_to_workspace

#' Import Code to the Databricks Workspace
#'
#' This function allows you to import .R and .Rmd files into Databricks as an R notebook.  This will work whether the files
#' are on your local machine, on DBFS, or from an instance of RStudio that is hosted on a Databricks cluster.
#'
#' The API endpoint for importing files to the workspace is '2.0/workspace/import'.  For all details on API calls please see the official documentation at \url{https://docs.databricks.com/dev-tools/api/latest/}.
#'
#' @param local_file A string representing the path to a .R or .Rmd file.  Generated by \code{\link{create_job}} or via the Databricks web interface.
#' @param workspace A string representing the web workspace of your Databricks instance. E.g., "https://eastus2.azuredatabricks.net" or "https://company.cloud.databricks.com".
#' @param token A valid authentication token generated via User Settings in Databricks or via the Databricks REST API 2.0.  If none is provided, netrc will be used.
#' @param overwrite Replace an existing notebook?  Defaults to 'true', a string.
#' @param verbose If true, will pretty print the success or failure of the request.  Defaults to TRUE.
#' @return The API response.
#'
#' @examples
#' ## Import an R script from your local machine to a Notebook on Databricks
#' import_to_workspace(file = "C:/my_files/my_r_program.R",
#'                     notebook_path = "/Shared/R/my_r_program",
#'                     workspace = "https://company.cloud.databricks.com",
#'                     token = "dapi2130940djvafn98wefasdfjn")
#'
#'
import_to_workspace <- function(file, notebook_path,
                                workspace, token = NULL,
                                overwrite = "false", verbose = T) {

  ## Parameters for file upload
  files <- list(
    path = notebook_path,
    language = "R",
    content = httr::upload_file(file),
    overwrite = overwrite
  )

  if (is.null(token)) {

    ## Using netrc by default
    use_netrc <- httr::config(netrc = 1)
    res <- httr::with_config(use_netrc, {
      httr::POST(url = paste0(workspace, "/api/2.0/workspace/import"),
                 body = files)})
  }

  else {

    ## Authenticate with token
    headers <- c(
      Authorization = paste("Bearer", token)
    )

    ## Make request
    res <- httr::POST(url = paste0(workspace, "/api/2.0/workspace/import"),
                      httr::add_headers(.headers = headers),
                      body = files)
  }


  if (verbose == T) {
    ## Successful request message
    if (res$status_code[1] == 200) {

      message(paste0(
        "Status: ", res$status_code[1],
        "\n\nObject: ", file,
        " was added to the workspace at ", notebook_path
      ))

    }

    ## Unsuccessful request message
    else {
      message(paste0(
        "Status: ", res$status_code[1],
        "\nThe request was not successful:\n\n", jsonlite::prettify(res)
      ))
    }
  }
  ## Return API response
  res
}
RafiKurlansik/bricksteR documentation built on Oct. 13, 2022, 6:58 a.m.