R/requests.R

#' Generalized DELETE method
#'
#' @param path Path of the HTTP request.
#' @param \dots Additional parameters to be passed to \code{build_url}
#'   as additional named components in the URL query.
#'
#' @return Status code of the API response.
#'
#' @keywords internal
DELETE_resource <- function(path, ...) {

  url <- build_url(path, ...)

  httr::DELETE(url, gc_token()) %>%
    httr::stop_for_status() %>%
    httr::status_code()

}

#' Generalized GET method
#'
#' @param path Path of the HTTP request.
#' @param fields Fields to be included in the response, as a character
#'   vector.
#'
#' @return If request was successful, the API response. Otherwise, an
#'   error or warning, as generated by \code{httr::stop_for_status}.
#'
#' @keywords internal
GET_resource <- function(path, fields = NULL) {

  url <- build_url(path, fields = fields)

  httr::GET(url, gc_token()) %>%
    httr::stop_for_status()

}

#' Generalized PATCH method
#'
#' @param path Path of the HTTP request.
#' @param body Request body to be encoded as JSON, as a named list.
#'
#' @return If request was successful, an invisible copy of the response.
#'   In this package, the PATCH return value is rarely used; instead,
#'   a GET method is usually used to refetch the appropriate object.
#'
#' @keywords internal
PATCH_resource <- function(path, body) {

  url <- build_url(path, fields = "id")

  httr::PATCH(url, gc_token(), encode = "json", body = body) %>%
    httr::stop_for_status() %>%
    invisible()

}

#' Generalized POST method
#'
#' @param path Path of the HTTP request.
#' @param body Request body to be encoded as JSON, as a named list.
#' @param \dots Additional parameters to be passed to \code{build_url}
#'   as additional named components of the URL query.
#'
#' @return If request was successful, the API response. Otherwise, an
#'   error or warning, as generated by \code{httr::stop_for_status}.
#'
#' @keywords internal
POST_resource <- function(path, body, ...) {

  url <- build_url(path, fields = "id", ...)

  httr::POST(url, gc_token(), encode = "json", body = body) %>%
    httr::stop_for_status()

}
benjcunningham/googlecalendar documentation built on May 12, 2019, 11:56 a.m.