File: download.R

package info (click to toggle)
r-cran-jsonld 2.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 892 kB
  • sloc: javascript: 12,209; sh: 19; makefile: 13
file content (25 lines) | stat: -rw-r--r-- 804 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
download <- function(url){
  h <- curl::new_handle()
  if(isTRUE(getOption('jsonld_use_accept', TRUE)))
    curl::handle_setheaders(h, "Accept" = "application/ld+json, application/json")
  req <- curl::curl_fetch_memory(url, handle = h)
  if(req$status >= 400){
    json <- rawToChar(req$content)
    if(jsonlite::validate(json)){
      data <- jsonlite::fromJSON(json)
      if(length(data$message))
        stop(sprintf("(%s) %s", url, data$message))
    }
    stop(sprintf("Download (HTTP %d): %s", req$status, url))
  }
  headers <- curl::parse_headers_list(req$headers)
  list(
    content_type = jsonlite::unbox(headers[["content-type"]]),
    link = jsonlite::unbox(headers[["link"]]),
    final_url = jsonlite::unbox(req$url),
    response_text = jsonlite::unbox(rawToChar(req$content))
  )
}