diff --git a/DESCRIPTION b/DESCRIPTION index 309f304..13737e4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -21,6 +21,7 @@ Imports: cli, httr, jsonlite, + laminr.api, purrr, R6 Suggests: @@ -31,3 +32,4 @@ Suggests: withr VignetteBuilder: quarto Config/testthat/edition: 3 +Remotes: github::data-intuitive/laminr.api diff --git a/NAMESPACE b/NAMESPACE index b4e087b..0034a4c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,10 +5,6 @@ importFrom(R6,R6Class) importFrom(cli,cli_abort) importFrom(cli,cli_inform) importFrom(cli,cli_warn) -importFrom(httr,GET) -importFrom(httr,POST) -importFrom(httr,add_headers) -importFrom(httr,content) importFrom(jsonlite,toJSON) importFrom(purrr,discard) importFrom(purrr,keep) diff --git a/R/InstanceAPI.R b/R/InstanceAPI.R index b6bbb99..1244164 100644 --- a/R/InstanceAPI.R +++ b/R/InstanceAPI.R @@ -15,21 +15,19 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter #' - schema_id: The ID of the schema initialize = function(instance_settings) { private$.instance_settings <- instance_settings + private$.api_client <- laminr.api::ApiClient$new(instance_settings$api_url) + private$.default_api <- laminr.api::DefaultApi$new(private$.api_client) }, #' @description #' Get the schema for the instance. - get_schema = function() { - # TODO: replace with laminr.api get_schema call - url <- paste0( - private$.instance_settings$api_url, - "/instances/", - private$.instance_settings$id, - "/schema" - ) - - response <- httr::GET(url) - - private$process_response(response, "get schema") + get_schema = function(id) { + try( + private$.default_api$GetSchemaInstancesInstanceIdSchemaGet( + private$.instance_settings$id + ), + silent = TRUE + ) |> + private$process_response("schema") }, #' @description #' Get a record from the instance. @@ -44,6 +42,7 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter if (!is.null(select) && !is.character(select)) { cli_abort("select must be a character vector") } + if (verbose) { field_name_str <- if (!is.null(select)) { @@ -58,43 +57,20 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter field_name_str, "\n" )) } - body_data <- list() - if (!is.null(select)) { - body_data$select <- select - } - body <- - if (length(body_data) > 0) { - jsonlite::toJSON(body_data) - } else { - "{}" - } - url <- paste0( - private$.instance_settings$api_url, - "/instances/", - private$.instance_settings$id, - "/modules/", - module_name, - "/", - registry_name, - "/", - id_or_uid, - "?schema_id=", - private$.instance_settings$schema_id, - "&include_foreign_keys=", - tolower(include_foreign_keys) - ) - - response <- httr::POST( - url, - httr::add_headers( - accept = "application/json", - `Content-Type` = "application/json" + try( + private$.default_api$GetRecordInstancesInstanceIdModulesModuleNameModelNameIdOrUidPost( + instance_id = private$.instance_settings$id, + module_name = module_name, + model_name = registry_name, + id_or_uid = id_or_uid, + schema_id = private$.instance_settings$schema_id, + include_foreign_keys = tolower(include_foreign_keys), + get_record_request_body = laminr.api::GetRecordRequestBody$new(select) ), - body = body - ) - - private$process_response(response, "get record") + silent = TRUE + ) |> + private$process_response("record") }, #' @description #' Print an `API` @@ -119,17 +95,17 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter ), private = list( .instance_settings = NULL, + .api_client = NULL, + .default_api = NULL, process_response = function(response, request_type) { - content <- httr::content(response) - if (httr::http_error(response)) { - if (is.list(content) && "detail" %in% names(content)) { - cli_abort(content$detail) - } else { - cli_abort("Failed to {request_type} from instance. Output: {content}") - } + if (inherits(response, "try-error")) { + cli::cli_abort(c( + "Request for {request_type} failed", + "i" = "Error message: {.code {response[1]}}" + )) } - content + return(response) } ) ) diff --git a/R/laminr-package.R b/R/laminr-package.R index 85e054e..1720484 100644 --- a/R/laminr-package.R +++ b/R/laminr-package.R @@ -4,7 +4,6 @@ ## usethis namespace: start #' @importFrom cli cli_abort cli_warn cli_inform #' @importFrom R6 R6Class -#' @importFrom httr GET POST content add_headers #' @importFrom purrr map map_chr map2 pmap set_names list_flatten transpose discard keep ## usethis namespace: end NULL