pb_download_url: Get the download url of a given file

View source: R/pb_download_url.R

pb_download_urlR Documentation

Get the download url of a given file

Description

Returns the URL download for a given file. This can be useful when using functions that are able to accept URLs.

Usage

pb_download_url(
  file = NULL,
  repo = guess_repo(),
  tag = "latest",
  url_type = c("browser", "api"),
  .token = gh::gh_token()
)

Arguments

file

character: vector of names of files to be downloaded. If NULL, all assets attached to the release will be downloaded.

repo

string: GH repository name in format "owner/repo". Default guess_repo() tries to guess based on current working directory's git repository

tag

string: tag for the GH release, defaults to "latest"

url_type

choice: one of "browser" or "api" - default "browser" is a web-facing URL that is not subject to API ratelimits but does not work for private repositories. "api" URLs work for private repos, but require a GitHub token passed in an Authorization header (see examples)

.token

GitHub authentication token, see gh::gh_token()

Value

the URL to download a file

Examples




# returns browser url by default (and all files if none are specified)
browser_url <- pb_download_url(
  repo = "tanho63/piggyback-tests",
  tag = "v0.0.2"
  )
print(browser_url)
utils::read.csv(browser_url[[1]])

# can return api url if desired
api_url <- pb_download_url(
  "mtcars.csv",
  repo = "tanho63/piggyback-tests",
  tag = "v0.0.2"
  )
print(api_url)

# for public repositories, this will still work
utils::read.csv(api_url)

# for private repos, can use httr or curl to fetch and then pass into read function
gh_pat <- Sys.getenv("GITHUB_PAT")

if(!identical(gh_pat, "")){
  resp <- httr::GET(api_url, httr::add_headers(Authorization = paste("Bearer", gh_pat)))
  utils::read.csv(text = httr::content(resp, as = "text"))
}

# or use pb_read which bundles some of this for you




cboettig/piggyback documentation built on Feb. 29, 2024, 2:35 a.m.