Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# httr2 (development version)

* Functions that capture interrutps (like `req_perform_parallel()` and friends) are now easier to escape if they're called inside a loop: you can press Ctrl + C twice to guarantee an exit (#1810).
* New `last_request_json()` and `last_response_json()` to conveniently see JSON bodies (#734).
* `req_body_json_modify()` can now be used on a request with an empty body.
* `resp_timing()` exposes timing information about the request measured by libcurl (@arcresu, #725).
Expand Down
1 change: 1 addition & 0 deletions R/httr2-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ the$token_cache <- new_environment()
the$last_response <- NULL
the$last_request <- NULL
the$pool_pollers <- new_environment()
the$last_interrupt <- 0
2 changes: 2 additions & 0 deletions R/req-perform-iterative.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@
}
},
interrupt = function(cnd) {
check_repeated_interrupt()

Check warning on line 184 in R/req-perform-iterative.R

View check run for this annotation

Codecov / codecov/patch

R/req-perform-iterative.R#L184

Added line #L184 was not covered by tests

# interrupt might occur after i was incremented
if (is.null(resps[[i]])) {
i <<- i - 1
Expand Down
2 changes: 2 additions & 0 deletions R/req-perform-parallel.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
tryCatch(
queue$process(),
interrupt = function(cnd) {
check_repeated_interrupt()

Check warning on line 95 in R/req-perform-parallel.R

View check run for this annotation

Codecov / codecov/patch

R/req-perform-parallel.R#L94-L95

Added lines #L94 - L95 were not covered by tests
queue$queue_status <- "errored"
queue$process()

Expand Down
2 changes: 2 additions & 0 deletions R/req-perform-sequential.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ req_perform_sequential <- function(
}
},
interrupt = function(cnd) {
check_repeated_interrupt()

resps <- resps[seq_len(i)]
cli::cli_alert_warning(
"Terminating iteration; returning {i} response{?s}."
Expand Down
10 changes: 10 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,13 @@
paste_c <- function(..., collapse = "") {
paste0(c(...), collapse = collapse)
}

# Give user the get-out-of-jail-free card if interrupt-capturing function
# is wrapped inside a loop
check_repeated_interrupt <- function() {
if (as.double(Sys.time()) - the$last_interrupt < 1) {
cli::cli_alert_warning("Interrupting")
interrupt()

Check warning on line 356 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L354-L356

Added lines #L354 - L356 were not covered by tests
}
the$last_interrupt <- as.double(Sys.time())

Check warning on line 358 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L358

Added line #L358 was not covered by tests
}