R/sections.R

Defines functions sections

Documented in sections

#' Get a list of available sections
#'
#' This will pull a list of all available sections
#'
#' @param bibleid Id of Bible whose Chapters to fetch
#' @param bookid Id of the Book whose Chapters to fetch
#' @param debug Used to help debug the query
#' @param apikey API.bible api key. Sign up here https://scripture.api.bible/signup
#'
#' @import dplyr
#' @import tibble
#' @importFrom glue glue
#' @importFrom purrr discard
#' @importFrom magrittr %>%
#' @importFrom httr RETRY
#' @importFrom httr add_headers
#' @importFrom httr verbose
#' @export
sections <- function(bookid = NULL,
                     bibleid = Sys.getenv('MAIN_BIBLEID'),
                     debug = FALSE,
                     apikey = Sys.getenv('BIBLER_APIKEY')) {

  baseurl <- 'https://api.scripture.api.bible/v1/bibles'

  req_url <- glue::glue('{baseurl}/{bibleid}/books/{bookid}/sections')

  #debug setup
  debug_call <- NULL

  if (debug) {
    debug_call <- httr::verbose(data_out = TRUE, data_in = TRUE, info = TRUE)
  }
  req <- httr::RETRY("GET",
                     url = req_url,
                     encode = "json",
                     debug_call,
                     httr::add_headers(
                       `api-key` = apikey
                     ))

  httr::stop_for_status(req, task = httr::content(req)$message)

  res1 <- httr::content(req)$data

  res2 <- tibble::as_tibble(do.call(rbind, res1))
  res2 %>%
    dplyr::mutate(across(.cols = everything(), as.character))
}
benrwoodard/bibler documentation built on Feb. 13, 2022, 1:26 p.m.