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
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ export(resp_body_tsv)
export(resp_parse)
export(resp_tidy)
export(resp_tidy_json)
export(resp_tidy_json_tibblify)
export(resp_tidy_unknown)
export(tidy_policy_body_auto)
export(tidy_policy_json)
export(tidy_policy_json_tibblify)
export(tidy_policy_prepare)
export(tidy_policy_unknown)
export(url_normalize)
Expand Down
48 changes: 15 additions & 33 deletions R/resp_tidy_json.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#' Extract and clean a JSON API response
#' Extract and optionally subset a JSON API response
#'
#' Parse the body of a response with [httr2::resp_body_json()], extract a named
#' subset of that body, and tidy the result with [tibblify::tibblify()].
#' Parse the body of a response with [httr2::resp_body_json()] and optionally
#' extract a named subset of that body.
#'
#' @inheritParams .shared-params
#' @inheritParams httr2::resp_body_json
#'
#' @returns The tibblified response body.
#' @returns The parsed response body, or `NULL` for an empty result.
#' @family opinionated response parsers
#' @export
#'
#' @examplesIf rlang::is_installed("tibblify")
#' @examples
#' resp <- httr2::response_json(
#' body = list(list(id = 1, name = "Alice"), list(id = 2, name = "Bob"))
#' )
Expand All @@ -20,28 +21,13 @@
#' body = list(data = list(list(id = 1), list(id = 2)))
#' )
#' resp_tidy_json(resp_nested, subset_path = "data")
resp_tidy_json <- function(
resp,
spec = NULL,
unspecified = "list",
subset_path = NULL
) {
rlang::check_installed(
"tibblify",
"to tidy the JSON response body."
)
# Let httr2 and tibblify validate their respective inputs, but check ours.
resp_tidy_json <- function(resp, subset_path = NULL, simplifyVector = FALSE) {
# Let httr2 validate its own inputs, but check ours.
subset_path <- stbl::to_chr(subset_path)
result <- httr2::resp_body_json(resp)
result <- httr2::resp_body_json(resp, simplifyVector = simplifyVector)
result <- purrr::pluck(result, !!!subset_path)
if (length(result)) {
return(
tibblify::tibblify(
result,
spec = spec,
unspecified = unspecified
)
)
return(result)
}
return(NULL)
}
Expand All @@ -51,22 +37,18 @@ resp_tidy_json <- function(
#' Create a reusable tidy policy that applies [resp_tidy_json()].
#'
#' @inheritParams .shared-params
#' @inheritParams httr2::resp_body_json
#' @returns A list with class `"nectar_tidy_policy"` and elements `tidy_fn` and
#' `tidy_args`.
#' @family opinionated response parsers
#' @export
#'
#' @examplesIf rlang::is_installed("tibblify")
#' @examples
#' tidy_policy_json(subset_path = "data")
tidy_policy_json <- function(
spec = NULL,
unspecified = "list",
subset_path = NULL
) {
tidy_policy_json <- function(subset_path = NULL, simplifyVector = FALSE) {
tidy_policy_prepare(
resp_tidy_json,
spec = spec,
unspecified = unspecified,
subset_path = subset_path
subset_path = subset_path,
simplifyVector = simplifyVector
)
}
73 changes: 73 additions & 0 deletions R/resp_tidy_json_tibblify.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#' Extract and clean a JSON API response with tibblify
#'
#' Parse the body of a response with [resp_tidy_json()] and tidy the result with
#' [tibblify::tibblify()].
#'
#' @inheritParams .shared-params
#'
#' @returns The tibblified response body.
#' @family opinionated response parsers
#' @export
#'
#' @examplesIf rlang::is_installed("tibblify")
#' resp <- httr2::response_json(
#' body = list(list(id = 1, name = "Alice"), list(id = 2, name = "Bob"))
#' )
#' resp_tidy_json_tibblify(resp)
#'
#' # Extract a nested subset of the response body
#' resp_nested <- httr2::response_json(
#' body = list(data = list(list(id = 1), list(id = 2)))
#' )
#' resp_tidy_json_tibblify(resp_nested, subset_path = "data")
resp_tidy_json_tibblify <- function(
resp,
spec = NULL,
unspecified = "list",
subset_path = NULL
) {
result <- resp_tidy_json(
resp = resp,
subset_path = subset_path,
simplifyVector = FALSE
)
if (length(result)) {
rlang::check_installed(
"tibblify",
"to tidy the JSON response body."
)
return(
tibblify::tibblify(
result,
spec = spec,
unspecified = unspecified
)
)
}
return(NULL)
}

#' A policy to parse a response body as JSON
#'
#' Create a reusable tidy policy that applies [resp_tidy_json_tibblify()].
#'
#' @inheritParams .shared-params
#' @returns A list with class `"nectar_tidy_policy"` and elements `tidy_fn` and
#' `tidy_args`.
#' @family opinionated response parsers
#' @export
#'
#' @examplesIf rlang::is_installed("tibblify")
#' tidy_policy_json_tibblify(subset_path = "data")
tidy_policy_json_tibblify <- function(
spec = NULL,
unspecified = "list",
subset_path = NULL
) {
tidy_policy_prepare(
resp_tidy_json_tibblify,
spec = spec,
unspecified = unspecified,
subset_path = subset_path
)
}
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ req <- req_prepare(
query = list(
rows = 10, cursor = "*", select = c("publisher", "DOI"), .multi = "comma"
),
tidy_policy = tidy_policy_json(subset_path = c("message", "items")),
tidy_policy = tidy_policy_json_tibblify(subset_path = c("message", "items")),
pagination_fn = iterate_with_json_cursor(
param_name = "cursor",
next_cursor_path = c("message", "next-cursor")
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ req <- req_prepare(
query = list(
rows = 10, cursor = "*", select = c("publisher", "DOI"), .multi = "comma"
),
tidy_policy = tidy_policy_json(subset_path = c("message", "items")),
tidy_policy = tidy_policy_json_tibblify(subset_path = c("message", "items")),
pagination_fn = iterate_with_json_cursor(
param_name = "cursor",
next_cursor_path = c("message", "next-cursor")
Expand Down
2 changes: 2 additions & 0 deletions man/req_tidy_policy.Rd

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

2 changes: 2 additions & 0 deletions man/resp_tidy.Rd

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

28 changes: 10 additions & 18 deletions man/resp_tidy_json.Rd

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

69 changes: 69 additions & 0 deletions man/resp_tidy_json_tibblify.Rd

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

2 changes: 2 additions & 0 deletions man/resp_tidy_unknown.Rd

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

2 changes: 2 additions & 0 deletions man/tidy_policy_body_auto.Rd

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

Loading
Loading