-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
47 changed files
with
538 additions
and
547 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ | |
^cran-comments\.md$ | ||
^CODE_OF_CONDUCT\.md$ | ||
^README$ | ||
^CRAN-SUBMISSION$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Version: 0.1.0 | ||
Date: 2024-02-26 18:33:26 UTC | ||
SHA: 0eba30fefa36068f06aed449fbd7841ab25054c7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,6 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(mm_auth) | ||
export(mm_define_gets) | ||
export(mm_get) | ||
export(mm_get_col_info) | ||
export(mm_get_criteria) | ||
export(mm_get_labels) | ||
export(mm_get_schema) | ||
export(mm_authorize) | ||
export(mm_data) | ||
export(mm_names) | ||
importFrom(lifecycle,deprecated) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#' Download data | ||
#' | ||
#' @description `mm_data()` downloads Megamation tables into R. It automatically | ||
#' | ||
#' 1. Creates and performs the necessary API GET request(s) | ||
#' 2. Extracts and combines the data from the API response(s) | ||
#' 3. Informs when no data is returned or when the API errors | ||
#' | ||
#' @inheritParams mm_req | ||
#' @param allfields If `TRUE`, return all fields currently available for the | ||
#' endpoint. | ||
#' @returns A data frame of class [`tbl_df`][tibble::tbl_df-class] containing | ||
#' the requested data. | ||
#' @export | ||
#' @examplesIf megamation:::has_creds() | ||
#' date <- seq.Date(as.Date("2023-01-01"), as.Date("2023-01-03"), by = "day") | ||
#' mm_data("timecard", date = date) | ||
mm_data <- function(endpoint, ..., allfields = TRUE) { | ||
gets <- mm_list_get_reqs(endpoint, ..., allfields = allfields) | ||
|
||
cli::cli_alert_info("Requesting...") | ||
|
||
responses <- purrr::map( | ||
gets |> | ||
cli::cli_progress_along(format = "Fulfilling request {cli::pb_current}"), | ||
\(x) mm_req_perform(gets[[x]]) | ||
) |> | ||
purrr::list_flatten() | ||
|
||
errors <- responses |> httr2::resps_failures() | ||
errors_occured <- !rlang::is_empty(errors) | ||
|
||
successes <- responses |> httr2::resps_successes() | ||
pages <- successes |> purrr::map(mm_resp_extract) | ||
|
||
no_data_i <- pages |> purrr::map_lgl(is.null) |> which() | ||
|
||
if (errors_occured || any(no_data_i)) { | ||
cli::cli_alert_info( | ||
c("NB: {length(errors) + length(no_data_i)}/{length(responses)}", | ||
" requests returned errors or no data.") | ||
) | ||
for (error in errors) { | ||
url <- error$request$url | ||
msg <- error$message | ||
cli::cli_h1("Errored") | ||
glue::glue("{msg} \n {cli::style_bold('GET')} {url}") |> | ||
cli::cli_alert_danger() | ||
} | ||
|
||
for (i in no_data_i) { | ||
url <- successes[[i]]$request$url | ||
cli::cli_h1("No Data Returned") | ||
glue::glue("\n {cli::style_bold('GET')} {url}") |> | ||
cli::cli_alert_danger() | ||
} | ||
} | ||
|
||
data <- pages |> mm_pagebind() | ||
remove_api_urls(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#' List GET requests | ||
#' | ||
#' @description `mm_list_get_reqs()` returns the necessary GET request(s) for | ||
#' fetching the supplied endpoint and parameters. Multiple GET requests are | ||
#' sometimes needed for two reasons. | ||
#' | ||
#' One, Megamation does not support common ways of separating multiple values | ||
#' for a field inside an HTTP URL. These ways include: | ||
#' | ||
#' * using a `,`, e.g. `?x=1,2` | ||
#' * using a `|`, e.g. `?x=1|2` | ||
#' * turning each element into its own parameter, e.g. `?x=1&x=2` | ||
#' | ||
#' For Megamation, the first two ways will result in a 404 error. The last way | ||
#' fetches data where x is both 1 and 2 (not 1 or 2). Hence | ||
#' `mm_list_get_reqs()` defines separate GET requests for each value given to | ||
#' a parameter. | ||
#' | ||
#' | ||
#' Two, the timecard endpoint is unique in that separate GET requests must be | ||
#' made for separate years of data (e.g. 2022 vs 2023). This is also | ||
#' automatically handled by `mm_list_get_reqs()`. | ||
#' | ||
#' @inheritParams mm_data | ||
#' @returns A list of GET requests of class `httr2_request`. | ||
#' @noRd | ||
#' @examplesIf megamation:::has_creds() | ||
#' date <- seq.Date(as.Date("2022-01-01"), as.Date("2023-01-31"), by = "day") | ||
#' megamation:::mm_list_get_reqs("timecard", date = date) | ||
mm_list_get_reqs <- function(endpoint, ..., allfields = TRUE) { | ||
params <- format_params(endpoint, ...) | ||
args <- tidyr::crossing(!!!params) |> | ||
## I() does not work here but we can hope | ||
dplyr::mutate( | ||
endpoint = endpoint, | ||
dplyr::across(dplyr::everything(), I) | ||
) | ||
|
||
if (endpoint == "timecard" && "DATE" %in% names(params)) { | ||
args <- add_tableyear(args) | ||
} | ||
|
||
requests <- args |> | ||
purrr::pmap(mm_req) |> | ||
purrr::list_flatten() | ||
|
||
if (!allfields) { | ||
return(requests) | ||
} | ||
|
||
requests |> purrr::map(\(req) httr2::req_url_query(req, ALLFIELDS = 1)) | ||
|
||
} | ||
|
||
# Add the TABLEYEAR parameter which is required for past years of the "timecard" | ||
# endpoint | ||
add_tableyear <- function(df) { | ||
DATE <- NULL | ||
df |> | ||
dplyr::mutate( | ||
TABLEYEAR = DATE |> | ||
stringi::stri_extract(regex = "\\d{4}") |> | ||
## Current year does not accept TABLEYEAR parameter | ||
dplyr::na_if(Sys.Date() |> lubridate::year() |> as.character()) | ||
) | ||
} |
Oops, something went wrong.