R/get_figma_file_content.R

Defines functions get_figma_file_content

Documented in get_figma_file_content

# WARNING - Generated by {fusen} from /dev/flat_figma_api_connection.Rmd: do not edit by hand

#' Connect to Figma API and extract the raw content of a file.
#'
#' @param file_key Character. Key of the Figma file. 
#' @param acess_token Character. Figma access token.
#'
#' @importFrom httr VERB add_headers content
#' @importFrom stringr str_remove_all
#' @importFrom glue glue
#' @importFrom cli cli_alert_danger cli_alert_success
#'
#' @details
#' The key of your Figma file corresponds to the character 
#' wRqIvMmymzSPuj0sEhnORb in the url 
#' "https://www.figma.com/file/wRqIvMmymzSPuj0sEhnORb/..." (example).
#' The acess token can be created in your Figma account 
#' (https://www.figma.com/developers/api Section Access tokens)
#'
#' @return A list. Content of the file.
#' @export
#' @examples
#' raw_file_content <- get_figma_file_content(file_key = "wRqIvMmymzSPuj0sEhnORb",
#'                                          acess_token = Sys.getenv("FIGMA_TOKEN"))
get_figma_file_content <- function(file_key, acess_token = Sys.getenv("FIGMA_TOKEN")) {
  
  api_response <- VERB(verb = "GET", 
                       url = glue("https://api.figma.com/v1/files/{file_key}"), 
                       add_headers(`X-FIGMA-TOKEN` = acess_token), 
                       encode = "json")
  
  if (api_response$status_code == "404") {
    
    cli_alert_danger("Content of the file has not been extracted. Please check the file key or your access token.")
    
    stop()
    
  } else {
    
    file_content <- content(api_response)
    
    file_name <- file_content$name %>% 
      str_remove_all("[:punct:]")
    
    cli_alert_success(glue("Content of the file '{file_name}' has been successfully extracted."))
    
    return(file_content)
    
  }
  
}
ThinkR-open/swatch documentation built on April 7, 2022, 6:08 p.m.