R/buildServiceRequest.R

Defines functions .buildServiceRequest

#' Build a request to a service for the DistillerSR API.
#' 
#' This helper function constructs and configures a request object for a
#' DistillerSR API service using the `httr2` package.
#' 
#' @param serviceUrl `character` (string). The URL of the service endpoint.
#' 
#' @param distillerToken `character` (string). The personal access token
#'   generated by the authentication request.
#' 
#' @param body `list` (optional). A list containing the body parameters to be
#'   encoded into JSON format.
#' 
#'   By default: NULL (no body).
#'
#' @param timeout `integer`. The maximum number of seconds to wait for the
#'   response.
#' 
#'   By default: 1800 seconds (30 minutes).
#' 
#' @return An `httr2_request` object representing the configured service
#'   request.
#' 
#' @details
#' The function sets the HTTP method, sets the timeout, and includes the
#' following headers:
#' \itemize{
#'  \item `Authorization` - the Bearer token used for authentication.
#' }
#' 
#' @importFrom checkmate assert_string assert_list assert_int
#' @importFrom httr2 request req_method req_headers req_timeout req_body_json
#' 
#' @keywords internal
#' @noRd
#' 
.buildServiceRequest <- function(
  serviceUrl,
  distillerToken,
  body = NULL,
  timeout = 1800) {
  
  assert_string(serviceUrl, pattern = "[^/]$")
  assert_string(distillerToken)
  if (!is.null(body)) { assert_list(body) }
  assert_int(timeout)
  
  request_ <- request(serviceUrl) |>
    req_headers(
      "Authorization" = paste("Bearer", distillerToken)
    ) |>
    req_timeout(timeout)
  
  if (is.null(body)) {
    request_ <- request_ |>
      req_method("GET") |>
      req_headers(
        "Content-Type" = "application/octet-stream"
      )
  } else {
    request_ <- request_ |>
      req_method("POST") |>
      req_headers(
        "Content-Type" = "application/json"
      ) |>
      req_body_json(body)
  }
  
  return(request_)
}

Try the distilleR package in your browser

Any scripts or data that you put into this service are public.

distilleR documentation built on April 24, 2026, 1:07 a.m.