Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace API calls with {laminr.api} #38

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Imports:
cli,
httr,
jsonlite,
laminr.api,
purrr,
R6
Suggests:
Expand All @@ -31,3 +32,4 @@ Suggests:
withr
VignetteBuilder: quarto
Config/testthat/edition: 3
Remotes: github::data-intuitive/laminr.api
4 changes: 0 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
86 changes: 31 additions & 55 deletions R/InstanceAPI.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)) {
Expand All @@ -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`
Expand All @@ -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)
}
)
)
1 change: 0 additions & 1 deletion R/laminr-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading