diff --git a/R/github.R b/R/github.R index 880dbbc6..78a8a04d 100644 --- a/R/github.R +++ b/R/github.R @@ -4,18 +4,13 @@ github_GET <- function(path, ..., host = "api.github.com", pat = github_pat(), u url <- build_url(host, path) if (isTRUE(use_curl)) { - h <- curl::new_handle() headers <- c( if (!is.null(pat)) { c("Authorization" = paste0("token ", pat)) } ) - curl::handle_setheaders(h, .list = headers) - res <- curl::curl_fetch_memory(url, handle = h) - if (res$status_code >= 300) { - stop(github_error(res)) - } + res <- github_curl_fetch_memory(url, headers) json$parse(raw_to_char_utf8(res$content)) } else { tmp <- tempfile() @@ -31,25 +26,21 @@ github_commit <- function(username, repo, ref = "HEAD", url <- build_url(host, "repos", username, repo, "commits", utils::URLencode(ref, reserved = TRUE)) if (isTRUE(use_curl)) { - h <- curl::new_handle() headers <- c( "Accept" = "application/vnd.github.v3.sha", if (!is.null(pat)) { c("Authorization" = paste0("token ", pat)) + }, + if (!is.null(current_sha)) { + c("If-None-Match" = paste0('"', current_sha, '"')) } ) - if (!is.null(current_sha)) { - headers <- c(headers, "If-None-Match" = paste0('"', current_sha, '"')) - } - curl::handle_setheaders(h, .list = headers) - res <- curl::curl_fetch_memory(url, handle = h) + res <- github_curl_fetch_memory(url, headers, accept = 304) + if (res$status_code == 304) { return(current_sha) } - if (res$status_code >= 300) { - stop(github_error(res)) - } raw_to_char_utf8(res$content) } else { @@ -116,7 +107,6 @@ github_DESCRIPTION <- function(username, repo, subdir = NULL, ref = "HEAD", host url <- paste0(url, "?ref=", utils::URLencode(ref)) if (isTRUE(use_curl)) { - h <- curl::new_handle() headers <- c( "Accept" = "application/vnd.github.v3.raw", if (!is.null(pat)) { @@ -124,11 +114,8 @@ github_DESCRIPTION <- function(username, repo, subdir = NULL, ref = "HEAD", host } ) - curl::handle_setheaders(h, .list = headers) - res <- curl::curl_fetch_memory(url, handle = h) - if (res$status_code >= 300) { - stop(github_error(res)) - } + res <- github_curl_fetch_memory(url, headers) + raw_to_char_utf8(res$content) } else { tmp <- tempfile() @@ -141,6 +128,20 @@ github_DESCRIPTION <- function(username, repo, subdir = NULL, ref = "HEAD", host } } +github_curl_fetch_memory <- function(url, headers, accept = NULL) { + h <- curl::new_handle() + curl::handle_setheaders(h, .list = headers) + res <- curl::curl_fetch_memory(url, handle = h) + + if (!(res$status_code %in% accept)) { + if (res$status_code >= 300) { + stop(github_error(res)) + } + } + + res +} + github_error <- function(res) { res_headers <- curl::parse_headers_list(res$headers) diff --git a/inst/install-github.R b/inst/install-github.R index ee08f07a..e0d92297 100644 --- a/inst/install-github.R +++ b/inst/install-github.R @@ -1687,18 +1687,13 @@ function(...) { url <- build_url(host, path) if (isTRUE(use_curl)) { - h <- curl::new_handle() headers <- c( if (!is.null(pat)) { c("Authorization" = paste0("token ", pat)) } ) - curl::handle_setheaders(h, .list = headers) - res <- curl::curl_fetch_memory(url, handle = h) - if (res$status_code >= 300) { - stop(github_error(res)) - } + res <- github_curl_fetch_memory(url, headers) json$parse(raw_to_char_utf8(res$content)) } else { tmp <- tempfile() @@ -1714,25 +1709,21 @@ function(...) { url <- build_url(host, "repos", username, repo, "commits", utils::URLencode(ref, reserved = TRUE)) if (isTRUE(use_curl)) { - h <- curl::new_handle() headers <- c( "Accept" = "application/vnd.github.v3.sha", if (!is.null(pat)) { c("Authorization" = paste0("token ", pat)) + }, + if (!is.null(current_sha)) { + c("If-None-Match" = paste0('"', current_sha, '"')) } ) - if (!is.null(current_sha)) { - headers <- c(headers, "If-None-Match" = paste0('"', current_sha, '"')) - } - curl::handle_setheaders(h, .list = headers) - res <- curl::curl_fetch_memory(url, handle = h) + res <- github_curl_fetch_memory(url, headers, accept = 304) + if (res$status_code == 304) { return(current_sha) } - if (res$status_code >= 300) { - stop(github_error(res)) - } raw_to_char_utf8(res$content) } else { @@ -1799,7 +1790,6 @@ function(...) { url <- paste0(url, "?ref=", utils::URLencode(ref)) if (isTRUE(use_curl)) { - h <- curl::new_handle() headers <- c( "Accept" = "application/vnd.github.v3.raw", if (!is.null(pat)) { @@ -1807,11 +1797,8 @@ function(...) { } ) - curl::handle_setheaders(h, .list = headers) - res <- curl::curl_fetch_memory(url, handle = h) - if (res$status_code >= 300) { - stop(github_error(res)) - } + res <- github_curl_fetch_memory(url, headers) + raw_to_char_utf8(res$content) } else { tmp <- tempfile() @@ -1824,6 +1811,20 @@ function(...) { } } + github_curl_fetch_memory <- function(url, headers, accept = NULL) { + h <- curl::new_handle() + curl::handle_setheaders(h, .list = headers) + res <- curl::curl_fetch_memory(url, handle = h) + + if (!(res$status_code %in% accept)) { + if (res$status_code >= 300) { + stop(github_error(res)) + } + } + + res + } + github_error <- function(res) { res_headers <- curl::parse_headers_list(res$headers) diff --git a/install-github.R b/install-github.R index ee08f07a..e0d92297 100644 --- a/install-github.R +++ b/install-github.R @@ -1687,18 +1687,13 @@ function(...) { url <- build_url(host, path) if (isTRUE(use_curl)) { - h <- curl::new_handle() headers <- c( if (!is.null(pat)) { c("Authorization" = paste0("token ", pat)) } ) - curl::handle_setheaders(h, .list = headers) - res <- curl::curl_fetch_memory(url, handle = h) - if (res$status_code >= 300) { - stop(github_error(res)) - } + res <- github_curl_fetch_memory(url, headers) json$parse(raw_to_char_utf8(res$content)) } else { tmp <- tempfile() @@ -1714,25 +1709,21 @@ function(...) { url <- build_url(host, "repos", username, repo, "commits", utils::URLencode(ref, reserved = TRUE)) if (isTRUE(use_curl)) { - h <- curl::new_handle() headers <- c( "Accept" = "application/vnd.github.v3.sha", if (!is.null(pat)) { c("Authorization" = paste0("token ", pat)) + }, + if (!is.null(current_sha)) { + c("If-None-Match" = paste0('"', current_sha, '"')) } ) - if (!is.null(current_sha)) { - headers <- c(headers, "If-None-Match" = paste0('"', current_sha, '"')) - } - curl::handle_setheaders(h, .list = headers) - res <- curl::curl_fetch_memory(url, handle = h) + res <- github_curl_fetch_memory(url, headers, accept = 304) + if (res$status_code == 304) { return(current_sha) } - if (res$status_code >= 300) { - stop(github_error(res)) - } raw_to_char_utf8(res$content) } else { @@ -1799,7 +1790,6 @@ function(...) { url <- paste0(url, "?ref=", utils::URLencode(ref)) if (isTRUE(use_curl)) { - h <- curl::new_handle() headers <- c( "Accept" = "application/vnd.github.v3.raw", if (!is.null(pat)) { @@ -1807,11 +1797,8 @@ function(...) { } ) - curl::handle_setheaders(h, .list = headers) - res <- curl::curl_fetch_memory(url, handle = h) - if (res$status_code >= 300) { - stop(github_error(res)) - } + res <- github_curl_fetch_memory(url, headers) + raw_to_char_utf8(res$content) } else { tmp <- tempfile() @@ -1824,6 +1811,20 @@ function(...) { } } + github_curl_fetch_memory <- function(url, headers, accept = NULL) { + h <- curl::new_handle() + curl::handle_setheaders(h, .list = headers) + res <- curl::curl_fetch_memory(url, handle = h) + + if (!(res$status_code %in% accept)) { + if (res$status_code >= 300) { + stop(github_error(res)) + } + } + + res + } + github_error <- function(res) { res_headers <- curl::parse_headers_list(res$headers)