dslc-io / club-wapir

https://dslc-io.github.io/club-wapir/
2 stars 2 forks source link

pagination & cursor (crossref.org) - add Counter #50

Closed jimrothstein closed 5 months ago

jimrothstein commented 5 months ago

To reduce risk of running this lengthy call by accident or without thinking. ``` file: httr2-pagination.qmd

httr2-pagination-crossref-multi

#| label: **httr2-pagination-crossref-multi**
crossref_multi <- 
  crossref_request |> 
  req_retry(max_tries = 3) |> 
  req_perform_iterative(
    next_req = iterate_with_offset(
      "offset",
      resp_pages = \(resp) {
        content <- resp_body_json(resp)
        content$pagination$message$`total-results`
      }
    ),
    max_reqs = Inf
  )
length(crossref_multi)
#> [1] 706

I did the following

Added a counter:

e = new.env(emptyenv())
e$counter = 0

Then in callback function, added lines, something like:

# return 4 entries only
if (e$counter > 4) return(NULL)
e$counter=e$counter+1
jonthegeek commented 5 months ago

That works, but you can also set max_reqs = 4 to do the same thing. I'll be sure to make sure that's clear!

jimrothstein commented 5 months ago

Then it must the cursor that runs everything right away.