-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
req_tidy_policy()
function to define policies for resp_tidy()
. (
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#' Apply policies to a request | ||
#' | ||
#' This function is based on the unexported `req_policies()` function from | ||
#' httr2. It is used to apply policies to a request object. I don't currently | ||
#' export this function, but that may change in the future. | ||
#' | ||
#' @inheritParams .shared-params | ||
#' @param ... | ||
#' | ||
#' @inherit .shared-request return | ||
#' @keywords internal | ||
.req_policy <- function(req, ..., call = rlang::caller_env()) { | ||
dots <- rlang::list2(...) | ||
if (!length(dots)) { | ||
return(req) | ||
} | ||
if (!rlang::is_named(dots)) { | ||
.nectar_abort( | ||
"All components of {.arg ...} must be named.", | ||
"bad_policy", | ||
call = call | ||
) | ||
} | ||
req$policies <- req$policies[!names(req$policies) %in% names(dots)] | ||
req$policies <- c(req$policies, Filter(length, dots)) | ||
if (!length(req$policies)) { | ||
names(req$policies) <- NULL | ||
} | ||
return(req) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#' Define a tidy policy for a request | ||
#' | ||
#' API responses generally follow a structured format. Use this function to | ||
#' define a policy that will be used by [resp_tidy()] to extract the relevant | ||
#' portion of a response and wrangle it into a desired format. | ||
#' | ||
#' @inheritParams .shared-params | ||
#' @param tidy_fn A function that will be invoked by [resp_tidy()] to tidy the | ||
#' response. | ||
#' @param tidy_args A list of additional arguments to pass to `tidy_fn`. | ||
#' | ||
#' @inherit .shared-request return | ||
#' @export | ||
#' | ||
#' @examples | ||
#' req <- httr2::request("https://example.com") | ||
#' req_tidy_policy(req, httr2::resp_body_json, list(simplifyVector = TRUE)) | ||
req_tidy_policy <- function(req, | ||
tidy_fn = resp_body_auto, | ||
tidy_args = list(), | ||
call = rlang::caller_env()) { | ||
tidy_fn <- rlang::as_function(tidy_fn, call = call) | ||
.req_policy( | ||
req, | ||
resp_tidy = list(tidy_fn = tidy_fn, tidy_args = tidy_args), | ||
call = call | ||
) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
test_that(".req_policy returns reqs unchanged when no policies added", { | ||
req <- httr2::request("https://example.com") | ||
expect_identical( | ||
.req_policy(req), | ||
req | ||
) | ||
}) | ||
|
||
test_that(".req_policy errors informatively for unnamed policies", { | ||
req <- httr2::request("https://example.com") | ||
expect_error( | ||
.req_policy(req, list(my_policy = "whatever")), | ||
"must be named", | ||
class = "nectar_error-bad_policy" | ||
) | ||
}) | ||
|
||
test_that(".req_policy applies a policy", { | ||
req <- httr2::request("https://example.com") | ||
new_policy <- list(a = 1, b = "thing") | ||
test_result <- .req_policy(req, new_policy = new_policy) | ||
expect_identical( | ||
test_result$policies, | ||
list(new_policy = new_policy) | ||
) | ||
}) | ||
|
||
test_that(".req_policy adds to existing policies", { | ||
req <- httr2::request("https://example.com") | ||
new_policy <- list(a = 1, b = "thing") | ||
new_policy2 <- list(a = 2, b = "thing2") | ||
req <- .req_policy(req, new_policy = new_policy) | ||
test_result <- .req_policy(req, new_policy2 = new_policy2) | ||
expect_identical( | ||
test_result$policies, | ||
list(new_policy = new_policy, new_policy2 = new_policy2) | ||
) | ||
}) | ||
|
||
test_that(".req_policy removes emptied policies", { | ||
req <- httr2::request("https://example.com") | ||
new_policy <- list(a = 1, b = "thing") | ||
req <- .req_policy(req, new_policy = new_policy) | ||
test_result <- .req_policy(req, new_policy = NULL) | ||
expect_identical( | ||
test_result$policies, | ||
list() | ||
) | ||
}) |