This is a short set of functions that use the httr
package to upload a directory of files as a gist. The post_gist
function uploads an anonymous gist, which can only be deleted within a short time of being uploaded. So be cautious in what you upload using this function.
Last active
September 9, 2022 02:41
-
-
Save ramnathv/4758157 to your computer and use it in GitHub Desktop.
Upload Directory of Files using Gist API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#" Function that takes a list of files and creates payload for API | |
#' | |
#' @param filenames names of files to post | |
#' @param description brief description of gist (optional) | |
#' @param public whether gist is public (defaults to TRUE) | |
create_gist <- function(filenames, description = "", public = TRUE){ | |
files = lapply(filenames, function(file){ | |
x = list(content = paste(readLines(file, warn = F), collapse = '\n')) | |
}) | |
names(files) = filenames | |
body = list(description = description, public = public, files = files) | |
RJSONIO::toJSON(body) | |
} | |
#' Function that posts a directory of files as a gist | |
#' | |
#' @param gdir directory of files to post | |
#' @param description brief description of gist | |
#' @param public whether gist is public (defaults to TRUE) | |
post_gist <- function(gdir, description = "", public = TRUE){ | |
g_url = "https://api.github.com/gists" | |
gist = create_gist(dir(gdir), description, public) | |
posted = httr::POST(g_url, body = gist) | |
browseURL(content(posted)$html_url) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
helpful little script. thanks.