R/execute_event_subscription.R

Defines functions execute_event_subscription

Documented in execute_event_subscription

#' Execute event subscription
#'
#' @param subscriptionId (numeric) Event subscription identifier
#' @param env (character) Repository environment. Can be: "production",
#' "staging", or "development".
#'
#' @return (logical) TRUE if the event subscription was executed
#'
#' @details Upon notification, the event manager queries its database for the
#' subscription matching the specified subscriptionId. POST requests are then
#' made (asynchronously) to the matching subscription.
#'
#' @note User authentication is required (see \code{login()})
#' 
#' @family Event Notifications
#'
#' @export
#'
#' @examples
#' \dontrun{
#'
#' login()
#'
#' # Create subscription
#' subscriptionId <- create_event_subscription(
#'   packageId = "knb-lter-vcr.340.1",
#'   url = "https://my.webserver.org/",
#'   env = "staging"
#' )
#' subscriptionId
#' #> [1] 48
#'
#' # Execute subscription
#' execute_event_subscription(
#'   subscriptionId = subscriptionId,
#'   env = "staging"
#' )
#' #> [1] TRUE
#'
#' # Delete subscription
#' delete_event_subscription(subscriptionId, env = "staging")
#' #> [1] TRUE
#'
#' logout()
#' }
#'
execute_event_subscription <- function(subscriptionId, env = "production") {
  url <- paste0(
    base_url(env), "/package/event/eml/",
    subscriptionId
  )
  cookie <- bake_cookie()
  resp <- httr::POST(
    url,
    set_user_agent(),
    cookie,
    handle = httr::handle("")
  )
  msg <- httr::content(resp, as = "text", encoding = "UTF-8")
  httr::stop_for_status(resp, msg)
  if (resp$status_code == "200") {
    return(TRUE)
  } else {
    return(FALSE)
  }
}
ropensci/EDIutils documentation built on Oct. 19, 2023, 7:21 p.m.