Skip to content

Commit 228a251

Browse files
committed
fix ropensci#68 replace httr with crul - except for gist_auth that uses oauth
1 parent 806dcef commit 228a251

32 files changed

+117
-126
lines changed

DESCRIPTION

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
Package: gistr
22
Title: Work with 'GitHub' 'Gists'
33
Description: Work with 'GitHub' 'gists' from 'R' (e.g.,
4-
<http://en.wikipedia.org/wiki/GitHub#Gist>,
5-
<https://help.github.com/articles/about-gists/>). A 'gist'
4+
<https://en.wikipedia.org/wiki/GitHub#Gist>,
5+
<https://docs.github.com/en/github/writing-on-github/creating-gists/>). A 'gist'
66
is simply one or more files with code/text/images/etc. This package allows
77
the user to create new 'gists', update 'gists' with new files, rename files,
88
delete files, get and delete 'gists', star and 'un-star' 'gists', fork 'gists',
99
open a 'gist' in your default browser, get embed code for a 'gist', list
1010
'gist' 'commits', and get rate limit information when 'authenticated'. Some
1111
requests require authentication and some do not. 'Gists' website:
1212
<https://gist.github.com/>.
13-
Version: 0.5.0.92
13+
Version: 0.5.1.91
1414
Authors@R: c(
1515
person("Scott", "Chamberlain", role = c("aut", "cre"),
1616
email = "myrmecocystus@gmail.com",
@@ -27,8 +27,9 @@ VignetteBuilder: knitr
2727
Encoding: UTF-8
2828
Language: en-US
2929
Imports:
30-
jsonlite (>= 1.4),
31-
httr (>= 1.2.0),
30+
jsonlite,
31+
crul,
32+
httr,
3233
magrittr,
3334
assertthat,
3435
knitr,
@@ -37,7 +38,7 @@ Imports:
3738
Suggests:
3839
git2r,
3940
testthat
40-
RoxygenNote: 7.0.2
41+
RoxygenNote: 7.1.1
4142
X-schema.org-applicationCategory: Web
4243
X-schema.org-keywords: http, https, API, web-services, GitHub, GitHub API, gist, gists, code, script, snippet
4344
X-schema.org-isPartOf: https://ropensci.org

NAMESPACE

+6-8
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,16 @@ export(update)
4848
export(update_files)
4949
importFrom(assertthat,assert_that)
5050
importFrom(assertthat,has_extension)
51+
importFrom(crul,HttpClient)
5152
importFrom(dplyr,as_data_frame)
5253
importFrom(dplyr,bind_rows)
53-
importFrom(httr,DELETE)
54-
importFrom(httr,GET)
55-
importFrom(httr,PATCH)
56-
importFrom(httr,POST)
57-
importFrom(httr,PUT)
5854
importFrom(httr,add_headers)
59-
importFrom(httr,content)
60-
importFrom(httr,stop_for_status)
61-
importFrom(httr,warn_for_status)
55+
importFrom(httr,config)
56+
importFrom(httr,oauth2.0_token)
57+
importFrom(httr,oauth_app)
58+
importFrom(httr,oauth_endpoints)
6259
importFrom(jsonlite,flatten)
60+
importFrom(jsonlite,fromJSON)
6361
importFrom(knitr,knit)
6462
importFrom(magrittr,"%>%")
6563
importFrom(rmarkdown,render)

R/commits.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#' @param page (integer) Page number to return.
66
#' @param per_page (integer) Number of items to return per page.
77
#' Default 30. Max 100.
8-
#' @param ... Further named args to [httr::GET()]
8+
#' @param ... Further named args to [crul::verb-GET]
99
#' @examples \dontrun{
1010
#' gists()[[1]] %>% commits()
1111
#' gist(id = '1f399774e9ecc9153a6f') %>% commits(per_page = 5)

R/delete.R

+1-4
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ delete <- function(gist, ...) {
1111
gist <- as.gist(gist)
1212
res <- gist_DELETE(paste0(ghbase(), '/gists/', gist$id),
1313
auth = gist_auth(), headers = ghead(), ...)
14-
stop_for_status(res)
14+
res$raise_for_status()
1515
message('Your gist has been deleted')
1616
}
17-
18-
# res <- gist_DELETE(paste0(ghbase(), '/gists/', gist$id, "/2233500e6aac91a32cb84878edfb7a619fd8c56a"),
19-
# auth = gist_auth(), headers = ghead(), httr::verbose())

R/forks.R

+4-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#' @param page (integer) Page number to return.
66
#' @param per_page (integer) Number of items to return per page. Default 30.
77
#' Max 100.
8-
#' @param ... Further named args to [httr::GET()]
8+
#' @param ... Further named args to [crul::verb-GET]
99
#' @return A list of gist class objects
1010
#' @examples \dontrun{
1111
#' gist(id='1642874') %>% forks(per_page=2)
@@ -28,19 +28,14 @@ forks <- function(gist, page=NULL, per_page=30, ...) {
2828
#'
2929
#' @export
3030
#' @param gist A gist object or something coerceable to a gist
31-
#' @param ... Further named args to \code{\link[httr]{GET}}
31+
#' @param ... Further named args to [crul::verb-GET]
3232
#' @return A gist class object
3333
#' @examples \dontrun{
3434
#' # fork a gist
35-
#' gists()[[1]] %>% fork()
35+
#' w <- gists()[[1]] %>% fork()
3636
#'
3737
#' # browse to newly forked gist
38-
#' gist(id='0831f3fbd83ac4d46451') %>% fork() %>% browse()
39-
#'
40-
#' # extract the last one
41-
#' gist(id='1642874') %>%
42-
#' forks() %>%
43-
#' .[length(.)]
38+
#' browse(w)
4439
#' }
4540

4641
fork <- function(gist, ...) {

R/gist.R

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
#' (bigfile <- gist(id = "b74b878fd7d9176a4c52"))
4848
#' ## then get the raw_url, and retrieve the file
4949
#' url <- bigfile$files$`plossmall.json`$raw_url
50-
#' # httr::GET(url)
5150
#' }
5251

5352
gist <- function(id, revision = NULL, ...){

R/gist_auth.R

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
gist_auth <- function(app = gistr_app, reauth = FALSE) {
2727

2828
if (exists("auth_config", envir = cache) && !reauth) {
29-
return(cache$auth_config)
29+
return(undo(cache$auth_config$headers))
3030
}
3131
pat <- Sys.getenv("GITHUB_PAT", "")
3232
if (!identical(pat, "")) {
@@ -41,9 +41,11 @@ gist_auth <- function(app = gistr_app, reauth = FALSE) {
4141
auth_config <- httr::config(token = token)
4242
}
4343
cache$auth_config <- auth_config
44-
auth_config
44+
undo(auth_config$headers)
4545
}
4646

47+
undo <- function(x) unclass(as.list(unclass(x)))
48+
4749
cache <- new.env(parent = emptyenv())
4850

4951
gistr_app <- httr::oauth_app(

R/gist_create.R

+2-4
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@
4343
#' ```
4444
#' '}, knit=TRUE)
4545
#'
46-
#' library('httr')
47-
#' url <- "https://github.com/ropensci/geojsonio/blob/master/inst/examples/zillow_or.geojson"
48-
#' res <- httr::GET(url)
49-
#' json <- httr::content(res, as = "text")
46+
#' url <- "https://raw.githubusercontent.com/ropensci/geojsonio/master/inst/examples/zillow_or.geojson"
47+
#' json <- crul::HttpClient$new(url)$get()$parse("UTF-8")
5048
#' gist_create(code = json, filename = "zillow_or.geojson")
5149
#'
5250
#' # Knit and include source file, so both files are in the gist

R/gist_create_git.R

+14-14
Original file line numberDiff line numberDiff line change
@@ -248,21 +248,21 @@ unpack <- function(z) {
248248
}
249249

250250
cgist <- function(description, public) {
251-
res <- httr::POST(paste0(ghbase(), '/gists'),
252-
gist_auth(),
253-
encode = "json",
254-
body = jsonlite::toJSON(list(
255-
description = description,
256-
public = public,
257-
files = list(
258-
".gistr" = list(
259-
content = "gistr"
260-
)
261-
)
262-
), auto_unbox = TRUE)
251+
gist_POST(
252+
paste0(ghbase(), '/gists'),
253+
gist_auth(),
254+
ghead(),
255+
body = jsonlite::toJSON(list(
256+
description = description,
257+
public = public,
258+
files = list(
259+
".gistr" = list(
260+
content = "gistr"
261+
)
262+
)
263+
), auto_unbox = TRUE),
264+
encode = "json"
263265
)
264-
stopstatus(res)
265-
jsonlite::fromJSON(httr::content(res, "text", encoding = "UTF-8"), FALSE)
266266
}
267267

268268
all_artifacts <- function(x) {

R/gist_create_obj.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#' @param pretty (logical) For data.frame and matrix objects, create
1111
#' a markdown table. If FALSE, pushes up json. (default: `TRUE`)
1212
#' @param filename Name of the file to create. Default: `file.txt`
13-
#' @param ... Further args passed on to [httr::POST()]
13+
#' @param ... Further args passed on to [crul::verb-POST]
1414
#' @details This function is specifically for going from R objects to a gist,
1515
#' whereas [gist_create()] is for going from files or executing code
1616
#' @seealso [gist_create()], [gist_create_git()]

R/gistr-package.R

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#' There are two ways to authorise gistr to work with your GitHub account:
77
#'
88
#' - Generate a personal access token (PAT) at
9-
#' <https://help.github.com/articles/creating-an-access-token-for-command-line-use>
9+
#' <https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token>
1010
#' and record it in the `GITHUB_PAT` envar.
1111
#' - Interactively login into your GitHub account and authorise with OAuth.
1212
#'
@@ -15,11 +15,11 @@
1515
#' @importFrom magrittr %>%
1616
#' @importFrom knitr knit
1717
#' @importFrom rmarkdown render
18-
#' @importFrom httr GET POST PATCH PUT DELETE content stop_for_status
19-
#' add_headers warn_for_status
18+
#' @importFrom crul HttpClient
19+
#' @importFrom httr add_headers oauth_endpoints oauth2.0_token config oauth_app
2020
#' @importFrom assertthat assert_that has_extension
2121
#' @importFrom dplyr bind_rows as_data_frame
22-
#' @importFrom jsonlite flatten
22+
#' @importFrom jsonlite fromJSON flatten
2323
#' @name gistr-package
2424
#' @aliases gistr
2525
#' @docType package

R/gists.R

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@
3333
#' # Your starred gists
3434
#' gists('starred')
3535
#' # pass in curl options
36-
#' gists(per_page=1, config=verbose())
37-
#' gists(per_page=1, config=timeout(seconds = 0.5))
36+
#' gists(per_page=1, verbose=TRUE)
3837
#' }
3938

4039
gists <- function(what='public', since=NULL, page=NULL, per_page=30, ...) {

R/rate_limit.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#' Get rate limit information
22
#'
33
#' @export
4-
#' @param ... Named args to [httr::GET()]
4+
#' @param ... Named args to [crul::verb-GET]
55
#' @examples \dontrun{
66
#' rate_limit()
77
#' }

R/run.R

+5-5
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,17 @@ run <- function(x, filename="code.R", knitopts=list()){
5959
if (inherits(x, 'gist')) x %>% update_files(outpath) else outpath
6060
}
6161

62-
check_files <- function(x){
62+
check_files <- function(x) {
6363
if (length(x$files) > 1) {
6464
stop("You can only feed one file to run()")
6565
} else {
6666
x$files[[1]]
6767
}
6868
}
6969

70-
get_raw <- function(path, ...){
71-
res <- httr::GET(path, gist_auth(), ghead(), ...)
70+
get_raw <- function(path, ...) {
71+
res <- cVERB("get", path, gist_auth(), ghead(), ...)
72+
res$raise_for_status()
7273
stopifnot(res$headers$`content-type` == 'text/plain; charset=utf-8')
73-
warn_for_status(res)
74-
httr::content(res, as = "text", encoding = "UTF-8")
74+
res$parse("UTF-8")
7575
}

R/star.R

+12-5
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323

2424
star <- function(gist, ...){
2525
gist <- as.gist(gist)
26-
res <- gist_PUT(url_star(gist$id), gist_auth(), ghead(),
27-
add_headers(`Content-Length` = 0), ...)
26+
res <- gist_PUT(url_star(gist$id), gist_auth(),
27+
c(ghead(), list(`Content-Length` = "0")), ...)
2828
star_mssg(res, 'Success, gist starred!')
2929
gist
3030
}
@@ -42,16 +42,17 @@ unstar <- function(gist, ...){
4242
#' @rdname star
4343
star_check <- function(gist, ...){
4444
gist <- as.gist(gist)
45-
res <- GET(url_star(gist$id), gist_auth(), ghead(), ...)
45+
res <- cVERB("get", url_star(gist$id), gist_auth(), ghead(), ...)
4646
msg <- if (res$status_code == 204) TRUE else FALSE
4747
message(msg)
4848
gist
4949
}
5050

5151
url_star <- function(x) sprintf('%s/gists/%s/star', ghbase(), x)
5252

53-
star_mssg <- function(x, y) if (x$status_code == 204) message(y) else
54-
warn_for_status(x)
53+
star_mssg <- function(x, y) {
54+
if (x$status_code == 204) message(y) else warn_status(x)
55+
}
5556

5657
star_action <- function(x, y) {
5758
if (x$status_code == 204) {
@@ -60,3 +61,9 @@ star_action <- function(x, y) {
6061
x$status_code
6162
}
6263
}
64+
65+
warn_status <- function(x) {
66+
if (x$status_code < 300) return(invisible(x))
67+
tmp <- x$status_http()
68+
warning(sprintf("(HTTP %s) %s", tmp$status_code, tmp$message), call.=FALSE)
69+
}

0 commit comments

Comments
 (0)