Pagination

Keyset (cursor) pagination

The following snippet pages through the audit logs, which uses keyset pagination, starting from the most recent entries, 25 entries at a time. If fewer than 25 audit log entries are present, then no paging occurs. Try adjusting the limit to produce pagination results.

Workflow

  1. Obtain the Posit Connect server URL and API Key from environment variables.
  2. Retrieve the first page of the audit logs via the GET /v1/audit_logs endpoint.
  3. Grab the response in JSON format.
  4. Add the results array from the JSON to a data frame.
  5. The paging.next response field indicates the URL for the next page. Load, parse, and append each additional page until paging.next is NULL.
import os
import requests
import pandas as pd
from IPython.display import display

connect_server = os.getenv("CONNECT_SERVER")
connect_api_key = os.getenv("CONNECT_API_KEY")
LIMIT = 25

session = requests.Session()
session.headers.update({"Authorization": f"Key {connect_api_key}"})

# Request a page of up to 25 audit log records.
response = session.get(
    f"{connect_server}/__api__/v1/audit_logs",
    params = {"limit":LIMIT,"ascOrder":"false"}
)
json_data = response.json()
next_page = json_data['paging']['next']
df = pd.DataFrame(json_data['results'])

# Continue to page through additional records
# while we have a "next" reference
while next_page:
  response = session.get(nextPage)
  json_data = response.json()
  next_page = json_data['paging']['next']
  results = pd.DataFrame(json_data['results'])
  df = pd.concat([df, results])

# Display all results
pd.set_option('display.max_rows', None)
display(df)
  1. Obtain the Posit Connect server URL and API Key from environment variables.
  2. Retrieve the first page of the audit logs via the GET /v1/audit_logs endpoint.
  3. Parse the response using httr::content.
  4. Print the current page.
  5. The paging.next response field indicates the URL for the next page. Load, parse, and print each additional page until paging.next is NULL.
library(httr)

connectServer <- Sys.getenv("CONNECT_SERVER")
connectAPIKey <- Sys.getenv("CONNECT_API_KEY")

# Request a page of up to 25 audit log records.
resp <- GET(
  paste0(connectServer, "/__api__/v1/audit_logs?ascOrder=false&limit=25"),
  add_headers(Authorization = paste("Key", connectAPIKey))
)
payload <- content(resp)
# print the current page results
print(payload$results)

# Continue to page through additional records
# while we have a "next" reference
while(!is.null(payload$paging[["next"]])) {
  resp <- GET(
    payload$paging[["next"]],
    add_headers(Authorization = paste("Key", connectAPIKey))
  )
  payload <- content(resp)
  # print the results on this page
  print(payload$results)
}

Offset pagination

The following snippet pages through the user’s list, which uses offset pagination, 25 entries at a time. If fewer than 25 users exist on the Connect server, then no paging occurs. Try adjusting the page
size to produce pagination results.

Workflow

  1. Obtain the Connect server URL and API Key from environment variables.
  2. To retrieve the first page of the user’s list, call the GET /v1/users endpoint.
  3. Grab the response in JSON format.
  4. Add the results array from the JSON to a data frame.
  5. Increment the requested page_number to load, parse, and append each additional page until no results are returned.
import os
import requests
import pandas as pd
from IPython.display import display

connect_server = os.getenv("CONNECT_SERVER")
connect_api_key = os.getenv("CONNECT_API_KEY")
PAGE_SIZE = 25

session = requests.Session()
session.headers.update({"Authorization": f"Key {connect_api_key}"})

# Request a page of up to 25 users.
response = session.get(
    f"{connect_server}/__api__/v1/users",
    params = {"page_size":PAGE_SIZE}
)
json_data = response.json()
df = pd.DataFrame(json_data['results'])

# While there are results in the response, continue
# paging through and adding them to the data frame.
while len(json_data['results']) > 0 :
  next_page = json_data['current_page'] + 1
  response = session.get(
     f"{connect_server}/__api__/v1/users",
     params = {"page_size":PAGE_SIZE,"page_number":next_page}
  )
  json_data = response.json()
  results = pd.DataFrame(json_data['results'])
  df = pd.concat([df, results])

# Display all results
pd.set_option('display.max_rows', None)
display(df)
  1. Obtain the Posit Connect server URL and API Key from environment variables.
  2. To retrieve the first page of the user’s list, call the GET /v1/users endpoint.
  3. Parse the response using httr::content.
  4. Print the current page.
  5. Increment the requested page_number to load, parse, and print each additional page until no results are returned.
library(httr)

connectServer <- Sys.getenv("CONNECT_SERVER")
connectAPIKey <- Sys.getenv("CONNECT_API_KEY")

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

# While the current page has results, print its contents
# then advance to the next page.
while(length(payload$result) > 0) {
  # print the current page results
  print(payload$results)

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