11# ' Core request function
22# '
33# ' @details
4+ # ' API authentication is handled automatically. See the
5+ # nolint start: line_length_linter
6+ # ' [Authentication article](https://scotgovanalysis.github.io/objr/articles/authentication.html)
7+ # nolint end
8+ # ' for more information.
9+ # '
410# ' More details on endpoints are available in the
511# nolint start: line_length_linter
612# ' \href{https://secure.objectiveconnect.co.uk/publicapi/1/swagger-ui/index.html?configUrl=/publicapi/1/v3/api-docs/swagger-config#/}{API documentation}.
713# nolint end
814# '
915# ' @param endpoint The endpoint to append to the API server address.
1016# ' @param url_path A list of values to be added to the request URL path.
11- # ' Values will be separated with `/`.
17+ # ' Values will be separated with `/`.
1218# ' @param url_query A list of named values to define query parameters.
1319# ' @param method HTTP method to use; e.g. `"GET"`, `"POST"`, `"PUT"`.
14- # ' Defaults to `"GET"`.
20+ # ' Defaults to `"GET"`.
1521# ' @param body A list of named values to be passed to the request body.
1622# ' @param path Optional file path to save body of request (mainly used when
17- # ' downloading files).
23+ # ' downloading files).
1824# ' @param accept Accept header. Defaults to `"application/json"`.
1925# ' @param content_type Content-Type header. Defaults to `"application/json"`.
2026# ' @param use_proxy Logical to indicate whether to use proxy.
@@ -121,11 +127,24 @@ objr_auth <- function(req,
121127 call = error_call )
122128 }
123129
124- if (exists(" token" , where = parent.frame())) {
130+ token_exists <- exists(" token" , where = globalenv())
131+
132+ if (token_exists ) {
133+ if (get(" token" , pos = globalenv())$ expiry < Sys.time()) {
134+ remove(" token" , pos = globalenv())
135+ token_exists <- FALSE
136+ cli :: cli_inform(c(
137+ " i" = " Existing authentication `token` has expired." ,
138+ " i" = " Re-trying authentication using username/password..."
139+ ))
140+ }
141+ }
142+
143+ if (token_exists ) {
125144
126145 httr2 :: req_headers(
127146 req ,
128- Authorization = get(" token" , pos = parent.frame ())
147+ Authorization = get(" token" , pos = globalenv ())$ value
129148 )
130149
131150 } else {
@@ -147,7 +166,7 @@ objr_auth <- function(req,
147166# ' header.
148167# ' @param store_env The environment to bind the token to.
149168# '
150- # ' @return Returns the token invisibly. This function is primarily used
169+ # ' @return API response ( invisibly) . This function is primarily used
151170# ' for its side effect - an environment variable is created called "token".
152171# '
153172# ' @noRd
@@ -169,11 +188,12 @@ store_token <- function(response,
169188 call = error_call )
170189 }
171190
172- token <- httr2 :: resp_header(response , " Authorization" )
191+ token <- list (
192+ value = httr2 :: resp_header(response , " Authorization" ),
193+ expiry = Sys.time() + (20 * 60 )
194+ )
173195
174- if (! exists(" token" , where = store_env )) {
175- rlang :: env_poke(env = store_env , nm = " token" , value = token )
176- }
196+ rlang :: env_poke(env = store_env , nm = " token" , value = token )
177197
178198 invisible (response )
179199
0 commit comments