Skip to content

Commit

Permalink
Put TABLEYEAR logic in mm_get()
Browse files Browse the repository at this point in the history
  • Loading branch information
asadow committed Jan 29, 2024
1 parent 19cdeba commit 3410327
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
6 changes: 3 additions & 3 deletions R/authorize.R
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,12 +15,12 @@
#' @export
#' @examples
#' \dontrun{
#' mm_authorize(
#' mm_auth(
#' key = "<YOUR-MEGAMATION_KEY>",
#' url = "<YOUR-MEGAMATION_URL>"
#' )
#' }
mm_authorize <- function(key, url) {
mm_auth <- function(key, url) {
check_string(key)
check_string(url)
url <- check_url(url)
Expand Down
8 changes: 8 additions & 0 deletions R/get.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 0 additions & 2 deletions R/utils-params.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<your-key>', url = 'your-base-url')} to
"i" = "Run {.fun mm_auth(key = '<your-key>', url = 'your-base-url')} to
install your credentials."
))
}
6 changes: 5 additions & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions man/mm_authorize.Rd

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

10 changes: 5 additions & 5 deletions tests/testthat/test-authorize.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 3410327

Please sign in to comment.