Skip to content
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

End iteration for blank cursor #612

Closed
jonthegeek opened this issue Dec 23, 2024 · 4 comments
Closed

End iteration for blank cursor #612

jonthegeek opened this issue Dec 23, 2024 · 4 comments

Comments

@jonthegeek
Copy link
Contributor

iterate_with_cursor() checks that the next cursor is NULL. I've had to build in a check to convert empty strings "" to NULL, for example for the Slack API. Would it be acceptable to build that into the default checker?
https://github.com/r-lib/httr2/blob/main/R%2Fiterate-helpers.R#L100

The same idea likely also occurs for iterate_with_link_url().

@hadley
Copy link
Member

hadley commented Dec 23, 2024

Can you point me to the docs or show an example?

@jonthegeek
Copy link
Contributor Author

You can kinda see it in the example response here: https://api.slack.com/methods/admin.apps.requests.list

I'll pull up specific examples when I'm back on my laptop.

@jonthegeek
Copy link
Contributor Author

And this is where I clean it up: https://github.com/jonthegeek/slackapi/blob/main/R%2F040-pagination.R

@hadley
Copy link
Member

hadley commented Dec 24, 2024

Given that you already have to write a function to extract it and this is a bit of a special case, I think your package is the right place for the code. I'd write it like this:

.iterator_fn_cursor <- function() {
  httr2::iterate_with_cursor(
    "cursor",
    resp_param_value = function(resp) {
      cursor <- httr2::resp_body_json(resp)$response_metadata$next_cursor
      fix_cursor(cursor)
    }
  )
}

fix_cursor <- function(x) {
  if (identical(cursor, "")) {
    NULL
  } else {
    cursor
  }
}

@hadley hadley closed this as completed Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants