Skip to content

Instantly share code, notes, and snippets.

@katembu
Last active July 9, 2021 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katembu/6b745ce40413cc4a1e7867fb75e324c7 to your computer and use it in GitHub Desktop.
Save katembu/6b745ce40413cc4a1e7867fb75e324c7 to your computer and use it in GitHub Desktop.
rapidpro_r_connector
library(httr)
library(jsonlite)
#> API Token
API_token <- "Token XXXXX"
#> First URL without pagination
url_api <- "https://rapidpro.ona.io/api/v2/runs.json?flow=XXXXX"
#> Request for data. This need to be converted to a function
request <- GET(url_api , add_headers("Authorization"= API_token))
data <- fromJSON(rawToChar(request$content))
#> print(names(data)); List objects names >> "next", "previous", "results"
#> next == link to next page. Null if there are no other pages
#> previous == link to next page. By default its NULL
#> results == results.
#> Instantiate DataFrame for all data
all_data <- data[['results']]
#> Get value of next(link to another page )
another_page <- data[['next']]
#> Check if next is null/blank. Basically the API is paginating 250 records
#> if next is null it means on one page is available (Records less than 250)
#> create a loop to check if next is null, if not querry the next url and ammend the data frame
while (!is.null(another_page))
{
request <- GET(another_page, add_headers("Authorization"= API_token))
data <- fromJSON(rawToChar(request$content))
another_page <- data[['next']]
#> Combine the pages using try to prevent the error stopping the process
all_data <- rbind_pages(list(all_data, data[['results']]))
}
#> Total number of rows just to be sure that its working
print(nrow(all_data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment