Skip to content

Commit

Permalink
first pass at new time zone URL
Browse files Browse the repository at this point in the history
  • Loading branch information
toph-allen committed Oct 11, 2024
1 parent 6085b53 commit 8ad8eb9
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 20 deletions.
2 changes: 1 addition & 1 deletion R/connect.R
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ connect <- function(
tryCatch(
{
check_connect_license(con)
check_connect_version(using_version = safe_server_version(con))
warn_old_connect(using_version = safe_server_version(con))
},
error = function(err) {
if (.check_is_fatal) {
Expand Down
21 changes: 20 additions & 1 deletion R/schedule.R
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,26 @@ schedule_describe <- function(.schedule) {
#' @family schedule functions
#' @export
get_timezones <- function(connect) {
raw_tz <- connect$GET(unversioned_url("timezones"))
res <- NULL

# If the version is greater than or equal to the target version, or if no
# version is available, we try the new path.
if (!connect_version_lt(safe_server_version(connect), "2024.09.0")) {
res <- connect$GET(v1_url("timezones"), parser = NULL)
print(res)
if (httr::status_code(res) == 404) {
# We will try the old URL.
res <- NULL
}
}
if (is.null(res)) {
res <- connect$GET(unversioned_url("timezones"), parser = NULL)
print(res)
}
connect$raise_error(res)

raw_tz <- httr::content(res)

tz_values <- purrr::map_chr(raw_tz, ~ .x[["timezone"]])
tz_display <- purrr::map_chr(raw_tz, ~ glue::glue("{.x[['timezone']]} ({.x[['offset']]})"))

Expand Down
36 changes: 28 additions & 8 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,26 @@ safe_server_version <- function(client) {
version <- safe_server_settings(client)$version
if (is.null(version) || nchar(version) == 0) {
message("Version information is not exposed by this Posit Connect instance.")
# Return 0 so this will always show up as "too old"
version <- "0"
version <- NULL
}
version
}

error_if_less_than <- function(client, tested_version) {
server_version <- safe_server_version(client)
comp <- compare_connect_version(server_version, tested_version)
if (comp < 0) {
msg <- paste0(
"ERROR: This API requires Posit Connect version ", tested_version,
" but you are using", server_version, ". Please use a previous version",
" of the `connectapi` package, upgrade Posit Connect, or review the API ",
if (is.null(comp)) {
msg <- glue::glue(
"WARNING: This API requires Posit Connect version {tested_version} ",
"but the server version is not exposed by this Posit Connect instance. ",
"You may be experience errors when using this functionality."
)
warn_once(msg)
} else if (comp < 0) {
msg <- glue::glue(
"ERROR: This API requires Posit Connect version {tested_version} ",
"but you are using {server_version}. Please use a previous version ",
"of the `connectapi` package, upgrade Posit Connect, or review the API ",
"documentation corresponding to your version."
)
stop(msg)
Expand All @@ -156,10 +162,11 @@ error_if_less_than <- function(client, tested_version) {
}

compare_connect_version <- function(using_version, tested_version) {
if (is.null(using_version)) return(NULL)
compareVersion(simplify_version(using_version), simplify_version(tested_version))
}

check_connect_version <- function(using_version, minimum_tested_version = "1.8.8.2") {
warn_old_connect <- function(using_version, minimum_tested_version = "1.8.8.2") {
comp <- compare_connect_version(using_version, minimum_tested_version)
if (comp < 0) {
warn_once(glue::glue(
Expand All @@ -171,6 +178,19 @@ check_connect_version <- function(using_version, minimum_tested_version = "1.8.8
invisible()
}

connect_version_gte <- function(client_version, target_version) {
comp <- compare_connect_version(client_version, target_version)
if (is.null(comp)) {
return(NULL)
} else {
return(comp >= 0)
}
}

connect_version_lt <- function(client_version, target_version) {
isTRUE(compare_connect_version(client_version, target_version))
}

token_hex <- function(n) {
raw <- as.raw(sample(0:255, n, replace = TRUE))
paste(as.character(raw), collapse = "")
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/utils.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# check_connect_version warning snapshot
# warn_old_connect warning snapshot

Code
capture_warning(check_connect_version("2022.02", "2022.01"))
capture_warning(warn_old_connect("2022.02", "2022.01"))
Output
NULL

---

Code
capture_warning(check_connect_version("2022.01", "2022.02"))
capture_warning(warn_old_connect("2022.01", "2022.02"))
Output
<warning/rlang_warning>
Warning:
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ test_that("error_if_less_than errors as expected", {
})
})

test_that("check_connect_version works", {
test_that("warn_old_connect works", {
# silent for patch version changes
expect_silent(check_connect_version("1.8.2.1-10", "1.8.2-4"))
expect_silent(warn_old_connect("1.8.2.1-10", "1.8.2-4"))

# silent if newer
expect_silent(check_connect_version("1.8.2-4", "1.8.0.5-1"))
expect_silent(warn_old_connect("1.8.2-4", "1.8.0.5-1"))

# warnings for minor version changes
expect_warning(check_connect_version("1.8.2-4", "2.8.0.5-1"), "older")
expect_warning(warn_old_connect("1.8.2-4", "2.8.0.5-1"), "older")
rlang::reset_warning_verbosity("old-connect")
})

test_that("check_connect_version warning snapshot", {
test_that("warn_old_connect warning snapshot", {
# warning messages seem to cause issues in different environments based on color codes
skip_on_cran()
# No warning
expect_snapshot(capture_warning(check_connect_version("2022.02", "2022.01")))
expect_snapshot(capture_warning(check_connect_version("2022.01", "2022.02")))
expect_snapshot(capture_warning(warn_old_connect("2022.02", "2022.01")))
expect_snapshot(capture_warning(warn_old_connect("2022.01", "2022.02")))
rlang::reset_warning_verbosity("old-connect")
})

0 comments on commit 8ad8eb9

Please sign in to comment.