From 3f7b51216467c3f3e4a67e316759c26d66bdd76a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Sat, 5 Dec 2020 19:02:49 +0100 Subject: [PATCH 1/6] Harmonize --- R/github.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/github.R b/R/github.R index 880dbbc6..f5284c7b 100644 --- a/R/github.R +++ b/R/github.R @@ -36,12 +36,12 @@ github_commit <- function(username, repo, ref = "HEAD", "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) if (res$status_code == 304) { From f154f57d423593b8f2e078a96d8f30e951456c00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Sat, 5 Dec 2020 19:05:20 +0100 Subject: [PATCH 2/6] Move call to curl::new_handle() --- R/github.R | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/R/github.R b/R/github.R index f5284c7b..baadbb44 100644 --- a/R/github.R +++ b/R/github.R @@ -4,12 +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)) } ) + + h <- curl::new_handle() curl::handle_setheaders(h, .list = headers) res <- curl::curl_fetch_memory(url, handle = h) @@ -31,7 +32,6 @@ 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)) { @@ -42,6 +42,7 @@ github_commit <- function(username, repo, ref = "HEAD", } ) + h <- curl::new_handle() curl::handle_setheaders(h, .list = headers) res <- curl::curl_fetch_memory(url, handle = h) if (res$status_code == 304) { @@ -116,7 +117,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,6 +124,7 @@ github_DESCRIPTION <- function(username, repo, subdir = NULL, ref = "HEAD", host } ) + h <- curl::new_handle() curl::handle_setheaders(h, .list = headers) res <- curl::curl_fetch_memory(url, handle = h) if (res$status_code >= 300) { From d561ed9beed788c013d34f21c7fe01afc9818bb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Sat, 5 Dec 2020 19:10:27 +0100 Subject: [PATCH 3/6] Extract curl_fetch_memory() --- R/github.R | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/R/github.R b/R/github.R index baadbb44..d33c83a3 100644 --- a/R/github.R +++ b/R/github.R @@ -10,13 +10,7 @@ github_GET <- function(path, ..., host = "api.github.com", pat = github_pat(), u } ) - h <- curl::new_handle() - curl::handle_setheaders(h, .list = headers) - res <- curl::curl_fetch_memory(url, handle = h) - - if (res$status_code >= 300) { - stop(github_error(res)) - } + res <- curl_fetch_memory(url, headers) json$parse(raw_to_char_utf8(res$content)) } else { tmp <- tempfile() @@ -42,15 +36,11 @@ github_commit <- function(username, repo, ref = "HEAD", } ) - h <- curl::new_handle() - curl::handle_setheaders(h, .list = headers) - res <- curl::curl_fetch_memory(url, handle = h) + res <- 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 { @@ -124,12 +114,8 @@ github_DESCRIPTION <- function(username, repo, subdir = NULL, ref = "HEAD", host } ) - h <- curl::new_handle() - curl::handle_setheaders(h, .list = headers) - res <- curl::curl_fetch_memory(url, handle = h) - if (res$status_code >= 300) { - stop(github_error(res)) - } + res <- curl_fetch_memory(url, headers) + raw_to_char_utf8(res$content) } else { tmp <- tempfile() @@ -142,6 +128,20 @@ github_DESCRIPTION <- function(username, repo, subdir = NULL, ref = "HEAD", host } } +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) From b3d72db9f4a1dee9bd3e4951d79e3c98fd92efaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Sat, 5 Dec 2020 19:14:37 +0100 Subject: [PATCH 4/6] Oops --- R/github.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/github.R b/R/github.R index d33c83a3..a1a01b71 100644 --- a/R/github.R +++ b/R/github.R @@ -36,7 +36,7 @@ github_commit <- function(username, repo, ref = "HEAD", } ) - res <- curl_fetch_memory(url, headers, accept == 304) + res <- curl_fetch_memory(url, headers, accept = 304) if (res$status_code == 304) { return(current_sha) From e559eb3b73ab40021557efc3c12c943bf60398e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Sat, 5 Dec 2020 19:33:49 +0100 Subject: [PATCH 5/6] Rename --- R/github.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/R/github.R b/R/github.R index a1a01b71..78a8a04d 100644 --- a/R/github.R +++ b/R/github.R @@ -10,7 +10,7 @@ github_GET <- function(path, ..., host = "api.github.com", pat = github_pat(), u } ) - res <- curl_fetch_memory(url, headers) + res <- github_curl_fetch_memory(url, headers) json$parse(raw_to_char_utf8(res$content)) } else { tmp <- tempfile() @@ -36,7 +36,7 @@ github_commit <- function(username, repo, ref = "HEAD", } ) - res <- curl_fetch_memory(url, headers, accept = 304) + res <- github_curl_fetch_memory(url, headers, accept = 304) if (res$status_code == 304) { return(current_sha) @@ -114,7 +114,7 @@ github_DESCRIPTION <- function(username, repo, subdir = NULL, ref = "HEAD", host } ) - res <- curl_fetch_memory(url, headers) + res <- github_curl_fetch_memory(url, headers) raw_to_char_utf8(res$content) } else { @@ -128,7 +128,7 @@ github_DESCRIPTION <- function(username, repo, subdir = NULL, ref = "HEAD", host } } -curl_fetch_memory <- function(url, headers, accept = NULL) { +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) From 97bd24210aabfa87383ca7e5c1ba871b02d98063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Sun, 24 Jan 2021 07:27:53 +0100 Subject: [PATCH 6/6] Sync --- inst/install-github.R | 43 ++++++++++++++++++++++--------------------- install-github.R | 43 ++++++++++++++++++++++--------------------- 2 files changed, 44 insertions(+), 42 deletions(-) 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)