Skip to content
9 changes: 7 additions & 2 deletions R/aaa-shared_params.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,20 @@
#' @param check_type (`length-1 logical`) Whether to check that the response has
#' the expected content type. Set to `FALSE` if the response is not
#' specifically tagged as the proper type.
#' @param cookie (`list` or `NULL`) An optional list of cookies to set on the
#' request using [httr2::req_cookies_set()]. `NULL` elements are removed.
#' @param existing_user_agent (`length-1 character`, optional) An existing user
#' agent, such as the value of `req$options$useragent` in a [httr2::request()]
#' object.
#' @param header (`list` or `NULL`) An optional list of headers to add to the
#' request using [httr2::req_headers()]. A `NULL` value for an individual
#' header will explicitly remove that header if it was previously set.
#' @param location (`length-1 character`) Where the API key should be passed.
#' One of `"header"` (default), `"query"`, or `"cookie"`.
#' @param method (`length-1 character`, optional) If the method is something
#' other than `GET` or `POST`, supply it. Case is ignored.
#' @param mime_type (`length-1 character`) The mime type of any files present in
#' the body. Some APIs allow you to leave this as NULL for them to guess.
#' @param location (`length-1 character`) Where the API key should be passed.
#' One of `"header"` (default), `"query"`, or `"cookie"`.
#' @param name (`length-1 character`) The name of a package or other thing to
#' add to or remove from the user agent string.
#' @param pagination_fn (`function`) A function that takes the previous response
Expand Down
16 changes: 16 additions & 0 deletions R/req_modify.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ req_modify <- function(
body = NULL,
mime_type = NULL,
method = NULL,
header = NULL,
cookie = NULL,
call = rlang::caller_env()
) {
rlang::check_dots_empty()
req <- .req_path_append(req, path, call = call)
req <- .req_query_flatten(req, query)
req <- .req_body_auto(req, body, mime_type, call = call)
req <- .req_method_apply(req, method, call = call)
if (length(header)) {
req <- rlang::inject(httr2::req_headers(req, !!!header))
}
req <- .req_cookies_flatten(req, cookie)
return(.as_nectar_request(req))
}

Expand Down Expand Up @@ -57,6 +63,16 @@ req_modify <- function(
rlang::inject(httr2::req_url_query(req, !!!query))
}

#' Add non-empty cookie elements to a request
#'
#' @inheritParams .shared-params
#' @inherit .shared-request return
#' @keywords internal
.req_cookies_flatten <- function(req, cookie) {
cookie <- purrr::discard(cookie, is.null)
rlang::inject(httr2::req_cookies_set(req, !!!cookie))
}

#' Add a method if it is supplied
#'
#' [httr2::req_method()] errors if `method` is `NULL`, rather than using the
Expand Down
4 changes: 4 additions & 0 deletions R/req_prepare.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ req_prepare <- function(
body = NULL,
mime_type = NULL,
method = NULL,
header = NULL,
cookie = NULL,
additional_user_agent = NULL,
auth = NULL,
tidy_policy = tidy_policy_body_auto(),
Expand All @@ -44,6 +46,8 @@ req_prepare <- function(
body = body,
mime_type = mime_type,
method = method,
header = header,
cookie = cookie,
call = call
)
auth <- .as_nectar_auth(auth, call = call)
Expand Down
21 changes: 21 additions & 0 deletions man/dot-req_cookies_flatten.Rd

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

13 changes: 10 additions & 3 deletions man/dot-shared-params.Rd

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

9 changes: 9 additions & 0 deletions man/req_modify.Rd

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

9 changes: 9 additions & 0 deletions man/req_prepare.Rd

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

25 changes: 25 additions & 0 deletions tests/testthat/_snaps/req_modify.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# req_modify() handles bodies with paths

Code
test_result <- req_modify(req_base, body = list(foo = "bar", baz = fs::path(
test_path("fixtures", "img-test.png"))))
test_result$body
Output
$data
$data$foo
Form data of length 5 (type: application/json)

$data$baz
Form file: img-test.png


$type
[1] "multipart"

$content_type
NULL

$params
list()


22 changes: 22 additions & 0 deletions tests/testthat/_snaps/req_prepare.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@
list()


# req_prepare() errors for unsupported tidy policy objects (#86)

Code
(expect_pkg_error_classes(req_prepare(base_url = "https://example.com",
tidy_policy = "not_tidy_policy"), "nectar", "unsupported_tidy_policy_class"))
Output
<error/nectar-error-unsupported_tidy_policy_class>
Error:
! `not_tidy_policy` must be `NULL` or a <nectar_tidy_policy>.
x `not_tidy_policy` is a string.

# req_prepare() errors for unsupported auth objects (#81)

Code
(expect_pkg_error_classes(req_prepare(base_url = "https://example.com", auth = "not_auth"),
"nectar", "unsupported_auth_class"))
Output
<error/nectar-error-unsupported_auth_class>
Error:
! `not_auth` must be `NULL` or a <nectar_auth>.
x `not_auth` is a string.

# .as_nectar_request() fails gracefully for non-reqs

Code
Expand Down
Loading
Loading