From 5dd2611b88ba4cfde073d1eb2d80d21732453ef2 Mon Sep 17 00:00:00 2001 From: Luke Zappia Date: Thu, 10 Oct 2024 14:09:01 +0200 Subject: [PATCH 1/5] Replace API calls in Instance API --- R/InstanceAPI.R | 84 +++++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 48 deletions(-) diff --git a/R/InstanceAPI.R b/R/InstanceAPI.R index f1c4c5a..2d46c44 100644 --- a/R/InstanceAPI.R +++ b/R/InstanceAPI.R @@ -15,25 +15,25 @@ 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) }, #' Get the schema for the instance. - get_schema = function() { - # TODO: replace with laminr.api get_schema call - request <- httr::GET( - paste0( - private$.instance_settings$api_url, - "/instances/", - private$.instance_settings$id, - "/schema" + get_schema = function(id) { + schema <- try( + private$.default_api$GetSchemaInstancesInstanceIdSchemaGet( + private$.instance_settings$id ) ) - content <- httr::content(request) - if (httr::http_error(request)) { - cli_abort(content$detail) + if (inherits(schema, "try-error")) { + cli::cli_abort(c( + "Failed to get schema", + "i" = "Error message: {.code {schema[1]}}" + )) } - content + return(schema) }, #' Get a record from the instance. #' @importFrom jsonlite toJSON @@ -46,6 +46,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)) { @@ -60,51 +61,38 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter field_name_str, "\n" )) } - body_data <- list() + if (!is.null(select)) { - body_data$select <- select + body <- jsonlite::toJSON(list(select = select)) + } else { + body <- "{}" } - 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) - ) - - request <- httr::POST( - url, - httr::add_headers( - accept = "application/json", - `Content-Type` = "application/json" - ), - body = body + record <- 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 = body + ) ) - content <- httr::content(request) - if (httr::http_error(request)) { - cli_abort(content$detail) + if (inherits(record, "try-error")) { + cli::cli_abort(c( + "Failed to get schema", + "i" = "Error message: {.code {record[1]}}" + )) } - content + return(record) } ), private = list( - .instance_settings = NULL + .instance_settings = NULL, + .api_client = NULL, + .default_api = NULL ) ) From baabb30aee6e04c45f676d2b14a10f1d8a813aab Mon Sep 17 00:00:00 2001 From: Luke Zappia Date: Mon, 14 Oct 2024 11:38:02 +0200 Subject: [PATCH 2/5] Use process_response() in InstanceAPI --- R/InstanceAPI.R | 53 +++++++++++++++---------------------------------- 1 file changed, 16 insertions(+), 37 deletions(-) diff --git a/R/InstanceAPI.R b/R/InstanceAPI.R index 68e95ab..114aafb 100644 --- a/R/InstanceAPI.R +++ b/R/InstanceAPI.R @@ -21,20 +21,12 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter #' @description #' Get the schema for the instance. get_schema = function(id) { - schema <- try( + try( private$.default_api$GetSchemaInstancesInstanceIdSchemaGet( private$.instance_settings$id ) - ) - - if (inherits(schema, "try-error")) { - cli::cli_abort(c( - "Failed to get schema", - "i" = "Error message: {.code {schema[1]}}" - )) - } - - return(schema) + ) |> + private$process_response("schema") }, #' @description #' Get a record from the instance. @@ -65,13 +57,7 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter )) } - if (!is.null(select)) { - body <- jsonlite::toJSON(list(select = select)) - } else { - body <- "{}" - } - - record <- try( + try( private$.default_api$GetRecordInstancesInstanceIdModulesModuleNameModelNameIdOrUidPost( instance_id = private$.instance_settings$id, module_name = module_name, @@ -79,33 +65,26 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter id_or_uid = id_or_uid, schema_id = private$.instance_settings$schema_id, include_foreign_keys = tolower(include_foreign_keys), - get_record_request_body = body + get_record_request_body = laminr.api::GetRecordRequestBody$new(select) ) - ) - - if (inherits(record, "try-error")) { - cli::cli_abort(c( - "Failed to get schema", - "i" = "Error message: {.code {record[1]}}" - )) - } - - content + ) |> + private$process_response("record") } ), 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) }, #' @description #' Print an `API` From 9a4aa34ae47ea932661062e7d71915078427132d Mon Sep 17 00:00:00 2001 From: Luke Zappia Date: Mon, 14 Oct 2024 14:52:19 +0200 Subject: [PATCH 3/5] Pass checks and style --- DESCRIPTION | 1 + NAMESPACE | 4 ---- R/InstanceAPI.R | 1 - R/laminr-package.R | 1 - 4 files changed, 1 insertion(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 309f304..f0f83da 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -21,6 +21,7 @@ Imports: cli, httr, jsonlite, + laminr.api, purrr, R6 Suggests: 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 114aafb..2fed8da 100644 --- a/R/InstanceAPI.R +++ b/R/InstanceAPI.R @@ -76,7 +76,6 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter .api_client = NULL, .default_api = NULL, process_response = function(response, request_type) { - if (inherits(response, "try-error")) { cli::cli_abort(c( "Request for {request_type} failed", 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 From a58f62107c4e84673168d6a1264c0f980fc96472 Mon Sep 17 00:00:00 2001 From: Luke Zappia Date: Mon, 14 Oct 2024 15:02:20 +0200 Subject: [PATCH 4/5] Silence try in API calls --- R/InstanceAPI.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/R/InstanceAPI.R b/R/InstanceAPI.R index b6fa1d1..1244164 100644 --- a/R/InstanceAPI.R +++ b/R/InstanceAPI.R @@ -24,7 +24,8 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter try( private$.default_api$GetSchemaInstancesInstanceIdSchemaGet( private$.instance_settings$id - ) + ), + silent = TRUE ) |> private$process_response("schema") }, @@ -66,7 +67,8 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter schema_id = private$.instance_settings$schema_id, include_foreign_keys = tolower(include_foreign_keys), get_record_request_body = laminr.api::GetRecordRequestBody$new(select) - ) + ), + silent = TRUE ) |> private$process_response("record") }, From 227b581075a164cbf3573b09953b93b5a12789e3 Mon Sep 17 00:00:00 2001 From: Luke Zappia Date: Mon, 14 Oct 2024 15:13:18 +0200 Subject: [PATCH 5/5] Add laminr.api GitHub as remote --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index f0f83da..13737e4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -32,3 +32,4 @@ Suggests: withr VignetteBuilder: quarto Config/testthat/edition: 3 +Remotes: github::data-intuitive/laminr.api