Skip to content

Commit

Permalink
Add TABLEYEAR
Browse files Browse the repository at this point in the history
NB Modifiers on params ot DATE do not work due to .multi bug when using I()
  • Loading branch information
asadow committed Jan 29, 2024
1 parent b60c88a commit 19cdeba
Show file tree
Hide file tree
Showing 19 changed files with 58 additions and 49 deletions.
25 changes: 14 additions & 11 deletions R/get.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ mm_get_col_info <- function(endpoint) {
#' containing the endpoint's appendix.
#' @keywords internal
mm_get_appendix <- function(endpoint, appendix) {
mm_request(endpoint) |>
mm_req(endpoint) |>
mm_req_append(appendix) |>
httr2::req_perform() |>
mm_resp_extract() |>
Expand Down Expand Up @@ -77,8 +77,9 @@ mm_get_labels <- function(endpoint) {
#' Where applicable, pagination is automatically applied to the request
#' and returned pages are automatically combined.
#' @inheritParams mm_req_params
#' @inheritParams mm_request
#' @inheritParams mm_req
#' @param .paginate If `TRUE`, paginate the request.
#' @param .perform If `TRUE`, perform the request. Otherwise only show.
#' @returns A data frame of class [`tbl_df`][tibble::tbl_df-class]
#' containing the requested data.
#' @export
Expand All @@ -92,18 +93,20 @@ mm_get_labels <- function(endpoint) {
#' by = "day"
#' )
#'
#' # Then prefix the trade values with the "containing" modifier "[]"
#' # since trade is a list column in the work order table
#' admin_and_it <- c("[]ADMIN", "[]IT")
#'
#' mm_get("workorder", date = jan_2023, trade = admin_and_it)
mm_get <- function(endpoint, ..., .paginate = TRUE) {
#' mm_get("timecard", date = jan_2023)
mm_get <- function(endpoint, ..., .paginate = TRUE, .perform = TRUE) {
check_bool(.paginate)
params <- rlang::list2(...)

resp <- mm_request(endpoint) |>
mm_req_params(!!!params) |>
mm_req_perform()
request <-
mm_req(endpoint) |>
mm_req_params(!!!params)

if (!.perform) {
return(request)
}

resp <- request |> mm_req_perform()

page_1 <- resp[[1]] |> mm_resp_extract()

Expand Down
6 changes: 6 additions & 0 deletions R/modify.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,11 @@ mm_req_params <- function(req, ..., allfields = TRUE) {
check_bool(allfields)
params <- format_params(...)
if (allfields) params <- c(params, "ALLFIELDS" = 1)

# Waiting for httr2 bug to be fixed ---------------------------------------
## https://github.com/r-lib/httr2/issues/404
## Can then remove I() from utils-params.R format_date()
## Can also remove line below and place in format_params()
# params <- params |> purrr::map(I)
httr2::req_url_query(req, !!!params, .multi = "comma")
}
2 changes: 1 addition & 1 deletion R/paginate.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' `mm_next_req()` returns the next request based on the `next_url` from
#' the parsed response.
#' @param resp An API response.
#' @param req An API request generated by [mm_request()].
#' @param req An API request generated by [mm_req()].
#' @returns An object of class `httr2_request` or `NULL`.
#' @keywords internal
mm_next_req <- function(resp, req) {
Expand Down
2 changes: 1 addition & 1 deletion R/perform.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' @description
#'
#' After creating a request with [mm_request()],
#' After creating a request with [mm_req()],
#' call `mm_req_perform()` to perform it and fetch
#' the results back to R.
#'
Expand Down
8 changes: 4 additions & 4 deletions R/request.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Create a Megamation API request
#'
#' @description
#' `mm_request()` creates a request using [httr2::request()] and
#' `mm_req()` creates a request using [httr2::request()] and
#' does the following:
#'
#' * Inserts the base URL using the environment variable `MEGAMATION_URL` from
Expand All @@ -22,9 +22,9 @@
#' @returns An object of class `httr2_request`.
#' @keywords internal
#' @examplesIf megamation:::has_creds()
#' megamation:::mm_request("timecard")
#' megamation:::mm_request("trade")
mm_request <- function(endpoint) {
#' megamation:::mm_req("timecard")
#' megamation:::mm_req("trade")
mm_req <- function(endpoint) {
check_string(endpoint)
agent <- "megamation (https://github.com/asadow/megamation)"
user <- "APIDL"
Expand Down
4 changes: 3 additions & 1 deletion R/utils-params.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ format_params <- function(...) {
date <- params$DATE
check_date(date)
params$DATE <- format_date(date)
tableyear <- lubridate::year(date) |> unique()
params$TABLEYEAR <- tableyear[tableyear != lubridate::year(Sys.Date())]
}
return(params)
}
Expand Down Expand Up @@ -53,7 +55,7 @@ format_date <- function(date) {
if (!date_is_sequence) {
format(date, "%m-%d-%Y")
} else {
date_as_between_string(.min, .max)
date_as_between_string(.min, .max) |> I()
}
}

Expand Down
2 changes: 1 addition & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ reference:
desc: >
Low-level functions used internally and made available for programming
contents:
- mm_request
- mm_req
- mm_error_body
- mm_req_append
- mm_req_params
Expand Down
10 changes: 4 additions & 6 deletions man/mm_get.Rd

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

2 changes: 1 addition & 1 deletion man/mm_next_req.Rd

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

12 changes: 6 additions & 6 deletions man/mm_request.Rd → man/mm_req.Rd

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

2 changes: 1 addition & 1 deletion man/mm_req_append.Rd

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

2 changes: 1 addition & 1 deletion man/mm_req_params.Rd

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

4 changes: 2 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.

2 changes: 1 addition & 1 deletion tests/testthat/helper.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
status_get <- function(.from) {
mm_request("status") |>
mm_req("status") |>
mm_req_append(.from) |>
httr2::req_perform()
}
2 changes: 1 addition & 1 deletion tests/testthat/test-paginate.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
with_mock_dir("status_resp", {
test_that("mm_next_req() returns NULL where one page exists", {
skip_on_cran()
req <- mm_request("status")
req <- mm_req("status")
resp <- httr2::req_perform(req)
result <- mm_next_req(resp, req)
expect_null(mm_next_req(resp, req))
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-perform.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
with_mock_dir("status_resp", {
test_that("mm_req_perform() returns list with HTTP response", {
skip_on_cran()
resp <- mm_request("status") |> mm_req_perform()
resp <- mm_req("status") |> mm_req_perform()
expect_type(resp, "list")
expect_s3_class(resp[[1]], "httr2_response")
})
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-request.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test_that("mm_request() returns class httr2_request", {
test_that("mm_req() returns class httr2_request", {
skip_on_cran()
req <- mm_request("status")
req <- mm_req("status")
expect_s3_class(req, "httr2_request")
expected_names <- c(
"url", "method", "headers", "body",
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test-utils-params.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ test_that("format_params() formats a sequence of dates", {
date_end <- as.Date("2023-10-20")
date <- seq(date_start, date_end, by = "day")

expected <- list(DATE = structure("<>09-20-2023,10-20-2023", class = "AsIs"))
expect_equal(format_params(date = date), expected)
expected <- list(DATE = structure("<>09-20-2023,10-20-2023",
class = c("AsIs", "glue", "character")))
actual <- format_params(date = date)["DATE"]
expect_equal(actual, expected)
})

test_that("format_params() formats dates not in sequence", {
date <- as.Date("2023-09-20")
date <- c(date, date + 3)

expected <- list(
DATE = structure("09-20-2023", class = "AsIs"),
DATE = structure("09-23-2023", class = "AsIs")
)
expect_equal(format_params(date = date), expected)
expected <- list(DATE = structure(c("09-20-2023", "09-23-2023")))
actual <- format_params(date = date)["DATE"]
expect_equal(actual, expected)
})

test_that("format_params() does not return accidental key", {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-utils-parse.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
with_mock_dir("status_resp", {
test_that("mm_resp_parse() for data gives list with '_embedded'", {
skip_on_cran()
resp <- mm_request("status") |> httr2::req_perform()
resp <- mm_req("status") |> httr2::req_perform()
parsed <- resp |> mm_resp_parse()
expect_type(parsed, "list")
expect_contains(names(parsed), "_embedded")
Expand Down

0 comments on commit 19cdeba

Please sign in to comment.