R/parse_response.R

Defines functions parse_response

Documented in parse_response

#' Parse API Response
#'
#' This function parses the content of an HTTP response based on the specified format.
#' It supports JSON and plain text formats.
#'
#' @param response An HTTP response object.
#' @param format A string indicating the expected response format ("json" or "text").
#' @return Parsed content from the response.
#' @importFrom httr content
#' @importFrom jsonlite fromJSON
#' @export
parse_response <- function(response, format = "json") {
  parsed_content <- tryCatch(
    {
      if (format == "json") {
        fromJSON(content(response, "text", encoding = "UTF-8"))
      } else if (format == "text") {
        content(response, "text", encoding = "UTF-8")
      } else {
        stop("Unsupported format for parsing the response: ", format)
      }
    },
    error = function(e) {
      stop("Parsing Error: Failed to parse the response. Error: ", e$message)
    }
  )

  return(parsed_content)
}

Try the rPDBapi package in your browser

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

rPDBapi documentation built on Sept. 11, 2024, 6:37 p.m.