R/ac_get_custom_contact_fields.R

Defines functions ac_get_custom_contact_fields

Documented in ac_get_custom_contact_fields

#' Retrieve Dictionary of Contacts Custom Fields
#'
#' @return tibble with dictionary of contacts field metadata
#' @export
#'
#' @examples
#' \dontrun{
#' Sys.setenv('ACTIVECAMPAGN_API_TOKEN' = "YOUR_TOKEN")
#' Sys.setenv('ACTIVECAMPAGN_API_URL' = "https://<your-account>.api-us1.com")
#'
#' contacts <- ac_get_custom_contact_fields()
#' }
ac_get_custom_contact_fields <- function() {

  ac_check_auth()

  # vars
  is_first_iteration <- TRUE
  limit  <- 100
  offset <- 0
  total  <- NA
  res    <- list()

  while ( (is.na(total) | offset <= total) | is_first_iteration  ) {

    # send request
    ans <- GET(str_glue("{Sys.getenv('ACTIVECAMPAGN_API_URL')}/api/3/fields"),
               query = list(limit  = limit,
                            offset = offset),
               add_headers("Api-Token" = Sys.getenv('ACTIVECAMPAGN_API_TOKEN')))

    data <- content(ans)

    if ( status_code(ans) > 299 ) {
      stop(data$message)
    }

    out_data <- tibble(data = data$fields) %>%
             unnest_wider(data)

    is_first_iteration <- FALSE
    offset <- offset + limit
    total  <- as.numeric(data$meta$total)
    res <- append(res, list(out_data))

    Sys.sleep(0.25)

  }

  res <- bind_rows(res)

  return(res)
}

Try the ractivecampaign package in your browser

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

ractivecampaign documentation built on Nov. 12, 2021, 5:07 p.m.