diff --git a/CHANGELOG.md b/CHANGELOG.md index 47c0d61..f046e0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # laminr v0.2.1 +## BUG FIXES + +* Allow connecting to private LaminDB instances (PR #118). + ## DOCUMENTATION * Update the laminr Getting Started vignette with feedback from demo (PR #113). diff --git a/R/InstanceAPI.R b/R/InstanceAPI.R index 1038b53..6c62143 100644 --- a/R/InstanceAPI.R +++ b/R/InstanceAPI.R @@ -31,7 +31,10 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter "/schema" ) - response <- httr::GET(url) + response <- httr::GET( + url, + httr::add_headers(.headers = private$get_headers()) + ) private$process_response(response, "get schema") }, @@ -99,10 +102,7 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter response <- httr::POST( url, - httr::add_headers( - accept = "application/json", - `Content-Type` = "application/json" - ), + httr::add_headers(.headers = private$get_headers()), body = body ) @@ -187,10 +187,7 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter response <- httr::POST( url, - httr::add_headers( - accept = "application/json", - `Content-Type` = "application/json" - ), + httr::add_headers(.headers = private$get_headers()), body = body ) @@ -202,13 +199,6 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter registry_name, id_or_uid, verbose = FALSE) { - user_settings <- .get_user_settings() - if (is.null(user_settings$access_token)) { - cli::cli_abort(c( - "There is no access token for the current user", - "i" = "Run {.code lamin login} and reconnect to the database in a new R session" - )) - } url <- paste0( private$.instance_settings$api_url, @@ -231,9 +221,7 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter response <- httr::DELETE( url, httr::add_headers( - accept = "application/json", - `Content-Type` = "application/json", - Authorization = paste("Bearer", user_settings$access_token) + .headers = private$get_headers(authorization_required = TRUE) ) ) @@ -262,6 +250,24 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter ), private = list( .instance_settings = NULL, + get_headers = function(authorization_required = FALSE) { + headers <- c( + accept = "application/json", + `Content-Type` = "application/json" + ) + user_settings <- .get_user_settings() + + if (!is.null(user_settings$access_token)) { + headers[["Authorization"]] <- paste("Bearer", user_settings$access_token) + } else if (authorization_required) { + cli::cli_abort(c( + "There is no access token for the current user", + "i" = "Run {.code lamin login} and reconnect to the database in a new R session" + )) + } + + return(headers) + }, process_response = function(response, request_type) { content <- httr::content(response) if (httr::http_error(response)) { diff --git a/tests/testthat/test-connect_lamindata.R b/tests/testthat/test-connect.R similarity index 77% rename from tests/testthat/test-connect_lamindata.R rename to tests/testthat/test-connect.R index fdc1fb9..e189041 100644 --- a/tests/testthat/test-connect_lamindata.R +++ b/tests/testthat/test-connect.R @@ -24,3 +24,12 @@ test_that("Connecting to lamindata works", { expect_s3_class(artifact$wells, "RelatedRecords") # one-to-many }) + +test_that("Connecting to a private instance works", { + skip_if_not_logged_in() + + db <- connect("laminlabs/lamin-dev") + + instance_settings <- db$get_settings() + expect_equal(instance_settings$name, "lamin-dev") +})