knitr::opts_chunk$set(echo = TRUE)
#Check that environment variables are set, exit knit if not.
library(connectview)
check_envvars()

Pin User Information to RStudio Connect

List local users with the GET /v1/users endpoint. This endpoint uses offset pagination (using page numbers). Results are sorted by first name, then last name, then username, then email. When called with a prefix parameter (not used in this example), it searches for local users matching the prefix.

library(httr)

# Request a page of up to 25 users.
resp <- GET(
  paste0(Sys.getenv("CONNECT_SERVER"), "__api__/v1/users?page_size=25"),
  add_headers(Authorization = paste("Key", Sys.getenv("CONNECT_API_KEY")))
)
payload <- content(resp)

payload_list <- list()
# While the current page has results, add its contents to the list holder
# then advance to the next page.
while(length(payload$result) > 0) {

  payload_list[[length(payload_list) + 1]] <- payload$results

  # get the next page
  nextPage <- payload$current_page + 1
  resp <- GET(
    paste0(Sys.getenv("CONNECT_SERVER"), "__api__/v1/users?page_size=25&page_number=", nextPage),
    add_headers(Authorization = paste("Key", Sys.getenv("CONNECT_API_KEY")))
  )
  payload <- content(resp)
}
library(tidyr)

# Unnest the list of lists into a single column tibble of lists
users_list <- unnest_longer(tibble::tibble(dat = payload_list), dat) 

# Rectangle the tibble of lists into a tibble of usable column data
df_users <- unnest_wider(users_list, dat)
library(pins)

# Pin the user data in df_users to RStudio Connect

# Pins will use CONNECT_API_KEY and CONNECT_SERVER by default,
# but we are being explicit here anyway.
pins::board_register_rsconnect(
  key = Sys.getenv("CONNECT_API_KEY"), 
  server = Sys.getenv("CONNECT_SERVER")
)

pins::pin(df_users, name = "user-info", description = "Results pulled from the /v1/users API", board = "rsconnect")

Pin Groups Information to RStudio Connect

List local groups with the GET /v1/groups endpoint. This endpoint uses offset pagination (using page numbers).

Note: This endpoint is available only when groups are enabled in RStudio Connect and it will return an error otherwise.

library(httr)

# Request a page of up to 25 groups.
resp <- GET(
  paste0(Sys.getenv("CONNECT_SERVER"), "__api__/v1/groups?page_size=25"),
  add_headers(Authorization = paste("Key", Sys.getenv("CONNECT_API_KEY")))
)
payload <- content(resp)

payload_list <- list()
# While the current page has results, add its contents to the list holder
# then advance to the next page.
while(length(payload$result) > 0) {

  payload_list[[length(payload_list) + 1]] <- payload$results

  # get the next page
  nextPage <- payload$current_page + 1
  resp <- GET(
    paste0(Sys.getenv("CONNECT_SERVER"), "__api__/v1/groups?page_size=25&page_number=", nextPage),
    add_headers(Authorization = paste("Key", Sys.getenv("CONNECT_API_KEY")))
  )
  payload <- content(resp)
}
library(tidyr)

# Unnest the list of lists into a single column tibble of lists
groups_list <- unnest_longer(tibble::tibble(dat = payload_list), dat) 

# Rectangle the tibble of lists into a tibble of usable column data
df_groups <- unnest_wider(groups_list, dat)
library(pins)

# Pin the user data in df_groups to RStudio Connect

pins::pin(df_groups, name = "group-info", description = "Results pulled from the /v1/groups API", board = "rsconnect")

Pin Content List to RStudio Connect

List all content items visible to the requesting user.

Authenticated access from a user is required. If an "administrator" role is used, then all content items will be returned regardless of the visibility to the requesting user.

library(httr)
library(tidyr)

# Use the /v1/content endpoint to retrieve the full list of content items
result <- GET(
  paste0(Sys.getenv("CONNECT_SERVER"),"__api__/v1/content"),
    add_headers(Authorization = paste("Key", Sys.getenv("CONNECT_API_KEY"))))

# Create a tibble for the content list result response
content_list <- unnest_wider(tibble::tibble(dat = content(result)), dat) 
library(pins)

# Pin the content data in content_list to RStudio Connect

# Pins will use CONNECT_API_KEY and CONNECT_SERVER by default,
# but we are being explicit here anyway.
pins::board_register_rsconnect(
  key = Sys.getenv("CONNECT_API_KEY"), 
  server = Sys.getenv("CONNECT_SERVER")
)

pins::pin(content_list, name = "content-info", description = "Results pulled from the /v1/content API", board = "rsconnect")


kmasiello/rscview documentation built on Jan. 3, 2023, 2:58 p.m.