Skip to content

Commit

Permalink
Expose api keys if desired
Browse files Browse the repository at this point in the history
  • Loading branch information
dieghernan committed Jun 21, 2024
1 parent 296d8a1 commit eb29957
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export(aemet_monthly_period)
export(aemet_monthly_period_all)
export(aemet_normal_clim)
export(aemet_normal_clim_all)
export(aemet_show_api_key)
export(aemet_stations)
export(climaemet_news)
export(climatestripes_station)
Expand Down
22 changes: 19 additions & 3 deletions R/aemet_api_key.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,30 @@ aemet_api_key <- function(apikey, overwrite = FALSE, install = FALSE) {
#'
#' @description
#' The function would detect if an API Key is available on this session:
#' * If an API Key is already set as an environment variable it would be
#' - If an API Key is already set as an environment variable it would be
#' preserved
#' * If no environment variable has been set and you have stored permanently
#' - If no environment variable has been set and you have stored permanently
#' an API Key using [aemet_api_key()], the latter would be loaded.
#'
#' @return `TRUE` or `FALSE`
#' @return
#' `TRUE` or `FALSE`. `aemet_show_api_key()` would display your stored API keys.
#'
#' @family aemet_auth
#'
#' @export
#'
#' @param ... Ignored
#'
#' @rdname aemet_detect_api_key
#'
#' @examples
#'
#' aemet_detect_api_key()
#'
#' # CAUTION: This may reveal API Keys
#' if (FALSE) {
#' aemet_show_api_key()
#' }
aemet_detect_api_key <- function(...) {
allvar <- Sys.getenv()

Expand Down Expand Up @@ -166,6 +173,15 @@ aemet_detect_api_key <- function(...) {
}
}

#' @export
#' @rdname aemet_detect_api_key
aemet_show_api_key <- function(...) {
# Expose internal function
# nocov start
aemet_hlp_get_allkeys(...)
# nocov end
}

aemet_hlp_get_allkeys <- function(...) {
allkeys <- Sys.getenv()[grepl("^AEMET_API", names(Sys.getenv()))]
allkeys <- unname(as.character(allkeys))
Expand Down
4 changes: 4 additions & 0 deletions R/aemet_daily.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ aemet_daily_clim <- function(station = "all", start = Sys.Date() - 7,
if (verbose) progress <- FALSE
if (!cli::is_dynamic_tty()) progress <- FALSE

# nolint start
# nocov start
if (progress) {
opts <- options()
Expand All @@ -147,6 +148,7 @@ aemet_daily_clim <- function(station = "all", start = Sys.Date() - 7,
}

# nocov end
# nolint end

### API Loop ----
for (id in ln) {
Expand All @@ -166,6 +168,7 @@ aemet_daily_clim <- function(station = "all", start = Sys.Date() - 7,
final_result <- c(final_result, list(df))
}

# nolint start
# nocov start
if (progress) {
cli::cli_progress_done()
Expand All @@ -177,6 +180,7 @@ aemet_daily_clim <- function(station = "all", start = Sys.Date() - 7,
}

# nocov end
# nolint end

# Final tweaks
final_result <- dplyr::bind_rows(final_result)
Expand Down
6 changes: 5 additions & 1 deletion R/aemet_extremes.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ aemet_extremes_clim <- function(station = NULL, parameter = "T",
if (verbose) progress <- FALSE
if (!cli::is_dynamic_tty()) progress <- FALSE

# nolint start
# nocov start
if (progress) {
opts <- options()
Expand All @@ -99,6 +100,8 @@ aemet_extremes_clim <- function(station = NULL, parameter = "T",
}

# nocov end
# nolint end

for (id in station) {
apidest <- paste0(
"/api/valores/climatologicos",
Expand All @@ -113,7 +116,7 @@ aemet_extremes_clim <- function(station = NULL, parameter = "T",
final_result <- c(final_result, list(df))
}


# nolint start
# nocov start
if (progress) {
cli::cli_progress_done()
Expand All @@ -124,6 +127,7 @@ aemet_extremes_clim <- function(station = NULL, parameter = "T",
)
}
# nocov end
# nolint end

bindtry <- try(dplyr::bind_rows(final_result), silent = TRUE)
if (inherits(bindtry, "try-error")) {
Expand Down
10 changes: 9 additions & 1 deletion man/aemet_detect_api_key.Rd

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

12 changes: 3 additions & 9 deletions tests/testthat/test-aemet_daily.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test_that("aemet_daily for all", {
expect_message(alll <- aemet_daily_clim(verbose = TRUE))
expect_s3_class(alll, "tbl_df")
Sys.sleep(0.5)
expect_silent(alll2 <- aemet_daily_clim())
alll2 <- aemet_daily_clim()

expect_identical(alll, alll2)
expect_gt(length(unique(alll$indicativo)), 100)
Expand All @@ -46,10 +46,7 @@ test_that("aemet_daily for all", {

# Single day
Sys.sleep(0.5)
expect_silent(alll3 <- aemet_daily_clim(
start = Sys.Date() - 20,
end = Sys.Date() - 20
))
alll3 <- aemet_daily_clim(start = Sys.Date() - 20, end = Sys.Date() - 20)


expect_s3_class(alll3, "tbl_df")
Expand Down Expand Up @@ -89,10 +86,7 @@ test_that("aemet_daily iterations", {

# sf
Sys.sleep(0.5)
expect_silent(alll_sf <- aemet_daily_clim(
st_demo,
return_sf = TRUE
))
alll_sf <- aemet_daily_clim(st_demo, return_sf = TRUE)

expect_identical(unique(alll_sf$indicativo), st_demo)
expect_s3_class(alll_sf, "sf")
Expand Down

0 comments on commit eb29957

Please sign in to comment.