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
- Obtain the Posit Connect server URL and API Key from environment variables.
- Retrieve the first page of the audit logs via the
GET /v1/audit_logs
endpoint. - Grab the response in JSON format.
- Add the results array from the JSON to a data frame.
- The
paging.next
response field indicates the URL for the next page. Load, parse, and append each additional page untilpaging.next
isNULL
.
import os
import requests
import pandas as pd
from IPython.display import display
= os.getenv("CONNECT_SERVER")
connect_server = os.getenv("CONNECT_API_KEY")
connect_api_key = 25
LIMIT
= requests.Session()
session "Authorization": f"Key {connect_api_key}"})
session.headers.update({
# Request a page of up to 25 audit log records.
= session.get(
response f"{connect_server}/__api__/v1/audit_logs",
= {"limit":LIMIT,"ascOrder":"false"}
params
)= response.json()
json_data = json_data['paging']['next']
next_page = pd.DataFrame(json_data['results'])
df
# Continue to page through additional records
# while we have a "next" reference
while next_page:
= session.get(nextPage)
response = response.json()
json_data = json_data['paging']['next']
next_page = pd.DataFrame(json_data['results'])
results = pd.concat([df, results])
df
# Display all results
'display.max_rows', None)
pd.set_option( display(df)
- Obtain the Posit Connect server URL and API Key from environment variables.
- Retrieve the first page of the audit logs via the
GET /v1/audit_logs
endpoint. - Parse the response using
httr::content
. - Print the current page.
- The
paging.next
response field indicates the URL for the next page. Load, parse, and print each additional page untilpaging.next
isNULL
.
library(httr)
<- Sys.getenv("CONNECT_SERVER")
connectServer <- Sys.getenv("CONNECT_API_KEY")
connectAPIKey
# Request a page of up to 25 audit log records.
<- GET(
resp paste0(connectServer, "/__api__/v1/audit_logs?ascOrder=false&limit=25"),
add_headers(Authorization = paste("Key", connectAPIKey))
)<- content(resp)
payload # 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"]])) {
<- GET(
resp $paging[["next"]],
payloadadd_headers(Authorization = paste("Key", connectAPIKey))
)<- content(resp)
payload # 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
- Obtain the Connect server URL and API Key from environment variables.
- To retrieve the first page of the user’s list, call the
GET /v1/users
endpoint. - Grab the response in JSON format.
- Add the results array from the JSON to a data frame.
- 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
= os.getenv("CONNECT_SERVER")
connect_server = os.getenv("CONNECT_API_KEY")
connect_api_key = 25
PAGE_SIZE
= requests.Session()
session "Authorization": f"Key {connect_api_key}"})
session.headers.update({
# Request a page of up to 25 users.
= session.get(
response f"{connect_server}/__api__/v1/users",
= {"page_size":PAGE_SIZE}
params
)= response.json()
json_data = pd.DataFrame(json_data['results'])
df
# While there are results in the response, continue
# paging through and adding them to the data frame.
while len(json_data['results']) > 0 :
= json_data['current_page'] + 1
next_page = session.get(
response f"{connect_server}/__api__/v1/users",
= {"page_size":PAGE_SIZE,"page_number":next_page}
params
)= response.json()
json_data = pd.DataFrame(json_data['results'])
results = pd.concat([df, results])
df
# Display all results
'display.max_rows', None)
pd.set_option( display(df)
- Obtain the Posit Connect server URL and API Key from environment variables.
- To retrieve the first page of the user’s list, call the
GET /v1/users
endpoint. - Parse the response using
httr::content
. - Print the current page.
- Increment the requested
page_number
to load, parse, and print each additional page until no results are returned.
library(httr)
<- Sys.getenv("CONNECT_SERVER")
connectServer <- Sys.getenv("CONNECT_API_KEY")
connectAPIKey
# Request a page of up to 25 users.
<- GET(
resp paste0(connectServer, "/__api__/v1/users?page_size=25"),
add_headers(Authorization = paste("Key", connectAPIKey))
)<- content(resp)
payload
# 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
<- payload$current_page + 1
nextPage <- GET(
resp paste0(connectServer, "/__api__/v1/users?page_size=25&page_number=", nextPage),
add_headers(Authorization = paste("Key", connectAPIKey))
)<- content(resp)
payload }