Skip to content

Commit 54e9dcd

Browse files
Copilotjonthegeek
andauthored
Clean up resp_parse() NULL-page handling and align parser docs/vignette (#91)
* Initial plan * chore: record baseline validation and investigation Agent-Logs-Url: https://github.com/api2r/nectar/sessions/32587dad-c73c-44d5-ba1f-ba55b2979338 Co-authored-by: jonthegeek <33983824+jonthegeek@users.noreply.github.com> * fix: clean up resp_parse null handling and docs Agent-Logs-Url: https://github.com/api2r/nectar/sessions/32587dad-c73c-44d5-ba1f-ba55b2979338 Co-authored-by: jonthegeek <33983824+jonthegeek@users.noreply.github.com> * Reflow docs * Apply suggestions from code review Co-authored-by: Jon Harmon <jonthegeek@gmail.com> Signed-off-by: Jon Harmon <jonthegeek@gmail.com> --------- Signed-off-by: Jon Harmon <jonthegeek@gmail.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jonthegeek <33983824+jonthegeek@users.noreply.github.com> Co-authored-by: Jon Harmon <jonthegeek@gmail.com>
1 parent 147294b commit 54e9dcd

6 files changed

Lines changed: 54 additions & 10 deletions

File tree

R/aaa-shared_params.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@
7171
#' @param resp_body_fn (`function`) A function to extract the body of the
7272
#' response. Default: [resp_body_auto()].
7373
#' @param response_parser (`function`) A function to parse the server response
74-
#' (`resp`). Defaults to [httr2::resp_body_json()], since JSON responses are
75-
#' common. Set this to `NULL` to return the raw response from
76-
#' [httr2::req_perform()].
74+
#' (`resp`). Defaults to [resp_tidy()], which applies a tidying policy from
75+
#' the request when available and otherwise uses [resp_body_auto()]. Set this
76+
#' to `NULL` to return the raw response from [httr2::req_perform()].
7777
#' @param response_parser_args (`list`) An optional list of arguments to pass to
7878
#' the `response_parser` function (in addition to `resp`).
7979
#' @param spec (`tspec` or `NULL`) A specification used by

R/resp_parse.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ resp_parse.list <- function(resps, ..., response_parser = resp_tidy) {
8484
}
8585

8686
.resps_combine <- function(resps_parsed) {
87-
purrr::discard(resps_parsed, is.null)
87+
resps_parsed <- purrr::discard(resps_parsed, is.null)
88+
if (!length(resps_parsed)) {
89+
return(NULL)
90+
}
8891
if (inherits(resps_parsed[[1]], "raw")) {
8992
# This is tested, but covr doesn't believe it.
9093
return(resps_parsed) # nocov

man/dot-shared-params.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/resp_parse.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-resp_parse.R

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,39 @@ test_that("resp_parse parses lists of httr2_responses (#10)", {
6161
expect_identical(test_result, 1:6)
6262
})
6363

64+
test_that("resp_parse drops NULL parsed pages before combining (#90)", {
65+
resps <- list(
66+
httr2::response_json(body = 1:2),
67+
httr2::response_json(body = list()),
68+
httr2::response_json(body = 3:4)
69+
)
70+
parser <- function(resp) {
71+
body <- httr2::resp_body_json(resp)
72+
if (!length(body)) {
73+
return(NULL)
74+
}
75+
body
76+
}
77+
test_result <- resp_parse(resps, response_parser = parser)
78+
expect_identical(test_result, as.list(1:4))
79+
})
80+
81+
test_that("resp_parse returns NULL when all parsed pages are NULL (#90)", {
82+
resps <- list(
83+
httr2::response_json(body = list()),
84+
httr2::response_json(body = list())
85+
)
86+
parser <- function(resp) {
87+
body <- httr2::resp_body_json(resp)
88+
if (!length(body)) {
89+
return(NULL)
90+
}
91+
body
92+
}
93+
test_result <- resp_parse(resps, response_parser = parser)
94+
expect_null(test_result)
95+
})
96+
6497
test_that("resp_parse works for raw results (#11)", {
6598
# reqs <- list(
6699
# httr2::request("https://httr2.r-lib.org/logo.png"),

vignettes/nectar.Rmd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ The main entry point in nectar is `req_prepare()`. It wraps `httr2::request()` a
2626

2727
Here we prepare a request to the Crossref `/works` endpoint. We ask for ten results per page (`rows = 10`), select only the "publisher" and "DOI" fields, tell `{httr2}` to concatenate the `select` parameter with commas (`.multi`), and set the `cursor` parameter to `"*"` to trigger cursor-based pagination:
2828

29+
```{r prepare, eval = FALSE}
30+
req <- req_prepare(
31+
"https://api.crossref.org/works",
32+
query = list(
33+
rows = 10, cursor = "*", select = c("publisher", "DOI"), .multi = "comma"
34+
)
35+
)
36+
```
2937

3038
## Authentication with `auth_api_key()`
3139

0 commit comments

Comments
 (0)