/
aemet_api_key.R
173 lines (155 loc) · 4.78 KB
/
aemet_api_key.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#' Install an AEMET API Key
#'
#' @family aemet_auth
#'
#' @description
#' This function will store your AEMET API key on your local machine so it can
#' be called securely without being stored in your code. After you have
#' installed your key, it can be called any time by typing
#' `Sys.getenv("AEMET_API_KEY")` and can be
#' used in package functions by simply typing `AEMET_API_KEY`.
#'
#' Alternatively, you can install the API Key manually:
#' * Run `Sys.setenv(AEMET_API_KEY = "Your_Key")`. You would need to run this
#' command on each session (Similar to `install = FALSE`).
#' * Write this line on your .Renviron file: `AEMET_API_KEY = "Your_Key"` (
#' same behavior than `install = TRUE`). This would store your API key
#' permanently.
#'
#' @return None
#'
#' @param apikey The API key provided to you from the AEMET formatted in quotes.
#' A key can be acquired at
#' <https://opendata.aemet.es/centrodedescargas/inicio>.
#' @param install if `TRUE`, will install the key in your local machine for
#' use in future sessions. Defaults to `FALSE.`
#' @param overwrite If this is set to `TRUE`, it will overwrite an existing
#' AEMET_API_KEY that you already have in local machine.
#'
#' @details
#' You can pass several `apikey` values as a vector `c(api1, api2)`, in this
#' case several `AEMET_API_KEY` values would be generated. In each subsequent
#' api call \CRANpkg{climaemet} would randomly choose one of the provided
#' API keys.
#'
#' This is useful when performing batch queries to avoid API throttling.
#'
#'
#' @note
#' To locate your API Key on your local machine, run
#' `rappdirs::user_cache_dir("climaemet", "R")`.
#'
#' @examples
#' # Don't run these examples!
#'
#' if (FALSE) {
#' aemet_api_key("111111abc", install = TRUE)
#'
#' # You can check it with:
#' Sys.getenv("AEMET_API_KEY")
#' }
#'
#' if (FALSE) {
#' # If you need to overwrite an existing key:
#' aemet_api_key("222222abc", overwrite = TRUE, install = TRUE)
#'
#' # You can check it with:
#' Sys.getenv("AEMET_API_KEY")
#' }
#' @export
aemet_api_key <- function(apikey, overwrite = FALSE, install = FALSE) {
# Validate
stopifnot(is.character(apikey), is.logical(overwrite), is.logical(install))
if (install) {
cachedir <- rappdirs::user_cache_dir("climaemet", "R")
# Create cache dir if not presente
if (!dir.exists(cachedir)) {
dir.create(cachedir, recursive = TRUE)
}
api_file <- file.path(cachedir, "aemet_api_key")
if (!file.exists(api_file) || overwrite == TRUE) {
# Create file if it doesn't exist
writeLines(apikey, con = api_file)
} else {
stop(
"An AEMET_API_KEY already exists. You can overwrite it with the ",
"argument overwrite=TRUE",
call. = FALSE
)
}
} else {
message(
"To install your API key for use in future sessions, run this ",
"function with `install = TRUE`."
)
}
# Name and assign
nms <- seq_len(length(apikey))
nms2 <- vapply(nms, function(x) {
if (x == 1) {
return("AEMET_API_KEY")
}
sprintf("AEMET_API_KEY%02d", x - 1)
}, FUN.VALUE = character(1))
names(apikey) <- nms2
do.call(Sys.setenv, as.list(apikey))
return(invisible())
}
#' Check if an AEMET API Key is present for the current session
#'
#' @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
#' preserved
#' * 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`
#'
#' @family aemet_auth
#'
#' @export
#'
#' @param ... Ignored
#'
#'
#' @examples
#'
#' aemet_detect_api_key()
aemet_detect_api_key <- function(...) {
allvar <- Sys.getenv()
if (!any(grepl("^AEMET_API", names(allvar)))) {
# Not set - tries to retrieve from cache
cachedir <- rappdirs::user_cache_dir("climaemet", "R")
api_file <- file.path(cachedir, "aemet_api_key")
if (file.exists(api_file)) {
cached_apikey <- readLines(api_file)
# Case on empty cached apikey
if (
any(is.null(cached_apikey), is.na(cached_apikey))
) {
return(FALSE)
}
# Name and assign
nms <- seq_len(length(cached_apikey))
nms2 <- vapply(nms, function(x) {
if (x == 1) {
return("AEMET_API_KEY")
}
sprintf("AEMET_API_KEY%02d", x - 1)
}, FUN.VALUE = character(1))
names(cached_apikey) <- nms2
do.call(Sys.setenv, as.list(cached_apikey))
return(TRUE)
} else {
return(FALSE)
}
} else {
return(TRUE)
}
}
aemet_hlp_get_allkeys <- function(...) {
allkeys <- Sys.getenv()[grepl("^AEMET_API", names(Sys.getenv()))]
allkeys <- unname(as.character(allkeys))
allkeys[nchar(allkeys) > 0]
}