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.
- Obtain the server URL and API Key from environment variables.
- Call the "Get audit logs" endpoint to get the first page. See the API Documentation for more information.
- Parse the response using
httr::content
. - Print the current page.
- Repeat steps 1 through 3 until there are no more results. Note also
that the
paging.next
property in the response is the URL of the next page.
library(httr) # The connectServer URL must have a trailing slash. 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.
- Obtain the server URL and API Key from environment variables.
- Call the "Get all users" endpoint to get the first page. See the API Documentation for more information.
- Parse the response using
httr::content
. - Print the current page.
- Repeat steps 1 through 3 until there is no more "next" page. Note
also that the query parameter
page_number
determines the page to return.
library(httr) # The connectServer URL must have a trailing slash. 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) }