Skip to content

Commit d561ed9

Browse files
committed
Extract curl_fetch_memory()
1 parent f154f57 commit d561ed9

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

R/github.R

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,7 @@ github_GET <- function(path, ..., host = "api.github.com", pat = github_pat(), u
1010
}
1111
)
1212

13-
h <- curl::new_handle()
14-
curl::handle_setheaders(h, .list = headers)
15-
res <- curl::curl_fetch_memory(url, handle = h)
16-
17-
if (res$status_code >= 300) {
18-
stop(github_error(res))
19-
}
13+
res <- curl_fetch_memory(url, headers)
2014
json$parse(raw_to_char_utf8(res$content))
2115
} else {
2216
tmp <- tempfile()
@@ -42,15 +36,11 @@ github_commit <- function(username, repo, ref = "HEAD",
4236
}
4337
)
4438

45-
h <- curl::new_handle()
46-
curl::handle_setheaders(h, .list = headers)
47-
res <- curl::curl_fetch_memory(url, handle = h)
39+
res <- curl_fetch_memory(url, headers, accept == 304)
40+
4841
if (res$status_code == 304) {
4942
return(current_sha)
5043
}
51-
if (res$status_code >= 300) {
52-
stop(github_error(res))
53-
}
5444

5545
raw_to_char_utf8(res$content)
5646
} else {
@@ -124,12 +114,8 @@ github_DESCRIPTION <- function(username, repo, subdir = NULL, ref = "HEAD", host
124114
}
125115
)
126116

127-
h <- curl::new_handle()
128-
curl::handle_setheaders(h, .list = headers)
129-
res <- curl::curl_fetch_memory(url, handle = h)
130-
if (res$status_code >= 300) {
131-
stop(github_error(res))
132-
}
117+
res <- curl_fetch_memory(url, headers)
118+
133119
raw_to_char_utf8(res$content)
134120
} else {
135121
tmp <- tempfile()
@@ -142,6 +128,20 @@ github_DESCRIPTION <- function(username, repo, subdir = NULL, ref = "HEAD", host
142128
}
143129
}
144130

131+
curl_fetch_memory <- function(url, headers, accept = NULL) {
132+
h <- curl::new_handle()
133+
curl::handle_setheaders(h, .list = headers)
134+
res <- curl::curl_fetch_memory(url, handle = h)
135+
136+
if (!(res$status_code %in% accept)) {
137+
if (res$status_code >= 300) {
138+
stop(github_error(res))
139+
}
140+
}
141+
142+
res
143+
}
144+
145145
github_error <- function(res) {
146146
res_headers <- curl::parse_headers_list(res$headers)
147147

0 commit comments

Comments
 (0)