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

Add paginate_req_perform_continue() #306

Closed
wants to merge 4 commits into from
Closed

Conversation

mgirlich
Copy link
Collaborator

@mgirlich mgirlich commented Sep 5, 2023

Closes #298.

devtools::load_all("~/GitHub/httr2/")
#> ℹ Loading httr2
page_size <- 7

req_pokemon <- request("https://pokeapi.co/api/v2/pokemon") %>%
  req_url_query(limit = page_size) %>%
  req_verbose(header_resp = FALSE) %>% 
  req_paginate_next_url(
    next_url = function(resp) resp_body_json(resp)[["next"]],
    n_pages = function(resp) {
      total <- resp_body_json(resp)$count
      ceiling(total / page_size)
    }
  )

responses <- paginate_req_perform(req_pokemon, max_pages = 2)
#> -> GET /api/v2/pokemon?limit=7 HTTP/2
#> -> Host: pokeapi.co
#> -> User-Agent: httr2/0.2.3.9000 r-curl/5.0.2 libcurl/8.1.2
#> -> Accept: */*
#> -> Accept-Encoding: deflate, gzip
#> -> 
#> -> GET /api/v2/pokemon?offset=7&limit=7 HTTP/2
#> -> Host: pokeapi.co
#> -> User-Agent: httr2/0.2.3.9000 r-curl/5.0.2 libcurl/8.1.2
#> -> Accept: */*
#> -> Accept-Encoding: deflate, gzip
#> ->

# oh, I actually wanted 3 pages, not 2
responses_all <- paginate_req_perform_continue(max_pages = 3)
#> -> GET /api/v2/pokemon?offset=14&limit=7 HTTP/2
#> -> Host: pokeapi.co
#> -> User-Agent: httr2/0.2.3.9000 r-curl/5.0.2 libcurl/8.1.2
#> -> Accept: */*
#> -> Accept-Encoding: deflate, gzip
#> ->

Created on 2023-09-05 with reprex v2.0.2

@@ -15,3 +15,8 @@ the$cache_throttle <- list()
the$token_cache <- new_environment()
the$last_response <- NULL
the$last_request <- NULL
the$last_pagination_request <- NULL
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should go into a list variable?

the$last_pagination <- list(
  request = NULL,
  responses = NULL,
  page = NULL,
  n_pages = NULL,
  max_pages = NULL
)

cli::cli_progress_bar(
"Paginate",
total = n_pages,
format = "{cli::pb_spin} Page {cli::pb_current}/{cli::pb_total} | ETA: {cli::pb_eta}",
current = 1L
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

current is actually not the current page as I though but whether this is the current progress bar or not.


#' @export
#' @rdname last_response
last_pagination_responses <- function() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also export last_pagination_page()?

@mgirlich mgirlich requested a review from hadley September 5, 2023 12:14
@mgirlich mgirlich mentioned this pull request Sep 5, 2023
@@ -56,6 +56,11 @@ req_paginate <- function(req,

#' Perform a paginated request
#'
#' After preparing a paginated request with [req_paginate()] perform it with
#' [paginate_req_perform()]. In case the requests were interrupted, you can
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to copy the multi_req_perform() interface here? i.e. paginate_req_perform() should always succeed but sometimes return an error object? Or maybe in this case it's sufficient to return a shorter list than expected with a warning.

You could use the same technique as in pool_run() to handle user interrupts.

@hadley
Copy link
Member

hadley commented Oct 24, 2023

Superseded by #353

@hadley hadley closed this Oct 24, 2023
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

Successfully merging this pull request may close these issues.

Store intermediate responses in req_paginate()
2 participants