R/core.R

Defines functions gh_is_avialable build_api_url get_api_url gh_GET gh_set_api_url

Documented in gh_set_api_url

#' Set gh API base url
#' @note Internally it calls \code{Sys.setenv} to store the API url
#'   in an environment variable called \code{GH_API_URL}.
#' @param api_url API base url
#' @examples
#' gh_set_api_url("http://localhost:8989")
#' @export
gh_set_api_url <- function(api_url) {
  Sys.setenv(GH_API_URL = api_url)
}

gh_GET <- function(path, query = list(), ...) {
  if (is.null(query$key)) query$key <- Sys.getenv(GH_API_KEY)
  httr::GET(build_api_url(path), query = query, ...)
}

get_api_url <- function() {
  api_url <- Sys.getenv("GH_API_URL")
  ifelse(identical(api_url, ""), DEFAULT_API_URL, api_url)
}

build_api_url <- function(path) {
  sub("/$", "", get_api_url()) %>%
    paste0("/", path)
}

gh_is_avialable <- function() {
  tryCatch({
    httr::GET(get_api_url())
    invisible(TRUE)
    },
    error = function(e) {
      message(e$message)
      invisible(FALSE)
    }
  )
}
crazycapivara/graphhopper-r documentation built on July 19, 2021, 1:16 p.m.