R/custom.R

Defines functions vsts_run_command

Documented in vsts_run_command

#' Azure DevOps Custom API Calls
#'
#' @description
#' For any requirement not currently in place in the \code{vstsr} package, then this function will allow you
#' to use the relevant API call without any extra requirements.
#'
#' For the most part it is just a shell of \code{\link[httr]{VERB}} but will have the \code{auth_key} set up already.
#'
#' @param url the URI of the API call to run
#' @param verb name of the verb to use
#' @param auth_key authentication key generated by using \code{\link{vsts_auth_key}}
#' @param body check \code{\link[httr]{VERB}} for more details. If the object is a named list, then it will be
#' transformed into a JSON string so that can be added to the call. Use
#' \url{https://docs.microsoft.com/en-us/rest/api/azure/devops} to find out any required parameter for the body.
#' @param query a list of extra parameters that can be sent to the API call. If not required then leave as \code{NULL}
#'
#' @examples
#' \dontrun{
#' auth_key <- vsts_auth_key("<username>", "<password>")
#' # Get commits of a repository
#' URL <- file.path(
#'   "https://dev.azure.com",
#'   domain,
#'   project,
#'   "_apis/git/repositories",
#'   repository_id,
#'   "commits?api-version=5.0"
#' )
#' vsts_run_command(URL, "GET", auth_key)
#' }
#'
#' @export
vsts_run_command <- function(url, verb, auth_key, body = NULL, query = NULL) {
  if (!is.null(body)) {
    content_body <- jsonlite::toJSON(body, auto_unbox = TRUE)
  } else {
    content_body <- NULL
  }

  response <- httr::VERB(
    verb = verb,
    url = url,
    config = httr::add_headers(Authorization = auth_key),
    httr::content_type_json(),
    query = query,
    body = content_body
  )

  if (httr::status_code(response) >= 300) {
    send_failure_message(response, "run custom command")
    return(invisible(NULL))
  }

  httr::content(response, encoding = "UTF-8", simplifyDataFrame = TRUE)
}
ashbaldry/vstsr documentation built on Aug. 24, 2023, 12:11 a.m.