rstudio / pins-r

Pin, discover, and share resources
https://pins.rstudio.com
Other
316 stars 62 forks source link

New `headers` argument to `board_url()` #732

Closed juliasilge closed 1 year ago

juliasilge commented 1 year ago

This PR adds a new add_headers() argument to board_url(), mostly for authentication.

Addresses #600 for private GH repos:

library(pins)
folder_on_gh <- "https://raw.githubusercontent.com/juliasilge/pkgpins/main/pkgdown/assets/pins-board/_pins.yaml"
my_headers <- httr::add_headers(Authorization = paste("token", "github_pat_XXXX"))
b <- board_url(folder_on_gh, add_headers = my_headers)
b %>% pin_list()
#> [1] "mtcars_metric"

Created on 2023-04-10 with reprex v2.0.2

Addresses #616 for Posit Connect vanity URLs that are not set to "Anyone - no login required":

library(pins)
board <- board_url(
  c(my_vanity_url_pin = "https://colorado.posit.co/rsc/some-nice-numbers/"),
  add_headers = httr::add_headers(Authorization = paste("Key", Sys.getenv("CONNECT_API_KEY")))
)
board %>% pin_read("my_vanity_url_pin")
#>  [1]  1  2  3  4  5  6  7  8  9 10

Created on 2023-04-10 with reprex v2.0.2

juliasilge commented 1 year ago

Upon further consideration, the solution in #706 doesn't really get us enough of what folks are wanting, and is quite a big change. This more simple change results in more consistent versioning options (does require a manifest for reading different versions; otherwise reads latest) and supports more use cases.

Ideally in the long term, we will have better options on Connect like service accounts that help with some of the same user needs. I think this PR is a good option to implement now, because it fits in with several use cases and is not a burdensome change.

juliasilge commented 1 year ago

Partly to get some more thorough testing of whether this all works (but also because folks continue to ask), I ended up adding back in a board_connect_url() function which is a very thin wrapper around board_url() with the new headers arg. It only authenticates to Connect via environment variable, which SE folks say is good because it is the only method that will work went content gets deployed to Connect.

We talked about whether it is possible to catch these kind of errors and provide a more helpful error message:

library(pins)

b2 <- board_url(
  c(non_public_numbers = "https://colorado.posit.co/rsc/some-nice-numbers/"),
)
b2 %>% pin_read("non_public_numbers")
#> Error in http_download(url = paste0(url, "data.txt"), path_dir = cache_dir, : Not Found (HTTP 404).

Created on 2023-04-12 with reprex v2.0.2

I don't know that we can for now, because Connect is returning a 404 where the real problem is lack of authentication (403).

github-actions[bot] commented 1 year ago

This pull request has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.