R/utils.R

Defines functions base_url get_path_url .get_results get_results_paged check_token

#' API base URL
#' @return API base URL (character)
#' @noRd
base_url <- function() {
  "https://api.hubapi.com"
}

#' @param path An API endpoint path
#' @return The URL to that API endpoint (character)
#' @noRd
get_path_url <- function(path) {
  httr::modify_url(base_url(),
    path = path
  )
}

#' @param path API endpoint path (character)
#' @param apikey API key (character)
#' @param token_path Path to cached token (character)
#' @param query Query parameters (named list)
#' @return A list
#' @noRd
.get_results <- function(path, apikey, token_path,
                         query = NULL) {
  auth <- hubspot_auth(
    token_path = token_path,
    apikey = apikey
  )

  # remove NULL elements from the query
  query <- purrr::discard(query, is.null)

  # auth
  if (auth$auth == "key") {
    query$hapikey <- auth$value
    res <- httr::GET(get_path_url(path),
      query = query,
      httr::user_agent("hubspot R package by Locke Data")
    )
  } else {
    token <- readRDS(auth$value)

    token <- check_token(token, file = auth$value)

    res <- httr::GET(get_path_url(path),
      query = query,
      httr::config(httr::user_agent("hubspot R package by Locke Data"),
        token = token
      )
    )
  }
  httr::warn_for_status(res)
  res %>% httr::content()
}

get_results <- ratelimitr::limit_rate(
  .get_results,
  ratelimitr::rate(100, 10)
)

#' @param path API endpoint path (character)
#' @param apikey API key (character)
#' @param query Query parameters (named list)
#' @param max_iter Maximal number of iterations (integer)
#' @param element Element to retrieve from API raw results (character)
#' @param hasmore_name Name of the has-more parameter for the API
#'                     endpoint (character)
#' @param offset_name_in Name of the offset parameter to send to the API
#' @param offset_name_out Name of the offset parameter returned
#' @return A list
#' @noRd
get_results_paged <- function(path, token_path, apikey, query = NULL,
                              max_iter = max_iter, element,
                              hasmore_name,
                              offset_name_in = "offset",
                              offset_name_out = "offset") {
  results <- list()
  n <- 0
  do <- TRUE
  offset <- 0

  while (do & n < max_iter) {
    query[[offset_name_in]] <- offset

    res_content <- get_results(
      path = path,
      token_path = token_path,
      apikey = apikey,
      query = query
    )
    n <- n + 1

    results[n] <- list(res_content[[element]])
    do <- res_content[[hasmore_name]]
    offset <- res_content[[offset_name_out]]
  }

  results <- purrr::flatten(results)

  return(results)
}


check_token <- function(token, file) {
  info <- httr::GET(get_path_url(
    glue::glue("/oauth/v1/access-tokens/{
               token$credentials$access_token}")
  )) %>%
    httr::content()

  if ("message" %in% names(info)) {
    if (grepl("expired", info$message)) {
      token$refresh()
      saveRDS(token, file)
    }
  }

  if ("expires_in" %in% names(info)) {
    if (info$expires_in < 60) {
      token$refresh()
      saveRDS(token, file)
    }
  }


  token
}
lockedata/hubspot documentation built on March 1, 2020, 8:54 p.m.