Skip to content

Commit a1ce77a

Browse files
Cache timezone response and remove other caller of unversioned API (#405)
1 parent bc697d1 commit a1ce77a

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

R/connect.R

+14-1
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,8 @@ Connect <- R6::R6Class(
955955
# end --------------------------------------------------------
956956
),
957957
private = list(
958-
.version = NULL
958+
.version = NULL,
959+
.timezones = NULL
959960
),
960961
active = list(
961962
#' @field version The server version.
@@ -964,6 +965,18 @@ Connect <- R6::R6Class(
964965
private$.version <- safe_server_version(self)
965966
}
966967
private$.version
968+
},
969+
#' @field timezones The server timezones.
970+
timezones = function() {
971+
if (is.null(private$.timezones)) {
972+
private$.timezones <- tryCatch(
973+
self$GET(v1_url("timezones")),
974+
error = function(e) {
975+
self$GET(unversioned_url("timezones"))
976+
}
977+
)
978+
}
979+
private$.timezones
967980
}
968981
)
969982
)

R/schedule.R

+2-10
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ VariantSchedule <- R6::R6Class(
129129
"year" = glue::glue("Every {schdata$N} year{plural}"),
130130
"Unknown schedule"
131131
)
132-
# TODO: is fetching data during a PRINT a bit overkill?
133132
tz_offset <- .get_offset(self$get_connect(), rawdata$timezone)
134133
c(
135134
desc,
@@ -143,9 +142,7 @@ VariantSchedule <- R6::R6Class(
143142
)
144143

145144
.get_offset <- function(connect, timezone) {
146-
# TODO: some type of cache to reduce churn here?
147-
tz <- connect$GET(unversioned_url("timezones"))
148-
res <- purrr::keep(tz, ~ .x[["timezone"]] == timezone)
145+
res <- purrr::keep(connect$timezones, ~ .x[["timezone"]] == timezone)
149146
if (length(res) != 1) {
150147
stop(glue::glue("ERROR: timezone '{timezone}' not found"))
151148
}
@@ -528,12 +525,7 @@ schedule_describe <- function(.schedule) {
528525
#' @family schedule functions
529526
#' @export
530527
get_timezones <- function(connect) {
531-
raw_tz <- tryCatch(
532-
connect$GET(v1_url("timezones")),
533-
error = function(e) {
534-
connect$GET(unversioned_url("timezones"))
535-
}
536-
)
528+
raw_tz <- connect$timezones
537529

538530
tz_values <- purrr::map_chr(raw_tz, ~ .x[["timezone"]])
539531
tz_display <- purrr::map_chr(

man/PositConnect.Rd

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-parse.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ test_that("parse_connect_rfc3339() handles fractional seconds", {
131131
"2024-12-06T19:09:29.948070345+0000"
132132
),
133133
format = "%Y-%m-%dT%H:%M:%OS%z",
134-
tz = "UTC"
134+
tz = Sys.timezone()
135135
))
136136

137137
x <- c("2024-12-06T19:09:29.948016766Z", "2024-12-06T19:09:29.948070345Z")

0 commit comments

Comments
 (0)