gh
GitHub API
Minimalistic client to access GitHub's API v3.
Installation
devtools::install_github("gaborcsardi/gh")
Usage
library(gh)
Use the gh()
function to access all API endpoints. The endpoints are
listed in the documentation.
The first argument of gh()
is the endpoint. Note that the leading slash
must be included as well. Parameters can be passed as extra arguments. E.g.
my_repos <- gh("/user/repos", type = "public")
vapply(my_repos, "[[", "", "name")
#> [1] "ask" "background"
#> [3] "baseimports" "bundler"
#> [5] "clisymbols" "closure"
#> [7] "cmaker" "cranky"
#> [9] "crayon" "datastore"
#> [11] "decima" "disposables"
#> [13] "docstrings" "dot-emacs"
#> [15] "dotenv" "elasticsearch-jetty"
#> [17] "ensurethat" "falsy"
#> [19] "feast" "flock"
#> [21] "gaborcsardi.github.io" "gh"
#> [23] "ISA" "json2r6"
#> [25] "keypress" "locker"
#> [27] "macBriain" "mason"
#> [29] "mason.rpkg" "massig"
The JSON result sent by the API is converted to an R object.
If the end point itself has parameters, these can also be passed as extra arguments:
j_repos <- gh("/users/:username/repos", username = "jeroenooms")
vapply(j_repos, "[[", "", "name")
#> [1] "apps" "blog"
#> [3] "cheerio" "cmark"
#> [5] "commonmark" "curl"
#> [7] "daff" "data"
#> [9] "devtools" "DiagrammeR"
#> [11] "docdbi" "docplyr"
#> [13] "dplyr" "encode"
#> [15] "evaluate" "fib"
#> [17] "git" "httr"
#> [19] "icu" "interactivity"
#> [21] "ipyr" "IRkernel"
#> [23] "jeroenooms.github.com" "JJcorr"
#> [25] "js" "JSlibs"
#> [27] "jsonlite" "lausd-data"
#> [29] "lawn" "leaflet-pip"
POST, PATCH, PUT and DELETE requests
POST, PATCH, PUT, and DELETE requests can be sent by including the HTTP verb before the endpoint, in the first argument. E.g. to create a repository:
new_repo <- gh("POST /user/repos", name = "my-new-repo-for-gh-testing")
and then delete it:
gh("DELETE /repos/:owner/:repo", owner = "gaborcsardi",
repo = "my-new-repo-for-gh-testing")
Tokens
By default the GITHUB_TOKEN
environment variable is used. Alternatively,
one can set the .token
argument of gh()
.
Pagination
Supply the page
parameter to get subsequent pages:
my_repos2 <- gh("GET /users/:username/repos", username = "gaborcsardi",
type = "public", page = 2)
vapply(my_repos2, "[[", "", "name")
#> [1] "MISO" "my-old-MISO"
#> [3] "parsedate" "pingr"
#> [5] "pkgconfig" "playground"
#> [7] "pretty" "prettyunits"
#> [9] "printr" "procrustes"
#> [11] "progress" "r-font"
#> [13] "r-wiki-engine" "ratlab"
#> [15] "rcorpora" "Rcpp"
#> [17] "redsvd" "regexp"
#> [19] "resume" "rfunctions"
#> [21] "roxygen" "rsmith"
#> [23] "rsync-mirror" "scidb"
#> [25] "Semantic-plotting-in-R" "snap"
#> [27] "spark" "splicing"
#> [29] "staticdocs" "tab"
License
MIT © Gabor Csardi.