R/list-docs.R

#' @export
search_docs <- function(.data, url, method = 'get', ..., parm_fun, npag_fun) {
  if(missing(.data)) {
    if(!missing(parm_fun)) {
      .data = parm_fun(...)
    } else {
      .data = list(...)
    }
  }
  if(method == 'get') {
    r <- httr::GET(url, query = .data,
                   config = list(ssl.verifypeer = FALSE))
  } else if(method == 'post') {
    r <- httr::POST(url, body = .data,
                    config = list(ssl.verifypeer = FALSE))
  } else if(is.function(method)) {
    r <- method(.data, url, ...)
  }
  if(r$status_code != 200) {
    stop('Request failed.')
  }
  if(!missing(npag_fun)) {
    npag <- npag_fun(r)
  } else {
    npag <- NULL
  }
  obj <- structure(
    list(result = r, url = url, data = .data, npag = npag, method = method),
    class = 'searchdoc'
  )
  return(obj)
}
jtrecenti/crawlr documentation built on May 20, 2019, 3:17 a.m.