diff --git a/R/generate_pkg-paths.R b/R/generate_pkg-paths.R index 969cfa2..5080bb8 100644 --- a/R/generate_pkg-paths.R +++ b/R/generate_pkg-paths.R @@ -224,6 +224,7 @@ S7::method(as_bk_data, class_paths) <- function(x) { params <- .remove_security_args(op$params, security_arg_names) params_query <- .prep_param_args(op$params_query_raw, security_arg_names) params_header <- .prep_param_args(op$params_header_raw, security_arg_names) + params_cookie <- .prep_param_args(op$params_cookie_raw, security_arg_names) args <- .params_to_args(params) args_named <- .params_to_named_args(params) c( @@ -232,6 +233,7 @@ S7::method(as_bk_data, class_paths) <- function(x) { params = params, params_query = params_query, params_header = params_header, + params_cookie = params_cookie, args = args, args_named = args_named, test_args = args diff --git a/inst/templates/010-prepare.R b/inst/templates/010-prepare.R index 3de93aa..87b41af 100644 --- a/inst/templates/010-prepare.R +++ b/inst/templates/010-prepare.R @@ -15,6 +15,8 @@ path, query = list(), body = NULL, + header = list(), + cookie = list(), method = NULL, tidy_policy = nectar::tidy_policy_unknown(),{{#has_security}} {{{security_signature}}},{{/has_security}} @@ -24,6 +26,8 @@ path = path, query = query, body = body, + header = header, + cookie = cookie, method = method, tidy_policy = tidy_policy, call = call diff --git a/inst/templates/paths.R b/inst/templates/paths.R index f5eb9a1..e793004 100644 --- a/inst/templates/paths.R +++ b/inst/templates/paths.R @@ -29,7 +29,8 @@ req_{{api_abbr}}_{{operation_id}} <- function({{#args}}{{{args}}}{{/args}}{{#has method = "{{method}}"{{#has_security}}, {{security_arg_list}}{{/has_security}}{{#params_query}}, query = list({{params_query}}){{/params_query}}{{#params_header}}, - body = list({{params_header}}){{/params_header}}{{#pagination}}, + header = list({{params_header}}){{/params_header}}{{#params_cookie}}, + cookie = list({{params_cookie}}){{/params_cookie}}{{#pagination}}, pagination_fn = {{pagination_fn}}{{/pagination}}{{#tidy}}, tidy_policy = {{tidy_policy}}{{/tidy}} ) diff --git a/tests/testthat/_fixtures/guru/010-prepare.R b/tests/testthat/_fixtures/guru/010-prepare.R index d4dc478..e72d26b 100644 --- a/tests/testthat/_fixtures/guru/010-prepare.R +++ b/tests/testthat/_fixtures/guru/010-prepare.R @@ -14,6 +14,8 @@ guru_req_prepare <- function( path, query = list(), body = NULL, + header = list(), + cookie = list(), method = NULL, tidy_policy = nectar::tidy_policy_unknown(), call = rlang::caller_env()) { @@ -22,6 +24,8 @@ guru_req_prepare <- function( path = path, query = query, body = body, + header = header, + cookie = cookie, method = method, tidy_policy = tidy_policy, call = call diff --git a/tests/testthat/_fixtures/header_cookie/paths-things-search_things.R b/tests/testthat/_fixtures/header_cookie/paths-things-search_things.R new file mode 100644 index 0000000..ff325ca --- /dev/null +++ b/tests/testthat/_fixtures/header_cookie/paths-things-search_things.R @@ -0,0 +1,36 @@ +# These functions were generated by the {beekeeper} package, based on the paths +# element from the source API description. You should carefully review these +# functions. + +#' Search things +#' +#' Search for things. +#' +#' @param x_auth_token (length-1 `character`) Authentication token +#' @param session_id (length-1 `character`) Session identifier +#' @param q (length-1 `character`) Search query +#' @inheritParams .shared-params +#' +#' @returns `test_search_things()`: The API response. +#' @export +test_search_things <- function(x_auth_token, session_id, q, max_reqs = Inf, max_tries_per_req = 3) { + req <- req_test_search_things(x_auth_token = x_auth_token, session_id = session_id, q = q) + resps <- nectar::req_perform_opinionated( + req, + max_reqs = max_reqs, + max_tries_per_req = max_tries_per_req + ) + return(nectar::resp_parse(resps)) +} + +#' @rdname test_search_things +#' @returns `req_test_search_things()`: A `httr2_request` request object. +req_test_search_things <- function(x_auth_token, session_id, q) { + test_req_prepare( + path = "/things", + method = "get", + query = list(q = q), + header = list(x_auth_token = x_auth_token), + cookie = list(session_id = session_id) + ) +} diff --git a/tests/testthat/_fixtures/trello/010-prepare.R b/tests/testthat/_fixtures/trello/010-prepare.R index 64954de..9ab9a2c 100644 --- a/tests/testthat/_fixtures/trello/010-prepare.R +++ b/tests/testthat/_fixtures/trello/010-prepare.R @@ -15,6 +15,8 @@ trello_req_prepare <- function( path, query = list(), body = NULL, + header = list(), + cookie = list(), method = NULL, tidy_policy = nectar::tidy_policy_unknown(), key = Sys.getenv("TRELLO_KEY"), @@ -25,6 +27,8 @@ token = Sys.getenv("TRELLO_TOKEN"), path = path, query = query, body = body, + header = header, + cookie = cookie, method = method, tidy_policy = tidy_policy, call = call diff --git a/tests/testthat/test-generate_pkg-paths.R b/tests/testthat/test-generate_pkg-paths.R index 9386476..e635798 100644 --- a/tests/testthat/test-generate_pkg-paths.R +++ b/tests/testthat/test-generate_pkg-paths.R @@ -257,3 +257,53 @@ test_that(".compile_param_class_descriptions() uses class names (#85)", { ) ) }) + +test_that(".generate_paths_file() renders header and cookie params correctly (#84)", { + skip_on_cran() + expected_content <- readLines( + test_path("_fixtures", "header_cookie", "paths-things-search_things.R") + ) + + tmp <- withr::local_tempdir() + local_mocked_bindings(.bk_use_template_impl = make_writing_impl(tmp)) + + op <- list( + operation_id = "search_things", + tag = "things", + path = '"/things"', + method = "get", + summary = "Search things", + description = "Search for things.", + params = list( + list( + name = "x_auth_token", + class = "length-1 `character`", + description = "Authentication token" + ), + list( + name = "session_id", + class = "length-1 `character`", + description = "Session identifier" + ), + list( + name = "q", + class = "length-1 `character`", + description = "Search query" + ) + ), + params_query = "q = q", + params_header = "x_auth_token = x_auth_token", + params_cookie = "session_id = session_id", + args = "x_auth_token, session_id, q", + args_named = "x_auth_token = x_auth_token, session_id = session_id, q = q", + test_args = "x_auth_token, session_id, q", + pagination = FALSE, + pagination_fn = "" + ) + .generate_paths_file(op, "search_things", "test", list()) + + expect_identical( + readLines(file.path(tmp, "R", "paths-things-search_things.R")), + expected_content + ) +})