Skip to content

Commit

Permalink
Add resp_stream_is_complete()
Browse files Browse the repository at this point in the history
To determine if there is data remaining on the stream. Fixes #559
  • Loading branch information
hadley committed Dec 23, 2024
1 parent 9db8f7e commit 55229aa
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export(resp_retry_after)
export(resp_status)
export(resp_status_desc)
export(resp_stream_aws)
export(resp_stream_is_complete)
export(resp_stream_lines)
export(resp_stream_raw)
export(resp_stream_sse)
Expand Down
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)

* `resp_stream_is_complete()` tells you if there is still data remaining to be streamed (#559).
* `url_parse()` now uses `curl::curl_parse_url()` which is much faster and more correct (#577).
* `req_retry()` now defaults to `max_tries = 2` with a message.
Set to `max_tries = 1` to disable retries.
Expand Down
11 changes: 11 additions & 0 deletions R/resp-stream.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#' * `resp_stream_aws()` retrieves a single event from an AWS stream
#' (i.e. mime type `application/vnd.amazon.eventstream``).
#'
#' Use `resp_stream_is_complete()` to determine if there is further data
#' waiting on the stream.
#'
#' @returns
#' * `resp_stream_raw()`: a raw vector.
#' * `resp_stream_lines()`: a character vector.
Expand Down Expand Up @@ -80,6 +83,14 @@ resp_stream_sse <- function(resp, max_size = Inf) {
}
}

#' @export
#' @rdname resp_stream_raw
resp_stream_is_complete <- function(resp) {
check_response(resp)

Check warning on line 89 in R/resp-stream.R

View check run for this annotation

Codecov / codecov/patch

R/resp-stream.R#L89

Added line #L89 was not covered by tests

!isIncomplete(resp$body)

Check warning on line 91 in R/resp-stream.R

View check run for this annotation

Codecov / codecov/patch

R/resp-stream.R#L91

Added line #L91 was not covered by tests
}

#' @export
#' @param ... Not used; included for compatibility with generic.
#' @rdname resp_stream_raw
Expand Down
6 changes: 6 additions & 0 deletions man/resp_stream_raw.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tests/testthat/test-resp-stream.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ test_that("can stream bytes from a connection", {
expect_length(out, 0)
})

test_that("can determine if a stream is complete (blocking)", {
resp <- request_test("/stream-bytes/2048") %>% req_perform_connection()
withr::defer(close(resp))

expect_false(resp_stream_is_complete(resp))
resp_stream_raw(resp, kb = 2)
expect_true(resp_stream_is_complete(resp))
})

test_that("can determine if a stream is complete (non-blocking)", {
resp <- request_test("/stream-bytes/2048") %>% req_perform_connection(blocking = FALSE)
withr::defer(close(resp))

expect_false(resp_stream_is_complete(resp))
resp_stream_raw(resp, kb = 2)
expect_true(resp_stream_is_complete(resp))
})

test_that("can't read from a closed connection", {
resp <- request_test("/stream-bytes/1024") %>% req_perform_connection()
close(resp)
Expand Down

0 comments on commit 55229aa

Please sign in to comment.