Lab 3: Data in Production

Setup

To setup your cloud-based development environment create an account using your email at http://rstd.io/class with code devops_workshop. Click on the Rstudio Connect widget to start your environment.

Part 1: Host API on Posit Connect

  1. Go to the Solutions Engineering R-examples repo and copy the HTTPS url.
  2. In Connect click Publish and Import from Git. Enter the URL that you copied above and click Next.
  3. Select the main branch and click next.
  4. Select the plumber-penguins/app directory and give it a name.
  5. Click `Deploy Content. In a few moments your API should be live!

Part 2: Explore your API

  1. Explore the endpoints for the API. Click the GET for each endpoint and then click Try it out and Execute.
  2. Click on the link ending in openapi.json below the title of you API.

  1. Try accessing the API via the terminal with curl <URL>. You should receive output that the app is not authorized!
  2. Authorize the app for everyone by changing the Access > Sharing option to Anyone - no login required and then try the curl command again. It should work now!

  1. Try out the /penguins endpoint and grab the Request URL.
  2. Access the /penguins endpoint and provide input for the sample size in your terminal and in the app itself.

curl "<REQUEST URL>/penguins?sample_size=5"

Part 3: Plumber Examples

  1. Install the plumber examples package. remotes::install_github("sol-eng/plumberExamples")
  2. Run available_apis to see plumber examples in the package.
  3. Access examples and code:
library(plumber)
plumb_api(package = "plumberExamples", name = "00-hello") %>% pr_run()

Part 4: Push-button deployment

  1. Publish the 11-car-inventory example to Connect.
  2. Click the blue publishing icon in the upper right of the file editor.

  1. When prompted connect to the Posit Connect server with the provided url. Click Publish.

Part 5: Programmatically access Connect

  1. We want to programmatically identify all the content that has been published. You will need to use the Connect API Key that you created in the earlier lab. Or you can create a new key.

  1. Open a new R script In your workbench console. Add your Connect information:
# Add server
rsconnect::addServer(
  url = "https://liberal-bullfinch.74633.fleeting.rstd.io/rsconnect/__api__",
  name = "colorado"
)

# Add account
rsconnect::connectApiUser(
  account = "",
  server = "colorado",
  apiKey = Sys.getenv("CONNECT_API_KEY"),
)
  1. Access your content programmatically in R: - need to add key ias env variable
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
df_full <- unnest_wider(tibble::tibble(dat = content(result)), dat) 
  1. Access your content programmatically in the terminal using curl:
export CONNECT_API_KEY=XXX
export CONNECT_SERVER=https://liberal-bullfinch.74633.fleeting.rstd.io/rsconnect/

curl --silent --show-error -L --max-redirs 0 --fail \
    -H "Authorization: Key ${CONNECT_API_KEY}" \
    "${CONNECT_SERVER}__api__/v1/content"