# One use of req_perform_sequential() is if the API allows you to request
# data for multiple objects, you want data for more objects than can fit
# in one request.
req <- request("https://api.restful-api.dev/objects")
# Imagine we have 50 ids:
ids <- sort(sample(100, 50))
# But the API only allows us to request 10 at time. So we first use split
# and some modulo arithmetic magic to generate chunks of length 10
chunks <- unname(split(ids, (seq_along(ids) - 1) %/% 10))
# Then we use lapply to generate one request for each chunk:
reqs <- chunks |> lapply(\(idx) req |> req_url_query(id = idx, .multi = "comma"))
# Then we can perform them all and get the results
if (FALSE) {
resps <- reqs |> req_perform_sequential()
resps_data(resps, \(resp) resp_body_json(resp))
}
Run the code above in your browser using DataLab