diff --git a/NAMESPACE b/NAMESPACE index 7ee1853..22897a1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,6 @@ # Generated by roxygen2: do not edit by hand -export(mm_authorize) +export(mm_auth) export(mm_get) export(mm_get_col_info) export(mm_get_criteria) diff --git a/R/authorize.R b/R/authorize.R index a5c9e85..7859f45 100644 --- a/R/authorize.R +++ b/R/authorize.R @@ -1,7 +1,7 @@ #' Install Megamation credentials #' #' @description -#' `mm_authorize()` installs your credentials so that `megamation` functions +#' `mm_auth()` installs your credentials so that `megamation` functions #' that need them, like [mm_get()], can use them automatically. #' #' Specifically, it places your Megamation API key and base URL @@ -15,12 +15,12 @@ #' @export #' @examples #' \dontrun{ -#' mm_authorize( +#' mm_auth( #' key = "", #' url = "" #' ) #' } -mm_authorize <- function(key, url) { +mm_auth <- function(key, url) { check_string(key) check_string(url) url <- check_url(url) diff --git a/R/get.R b/R/get.R index 6d0708b..78688cb 100644 --- a/R/get.R +++ b/R/get.R @@ -102,6 +102,14 @@ mm_get <- function(endpoint, ..., .paginate = TRUE, .perform = TRUE) { mm_req(endpoint) |> mm_req_params(!!!params) + ## TABLEYEAR parameter required for previous years of timecard + if (endpoint == "timecard" && "date" %in% tolower(names(params))) { + tableyear <- lubridate::year(params$date) |> unique() + tableyear <- tableyear[tableyear != lubridate::year(Sys.Date())] + request <- request |> + httr2::req_url_query(TABLEYEAR = tableyear) + } + if (!.perform) { return(request) } diff --git a/R/utils-params.R b/R/utils-params.R index 2935a3c..fb44e68 100644 --- a/R/utils-params.R +++ b/R/utils-params.R @@ -24,8 +24,6 @@ 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) } diff --git a/R/utils.R b/R/utils.R index ca1fb33..732a482 100644 --- a/R/utils.R +++ b/R/utils.R @@ -10,7 +10,7 @@ remove_api_urls <- function(.data) { stop_missing_cred <- function() { cli::cli_abort(c( "Missing credentials.", - "i" = "Run {.fun mm_authorize(key = '', url = 'your-base-url')} to + "i" = "Run {.fun mm_auth(key = '', url = 'your-base-url')} to install your credentials." )) } diff --git a/_pkgdown.yml b/_pkgdown.yml index 349b6ad..458ddd3 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -2,6 +2,10 @@ url: https://asadow.github.io/megamation/ template: bootstrap: 5 reference: + - title: "Authorize access to Megamation" + desc: Create environment variables for Megamation credentials + contents: + - mm_auth - title: "Get your data" desc: > Retrieve data from/about your Megamation tables so you can act on them @@ -13,7 +17,7 @@ reference: - mm_get_labels - title: "Programming around the Megamation API" desc: > - Low-level functions used internally and made available for programming + Low-level functions used internally contents: - mm_req - mm_error_body diff --git a/man/mm_authorize.Rd b/man/mm_authorize.Rd index f3632d1..ac68776 100644 --- a/man/mm_authorize.Rd +++ b/man/mm_authorize.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/authorize.R -\name{mm_authorize} -\alias{mm_authorize} +\name{mm_auth} +\alias{mm_auth} \title{Install Megamation credentials} \usage{ -mm_authorize(key, url) +mm_auth(key, url) } \arguments{ \item{key}{Single string of the Megamation API key.} @@ -15,7 +15,7 @@ mm_authorize(key, url) Writes \code{MEGAMATION_KEY} and \code{MEGAMATION_URL} environment variables. } \description{ -\code{mm_authorize()} installs your credentials so that \code{megamation} functions +\code{mm_auth()} installs your credentials so that \code{megamation} functions that need them, like \code{\link[=mm_get]{mm_get()}}, can use them automatically. Specifically, it places your Megamation API key and base URL @@ -25,7 +25,7 @@ variables. } \examples{ \dontrun{ -mm_authorize( +mm_auth( key = "", url = "" ) diff --git a/tests/testthat/test-authorize.R b/tests/testthat/test-authorize.R index 9ad4afb..7a97ec0 100644 --- a/tests/testthat/test-authorize.R +++ b/tests/testthat/test-authorize.R @@ -14,21 +14,21 @@ test_that("testing key", { ) }) -test_that("mm_authorize() gives bad url error", { +test_that("mm_auth() gives bad url error", { expect_error( - mm_authorize(key = "1", url = "a"), + mm_auth(key = "1", url = "a"), "`url` must be of the form" ) }) -test_that("mm_authorize() sets credentials", { +test_that("mm_auth() sets credentials", { skip_on_cran() key_original <- mm_key() url_original <- mm_url() withr::defer({ - mm_authorize(key = key_original, url = url_original) + mm_auth(key = key_original, url = url_original) }) - mm_authorize(key = "1", url = "https://api.megamation.com/uw/joe/") + mm_auth(key = "1", url = "https://api.megamation.com/uw/joe/") expect_equal( mm_url(), "https://api.megamation.com/uw/joe"