R/01_get_cubes.R

#' Obtain available cubes
#' @export
#' @keywords functions

get_cubes <- function() {
  ua <- user_agent("httr")

  url <- "https://chilecube.datawheel.us/cubes"

  resp <- GET(url, ua)

  if (http_type(resp) != "application/json") {
    stop("API did not return json", call. = FALSE)
  }

  parsed <- fromJSON(content(resp, "text"), simplifyVector = TRUE)

  if (http_error(resp)) {
    stop(
      sprintf(
        "DataChile API request failed [%s]\n%s\n<%s>",
        status_code(resp),
        parsed$message,
        parsed$documentation_url
      ),
      call. = FALSE
    )
  }

  parsed <- tibble(cube = parsed$cubes$name) %>% distinct()

  structure(
    list(
      cubes = parsed,
      path = url,
      response = resp
    ),
    class = "datachile_api"
  )
}
Datawheel/datachile-r documentation built on May 26, 2019, 7:24 a.m.