R/syn_rest_put.R

Defines functions rest_PUT

Documented in rest_PUT

#' Synapse rest PUT command
#'
#' @param path Path to synapse endpoint
#' @param body Request Body
#'
#' @return Synapse API class
#' @importFrom httr modify_url PUT http_type status_code
#' @importFrom jsonlite fromJSON
#' @export
#'
#' @examples
#' rest_PUT("entity/syn26896636", body = list(...))
rest_PUT <- function(path, body) {
  url <- httr::modify_url("https://repo-prod.prod.sagebase.org/repo/v1",
    hostname = "repo-prod.prod.sagebase.org/repo/v1",
    path = path
  )

  # resp <- httr::PUT(url, ua, body = body,
  #                   httr::add_headers(Authorization = paste0("Bearer ", synapse_pat)),
  #                   encode = "json")
  resp <- httr::RETRY("PUT", url, syn_global$ua,
    body = body,
    httr::add_headers(Authorization = paste0("Bearer ", syn_global$synapse_pat)),
    encode = "json",
    terminate_on = c(400, 401, 403, 404)
  )

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

  parsed <- jsonlite::fromJSON(httr::content(resp, "text"), simplifyVector = FALSE)

  if (!httr::status_code(resp) %in% c(200, 201)) {
    stop(
      sprintf(
        "Synapse rest GET request failed [%s]\n%s",
        httr::status_code(resp),
        parsed$reason
      ),
      call. = FALSE
    )
  }

  structure(
    list(
      content = parsed,
      path = path,
      response = resp
    ),
    class = "synapse_api"
  )
}
thomasyu888/puresynapser documentation built on Jan. 30, 2022, 12:14 p.m.