library(testthat)
library(purrr)
library(httr)
library(glue)
library(stringr)
pkgload::load_all()

Connection to Figma API and extraction of the content of a file: get_file_content() function

Connection to Figma API

Extraction of the content of a file (in the form of a list)

#' 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
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)

  }

}
raw_file_content <- get_figma_file_content(file_key = "wRqIvMmymzSPuj0sEhnORb",
                                         acess_token = Sys.getenv("FIGMA_TOKEN"))
test_that("get_figma_file_content works", {

  # No error
  expect_error(file_content <- get_figma_file_content(file_key = "wRqIvMmymzSPuj0sEhnORb",
                                                    acess_token = Sys.getenv("FIGMA_TOKEN")), 
               regexp = NA)

  expect_error(file_content <- get_figma_file_content(file_key = "wRqIvMmymzSPuj0sEhnORb"), 
               regexp = NA)

  expect_equal(length(file_content), 
               12)

  # Error
  expect_error(file_content <- get_figma_file_content(acess_token = ""))

  expect_error(file_content <- get_figma_file_content(file_key = ""))

  expect_error(file_content <- get_figma_file_content(file_key = "", 
                                                    acess_token = ""))

})
# Run but keep eval=FALSE to avoid infinite loop
# Execute in the console directly
fusen::inflate(flat_file = "dev/flat_figma_api_connection.Rmd", 
               vignette_name = "b - Figma API connection and file extraction", 
               open_vignette = FALSE,
               check = FALSE,
               overwrite = TRUE)


ThinkR-open/swatch documentation built on April 7, 2022, 6:08 p.m.