Skip to content

Commit

Permalink
Test for max_reqs
Browse files Browse the repository at this point in the history
  • Loading branch information
asadow committed May 17, 2024
1 parent 33bf5bd commit d4601c6
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 13 deletions.
6 changes: 3 additions & 3 deletions R/extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
#' resp <- httr2::response_json()
#' resp |> megamation:::mm_resp_extract()
mm_resp_extract <- function(resp) {
.from <- sub(".*/@", "", resp$url) |> tolower()
.from <- switch(.from,
.get <- sub(".*/@", "", resp$url) |> tolower()
.get <- switch(.get,
"data",
labels = "labels",
criteria = "criteria",
schema = "schema"
)
resp |>
mm_resp_parse() |>
mm_parsed_extract(.from)
mm_parsed_extract(.get)
}
7 changes: 4 additions & 3 deletions R/perform.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
#' it with pagination and fetch the results back to R.
#'
#' @inheritParams mm_next_req
#' @inheritParams httr2::req_perform_iterative
#' @returns A list of HTTP responses. Each response is an S3 list with class
#' `httr2_response`. (For more on this class, see [httr2::response].) If the
#' request was paginated, these responses correspond to pages.
#' request was paginated, the responses correspond to pages.
#' @keywords internal
mm_req_perform <- function(req) {
mm_req_perform <- function(req, max_reqs = Inf) {
check_request(req)
httr2::req_perform_iterative(
req,
next_req = mm_next_req,
on_error = "return",
max_reqs = Inf
max_reqs = max_reqs
)
}
9 changes: 5 additions & 4 deletions R/pull.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
mm_data <- function(endpoint, ..., allfields = TRUE) {
gets <- mm_list_get_reqs(endpoint, ..., allfields = allfields)

cli::cli_alert_info("Requesting...")
cli::cli_alert_info("Requesting...\n")

responses <- purrr::map(
gets |>
cli::cli_progress_along(format = "Fulfilling request {cli::pb_current}"),
\(x) mm_req_perform(gets[[x]])
) |>
cli::cli_progress_along(
format = "Fulfilling request {cli::pb_current}"),
\(x) mm_req_perform(gets[[x]], max_reqs = Inf)
) |>
purrr::list_flatten()

errors <- responses |> httr2::resps_failures()
Expand Down
7 changes: 5 additions & 2 deletions man/mm_req_perform.Rd

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

7 changes: 7 additions & 0 deletions tests/testthat/test-get.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
without_internet({
expect_GET(
mm_data("workorder") |> suppressMessages(),
"https://api.megamation.com/uog/dl/workorder?ALLFIELDS=1"
)
})

with_mock_dir("mm_data", {
test_that("mm_data() returns a tibble", {
skip_on_cran()
Expand Down
13 changes: 12 additions & 1 deletion tests/testthat/test-perform.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
with_mock_dir("status_resp", {
test_that("mm_req_perform() returns list with HTTP response", {
skip_on_cran()
resp <- mm_req("status") |> mm_req_perform()
resp <- mm_req("status") |> mm_req_perform(max_reqs = 1)
expect_type(resp, "list")
expect_s3_class(resp[[1]], "httr2_response")
})
})

test_that("mm_req_perform() performs Inf requests by default", {
expect_equal(formals(mm_req_perform)$max_reqs, Inf)
})

with_mock_api({
test_that("mm_req_perform() returns API page_count = 21 responses", {
responses <- mm_req("timecard") |> mm_req_perform()
expect_length(responses, 21)
})
})
42 changes: 42 additions & 0 deletions tests/testthat/timecard-6fdf2d.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
structure(list(method = "GET", url = "https://api.megamation.com/uog/dl/timecard?ALLFIELDS=1",
status_code = 200L, headers = structure(list(`Cache-Control` = "REDACTED",
`Content-Type` = "application/hal+json", Server = "REDACTED",
`X-Powered-By` = "REDACTED", `Access-Control-Allow-Origin` = "REDACTED",
`WWW-Authenticate` = "REDACTED",
`X-Powered-By` = "REDACTED", `X-Robots-Tag` = "REDACTED",
Date = "Fri, 17 May 2024 18:28:10 GMT", `Content-Length` = "REDACTED",
`Set-Cookie` = "REDACTED",
`Strict-Transport-Security` = "REDACTED"), class = "httr2_headers"),
body = as.raw(c(0x5b, 0x7b, 0x22, 0x68, 0x61, 0x73, 0x5f, 0x6d, 0x6f,
0x72, 0x65, 0x22, 0x3a, 0x5b, 0x22, 0x54, 0x72, 0x75, 0x65, 0x22,
0x5d, 0x2c, 0x22, 0x63, 0x75, 0x72, 0x73, 0x6f, 0x72, 0x22, 0x3a,
0x5b, 0x22, 0x32, 0x30, 0x32, 0x34, 0x2d, 0x30, 0x35, 0x2d, 0x31,
0x37, 0x5f, 0x31, 0x35, 0x2d, 0x32, 0x37, 0x2d, 0x34, 0x38, 0x5f,
0x34, 0x32, 0x33, 0x34, 0x38, 0x2a, 0x31, 0x30, 0x30, 0x31, 0x22,
0x5d, 0x2c, 0x22, 0x6e, 0x65, 0x78, 0x74, 0x5f, 0x70, 0x61, 0x67,
0x65, 0x22, 0x3a, 0x5b, 0x22, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a,
0x2f, 0x2f, 0x61, 0x70, 0x69, 0x2e, 0x6d, 0x65, 0x67, 0x61, 0x6d,
0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x75,
0x6f, 0x67, 0x2f, 0x64, 0x6c, 0x2f, 0x74, 0x69, 0x6d, 0x65, 0x63,
0x61, 0x72, 0x64, 0x3f, 0x41, 0x4c, 0x4c, 0x46, 0x49, 0x45, 0x4c,
0x44, 0x53, 0x3d, 0x31, 0x26, 0x43, 0x55, 0x52, 0x53, 0x4f, 0x52,
0x3d, 0x32, 0x30, 0x32, 0x34, 0x2d, 0x30, 0x35, 0x2d, 0x31, 0x37,
0x5f, 0x31, 0x35, 0x2d, 0x32, 0x37, 0x2d, 0x34, 0x38, 0x5f, 0x34,
0x32, 0x33, 0x34, 0x38, 0x25, 0x32, 0x41, 0x31, 0x30, 0x30, 0x31,
0x22, 0x5d, 0x2c, 0x22, 0x70, 0x61, 0x67, 0x65, 0x5f, 0x63, 0x6f,
0x75, 0x6e, 0x74, 0x22, 0x3a, 0x5b, 0x22, 0x31, 0x5f, 0x32, 0x31,
0x22, 0x5d, 0x2c, 0x22, 0x5f, 0x6c, 0x69, 0x6e, 0x6b, 0x73, 0x22,
0x3a, 0x7b, 0x22, 0x73, 0x65, 0x6c, 0x66, 0x22, 0x3a, 0x7b, 0x22,
0x68, 0x72, 0x65, 0x66, 0x22, 0x3a, 0x5b, 0x22, 0x68, 0x74, 0x74,
0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x61, 0x70, 0x69, 0x2e, 0x6d, 0x65,
0x67, 0x61, 0x6d, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x63, 0x6f,
0x6d, 0x2f, 0x75, 0x6f, 0x67, 0x2f, 0x64, 0x6c, 0x2f, 0x74, 0x69,
0x6d, 0x65, 0x63, 0x61, 0x72, 0x64, 0x3f, 0x41, 0x4c, 0x4c, 0x46,
0x49, 0x45, 0x4c, 0x44, 0x53, 0x3d, 0x31, 0x22, 0x5d, 0x7d, 0x2c,
0x22, 0x61, 0x70, 0x69, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x50, 0x6f,
0x69, 0x6e, 0x74, 0x22, 0x3a, 0x7b, 0x22, 0x68, 0x72, 0x65, 0x66,
0x22, 0x3a, 0x5b, 0x22, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f,
0x2f, 0x61, 0x70, 0x69, 0x2e, 0x6d, 0x65, 0x67, 0x61, 0x6d, 0x61,
0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x75, 0x6f,
0x67, 0x2f, 0x64, 0x6c, 0x22, 0x5d, 0x7d, 0x7d, 0x7d, 0x5d)), request = "REDACTED",
cache = new.env(parent = emptyenv())), class = "httr2_response")
42 changes: 42 additions & 0 deletions tests/testthat/timecard.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
structure(list(method = "GET", url = "https://api.megamation.com/uog/dl/timecard?ALLFIELDS=1",
status_code = 200L, headers = structure(list(`Cache-Control` = "REDACTED",
`Content-Type` = "application/hal+json", Server = "REDACTED",
`X-Powered-By` = "REDACTED", `Access-Control-Allow-Origin` = "REDACTED",
`WWW-Authenticate` = "REDACTED",
`X-Powered-By` = "REDACTED", `X-Robots-Tag` = "REDACTED",
Date = "Fri, 17 May 2024 18:28:10 GMT", `Content-Length` = "REDACTED",
`Set-Cookie` = "REDACTED",
`Strict-Transport-Security` = "REDACTED"), class = "httr2_headers"),
body = as.raw(c(0x5b, 0x7b, 0x22, 0x68, 0x61, 0x73, 0x5f, 0x6d, 0x6f,
0x72, 0x65, 0x22, 0x3a, 0x5b, 0x22, 0x54, 0x72, 0x75, 0x65, 0x22,
0x5d, 0x2c, 0x22, 0x63, 0x75, 0x72, 0x73, 0x6f, 0x72, 0x22, 0x3a,
0x5b, 0x22, 0x32, 0x30, 0x32, 0x34, 0x2d, 0x30, 0x35, 0x2d, 0x31,
0x37, 0x5f, 0x31, 0x35, 0x2d, 0x32, 0x37, 0x2d, 0x34, 0x38, 0x5f,
0x34, 0x32, 0x33, 0x34, 0x38, 0x2a, 0x31, 0x30, 0x30, 0x31, 0x22,
0x5d, 0x2c, 0x22, 0x6e, 0x65, 0x78, 0x74, 0x5f, 0x70, 0x61, 0x67,
0x65, 0x22, 0x3a, 0x5b, 0x22, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a,
0x2f, 0x2f, 0x61, 0x70, 0x69, 0x2e, 0x6d, 0x65, 0x67, 0x61, 0x6d,
0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x75,
0x6f, 0x67, 0x2f, 0x64, 0x6c, 0x2f, 0x74, 0x69, 0x6d, 0x65, 0x63,
0x61, 0x72, 0x64, 0x3f, 0x41, 0x4c, 0x4c, 0x46, 0x49, 0x45, 0x4c,
0x44, 0x53, 0x3d, 0x31, 0x26, 0x43, 0x55, 0x52, 0x53, 0x4f, 0x52,
0x3d, 0x32, 0x30, 0x32, 0x34, 0x2d, 0x30, 0x35, 0x2d, 0x31, 0x37,
0x5f, 0x31, 0x35, 0x2d, 0x32, 0x37, 0x2d, 0x34, 0x38, 0x5f, 0x34,
0x32, 0x33, 0x34, 0x38, 0x25, 0x32, 0x41, 0x31, 0x30, 0x30, 0x31,
0x22, 0x5d, 0x2c, 0x22, 0x70, 0x61, 0x67, 0x65, 0x5f, 0x63, 0x6f,
0x75, 0x6e, 0x74, 0x22, 0x3a, 0x5b, 0x22, 0x31, 0x5f, 0x32, 0x31,
0x22, 0x5d, 0x2c, 0x22, 0x5f, 0x6c, 0x69, 0x6e, 0x6b, 0x73, 0x22,
0x3a, 0x7b, 0x22, 0x73, 0x65, 0x6c, 0x66, 0x22, 0x3a, 0x7b, 0x22,
0x68, 0x72, 0x65, 0x66, 0x22, 0x3a, 0x5b, 0x22, 0x68, 0x74, 0x74,
0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x61, 0x70, 0x69, 0x2e, 0x6d, 0x65,
0x67, 0x61, 0x6d, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x63, 0x6f,
0x6d, 0x2f, 0x75, 0x6f, 0x67, 0x2f, 0x64, 0x6c, 0x2f, 0x74, 0x69,
0x6d, 0x65, 0x63, 0x61, 0x72, 0x64, 0x3f, 0x41, 0x4c, 0x4c, 0x46,
0x49, 0x45, 0x4c, 0x44, 0x53, 0x3d, 0x31, 0x22, 0x5d, 0x7d, 0x2c,
0x22, 0x61, 0x70, 0x69, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x50, 0x6f,
0x69, 0x6e, 0x74, 0x22, 0x3a, 0x7b, 0x22, 0x68, 0x72, 0x65, 0x66,
0x22, 0x3a, 0x5b, 0x22, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f,
0x2f, 0x61, 0x70, 0x69, 0x2e, 0x6d, 0x65, 0x67, 0x61, 0x6d, 0x61,
0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x75, 0x6f,
0x67, 0x2f, 0x64, 0x6c, 0x22, 0x5d, 0x7d, 0x7d, 0x7d, 0x5d)), request = "REDACTED",
cache = new.env(parent = emptyenv())), class = "httr2_response")

0 comments on commit d4601c6

Please sign in to comment.