Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# connectapi (development version)

- Apps running on Connect Cloud are now supported for OAuth integrations via the
new `on_connect_cloud()` helper. The `POSIT_PRODUCT` environment variable is
now recognized as an alternative to `RSTUDIO_PRODUCT` for detecting Connect.
- When using integrations, prefer to read from `CONNECT_CONTENT_SESSION_TOKEN_FILE` to find the session token. This helps long-running processes ensure that they can maintain fresh credentials.

# connectapi 0.11.1
Expand Down
4 changes: 2 additions & 2 deletions R/connect.R
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ connect <- function(

if (!missing(token)) {
error_if_less_than(con$version, "2025.01.0")
Comment thread
karawoo marked this conversation as resolved.
if (on_connect()) {
if (on_connect() || on_connect_cloud()) {
visitor_creds <- get_oauth_credentials(
con,
user_session_token = token,
Expand All @@ -1018,7 +1018,7 @@ connect <- function(
} else {
con <- connect(server = server, api_key = token_local_testing_key)
message(paste0(
"Called with `token` but not running on Connect. ",
"Called with `token` but not running on Connect or Connect Cloud. ",
"Continuing with fallback API key."
))
}
Expand Down
11 changes: 9 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,16 @@ error_code <- function(res) {
}

# Returns `TRUE` if we're running on Connect as determined by the
# `RSTUDIO_PRODUCT` env var, else `FALSE`.
# `RSTUDIO_PRODUCT` or `POSIT_PRODUCT` env var, else `FALSE`.
on_connect <- function() {
Sys.getenv("RSTUDIO_PRODUCT") == "CONNECT"
Sys.getenv("RSTUDIO_PRODUCT") == "CONNECT" ||
Sys.getenv("POSIT_PRODUCT") == "CONNECT"
}

# Returns `TRUE` if we're running on Connect Cloud as determined by the
# `POSIT_PRODUCT` env var, else `FALSE`.
on_connect_cloud <- function() {
Sys.getenv("POSIT_PRODUCT") == "CONNECT_CLOUD"
}

# Returns `TRUE` if running via testthat
Expand Down
54 changes: 52 additions & 2 deletions tests/testthat/test-connect.R
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,56 @@ test_that("Visitor client can successfully be created running on Connect", {
})
})

test_that("Visitor client can successfully be created running on Connect (POSIT_PRODUCT)", {
with_mock_api({
withr::local_options(list(rlib_warning_verbosity = "verbose"))
withr::local_envvar(
CONNECT_SERVER = "https://connect.example",
CONNECT_API_KEY = "fake",
POSIT_PRODUCT = "CONNECT"
)

expect_warning(
client <- connect(token = "my-token"),
"This feature requires Posit Connect version"
)

expect_equal(
client$server,
"https://connect.example"
)
expect_equal(
client$api_key,
"visitor-api-key"
)
})
})

test_that("Visitor client can successfully be created running on Connect Cloud", {
with_mock_api({
withr::local_options(list(rlib_warning_verbosity = "verbose"))
withr::local_envvar(
CONNECT_SERVER = "https://connect.example",
CONNECT_API_KEY = "fake",
POSIT_PRODUCT = "CONNECT_CLOUD"
)

expect_warning(
client <- connect(token = "my-token"),
"This feature requires Posit Connect version"
)

expect_equal(
client$server,
"https://connect.example"
)
expect_equal(
client$api_key,
"visitor-api-key"
)
})
})

test_that("Visitor client can successfully be created running on Connect with audience", {
with_mock_dir("2025.07.0", {
withr::local_options(list(rlib_warning_verbosity = "verbose"))
Expand Down Expand Up @@ -299,7 +349,7 @@ test_that("Visitor client uses fallback api key when running locally", {
expect_warning(
expect_message(
client <- connect(token = NULL),
"Called with `token` but not running on Connect. Continuing with fallback API key."
"Called with `token` but not running on Connect or Connect Cloud. Continuing with fallback API key."
),
"the server version is not exposed by this Posit Connect instance"
)
Expand All @@ -320,7 +370,7 @@ test_that("Visitor client uses fallback api key when running locally", {
token = NULL,
token_local_testing_key = "fallback_fake"
),
"Called with `token` but not running on Connect. Continuing with fallback API key."
"Called with `token` but not running on Connect or Connect Cloud. Continuing with fallback API key."
),
"the server version is not exposed by this Posit Connect instance"
)
Expand Down
Loading