-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add helper to perform a request in chunks #303
Comments
With the API in #353, I'm having a hard time seeing where httr2 could help out. This mostly looks pretty nice to me: req <- request("https://api.restful-api.dev/objects")
ids <- sort(sample(100, 50))
chunks <- split(ids, (seq_along(ids) - 1) %/% (length(ids) / 10))
reqs <- chunks %>% lapply(\(idx) req %>% req_url_query(id = idx, .multi = "comma"))
resps <- reqs %>% req_perform_parallel()
resps_combine(resps, \(resp) resp_body_json(resp)) The only bit that feels like it wants a wrapper is the splitting, but it's hard to how that wrapper would fit into httr2's mission. |
Yes, this is right. As this is a very common pattern maybe we should at least mention it in a vignette and use your reprex as an example in the documentation of some of the functions? At least the splitting feels non-trivial to me to get right. |
But if we recommend to use |
Something like this would almost work with iterate_with_data <- function(data, chunk_size, req_update) {
chunks <- split(data, (seq_along(data) - 1) %/% (length(data) / chunk_size))
i <- 1
function(resp, req) {
if (i <= length(chunkss)) {
req <- req_update(req, chunks[[i]])
i <<- i + 1
req
}
}
} But to make it actually work, we'd need to give |
One possible idea would be to allow you to use |
I frequently use such a helper when working with REST APIs:
POST
as it uses a bodyUsually, there is a limit on the number of elements that can be updated/requested, so the requests have to be chunked.
This is kind of similar to
req_paginate()
but the user has to do the "pagination" himself.Below is an example of an implementation I use:
The text was updated successfully, but these errors were encountered: