Skip to content

Commit

Permalink
Big rewrite to use new connection interface to API
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwarkentin committed Jan 17, 2024
1 parent 6303468 commit 80ddc59
Show file tree
Hide file tree
Showing 45 changed files with 636 additions and 271 deletions.
131 changes: 131 additions & 0 deletions R/APIConnections.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#' OpenM++ API Connections
#'
#' Register a connection to the OpenM++ Application Programming Interface (API).
#' Currently, two different API connections "local" and "remote" are
#' available. Note that "local" and "remote" describe where the API is running
#' relative to the machine running the R session. For example, users logged
#' into the cloud-hosted RStudio Server sessions will acceess the API running
#' locally. Those who use a machine running on their local network may use
#' a remote connection to connect with the cloud-based API.
#'
#' @param url URl for making API requests. See `Details` for more
#' instructions.
#' @param user User name for logging into remote API. See `Details` for more
#' instructions.
#' @param pwd Password for logging into remote API. See `Details` for more
#' instructions.
#' @param ... Not currently used.
#'
#' @details A `user` name and password (`pwd`) are sensitive information and
#' should not be shared. To avoid hard-coding your user name and password into
#' your R scripts, we recommend declaring this information in your global or
#' project-specific `.Renviron` file. The same approach may be used to declare
#' your URLs for making API requests. While these URLs are typically not
#' sensitive information, keeping all of this information in one place makes
#' sense for consistenct. For local and remote API connections, set the
#' following environment variables in your `.Renviron` files:
#'
#' - `OPENMPP_LOCAL_URL`: URL for a local API connection.
#' - `OPENMPP_REMOTE_URL`: URL for a remote API connection.
#' - `OPENMPP_REMOTE_USER`: User name for logging into a remote API
#' connection.
#' - `OPENMPP_REMOTE_PWD`: Password for logging into a remote API connection.
#'
#' @return Nothing, invisibly. Behind-the-scenes, an instance of the
#' `OpenMppLocal` or `OpenMppRemote` R6 classes are created. These
#' objects should not be accessed directly by the user, instead, the package
#' internally uses these connections to communicate with the OpenM++ API.
#'
#' @md
#'
#' @export
use_OpenMpp_local <- function(url = Sys.getenv('OPENMPP_LOCAL_URL'), ...) {
rlang::check_dots_empty()
con <- OpenMppLocal$new(url)
assign('API', con, OpenMpp)
invisible()
}

#' @rdname use_OpenMpp_local
#' @export
use_OpenMpp_remote <- function(
url = Sys.getenv('OPENMPP_REMOTE_URL'),
user = Sys.getenv('OPENMPP_REMOTE_USER'),
pwd = Sys.getenv('OPENMPP_REMOTE_PWD'),
...
) {
rlang::check_dots_empty()
con <- OpenMppRemote$new(url, user, pwd)
assign('API', con, OpenMpp)
invisible()
}

OpenMppLocal <-
R6::R6Class(
classname = 'OpenMppLocal',
inherit = OpenMppConnection,
public = list(
url = NULL,
initialize = function(url) {
self$url <- url
invisible(self)
},
build_request = function() {
httr2::request(self$url) |>
httr2::req_url_path_append('api')
}
)
)

OpenMppRemote <-
R6::R6Class(
classname = 'OpenMppRemote',
inherit = OpenMppLocal,
public = list(
url = NULL,
token = NULL,
initialize = function(url, user, pwd) {
super$initialize(url)
private$login(user, pwd)
self
},
build_request = function() {
httr2::request(self$url) |>
httr2::req_url_path_append('api') |>
httr2::req_auth_bearer_token(self$token)
}
),
private = list(
login = function(user, pwd) {
self$token <-
httr2::request(self$url) |>
httr2::req_url_path_append('login') |>
httr2::req_body_form(
username = user,
password = pwd
) |>
httr2::req_perform() |>
httr2::resp_body_string()
invisible()
},
logout = function() {
httr2::request(self$url) |>
httr2::req_url_path_append('login') |>
httr2::req_url_query(logout = 'true') |>
httr2::req_auth_bearer_token(self$token) |>
httr2::req_perform()
invisible()
},
finalize = function() private$logout()
)
)

OpenMppConnection <-
R6::R6Class(
classname = 'OpenMppConnection',
public = list(
build_request = function() {
rlang::abort('Please register an API connection using `openmpp::use_OpenMpp_local()` or `openmpp::use_OpenMpp_remote()`.')
}
)
)
22 changes: 18 additions & 4 deletions R/Admin.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#' @export
admin_refresh_models <- function() {
api_path <- 'api/admin/all-models/refresh'
httr2::request(api_url()) |>
OpenMpp$API$build_request() |>
httr2::req_url_path(api_path) |>
httr2::req_method('POST') |>
httr2::req_perform()
Expand All @@ -20,7 +20,7 @@ admin_refresh_models <- function() {
#' @export
admin_close_models <- function() {
api_path <- 'api/admin/all-models/close'
httr2::request(api_url()) |>
OpenMpp$API$build_request() |>
httr2::req_url_path(api_path) |>
httr2::req_method('POST') |>
httr2::req_perform()
Expand All @@ -34,7 +34,21 @@ admin_pause_models <- function(pause) {
rlang::abort('`pause` must be a logical (TRUE or FALSE).')
}
api_path <- glue::glue('api/admin/jobs-pause/{pause}')
httr2::request(api_url()) |>
OpenMpp$API$build_request() |>
httr2::req_url_path(api_path) |>
httr2::req_method('POST') |>
httr2::req_perform()
invisible()
}

#' @rdname admin_refresh_models
#' @export
admin_pause_all_models <- function(pause) {
if (!rlang::is_scalar_logical(pause)) {
rlang::abort('`pause` must be a logical (TRUE or FALSE).')
}
api_path <- glue::glue('/api/admin-all/jobs-pause/{pause}')
OpenMpp$API$build_request() |>
httr2::req_url_path(api_path) |>
httr2::req_method('POST') |>
httr2::req_perform()
Expand All @@ -45,7 +59,7 @@ admin_pause_models <- function(pause) {
#' @export
admin_shutdown_service <- function() {
api_path <- 'api/admin/shutdown'
httr2::request(api_url()) |>
OpenMpp$API$build_request() |>
httr2::req_url_path(api_path) |>
httr2::req_method('PUT') |>
httr2::req_perform()
Expand Down
59 changes: 55 additions & 4 deletions R/Downloads.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' @export
initiate_model_download <- function(model) {
api_path <- glue::glue('api/download/model/{model}')
httr2::request(api_url()) |>
OpenMpp$API$build_request() |>
httr2::req_url_path(api_path) |>
httr2::req_body_json(
data = list(
Expand All @@ -30,7 +30,7 @@ initiate_model_download <- function(model) {
#' @export
initiate_run_download <- function(model, run) {
api_path <- glue::glue('/api/download/model/{model}/run/{run}')
httr2::request(api_url()) |>
OpenMpp$API$build_request() |>
httr2::req_url_path(api_path) |>
httr2::req_body_json(
data = list(
Expand All @@ -49,7 +49,7 @@ initiate_run_download <- function(model, run) {
#' @export
initiate_workset_download <- function(model, set) {
api_path <- glue::glue('/api/download/model/{model}/workset/{set}')
httr2::request(api_url()) |>
OpenMpp$API$build_request() |>
httr2::req_url_path(api_path) |>
httr2::req_body_json(
data = list(
Expand All @@ -68,9 +68,60 @@ initiate_workset_download <- function(model, set) {
#' @export
delete_download_files <- function(folder) {
api_path <- glue::glue('/api/download/delete/{folder}')
httr2::request(api_url()) |>
OpenMpp$API$build_request() |>
httr2::req_url_path(api_path) |>
httr2::req_method('DELETE') |>
httr2::req_perform()
invisible()
}

#' @rdname initiate_model_download
#' @export
delete_download_files_async <- function(folder) {
api_path <- glue::glue('/api/download/start/delete/{folder}')
OpenMpp$API$build_request() |>
httr2::req_url_path(api_path) |>
httr2::req_method('DELETE') |>
httr2::req_perform()
invisible()
}

#' @rdname initiate_model_download
#' @export
get_download_log <- function(name) {
api_path <- glue::glue('/api/download/log/file/{name}')
OpenMpp$API$build_request() |>
httr2::req_url_path(api_path) |>
httr2::req_perform() |>
httr2::resp_body_json()
}

#' @rdname initiate_model_download
#' @export
get_download_logs_model <- function(model) {
api_path <- glue::glue('/api/download/log/model/{model}')
OpenMpp$API$build_request() |>
httr2::req_url_path(api_path) |>
httr2::req_perform() |>
httr2::resp_body_json()
}

#' @rdname initiate_model_download
#' @export
get_download_logs_all <- function() {
api_path <- glue::glue('/api/download/log/all')
OpenMpp$API$build_request() |>
httr2::req_url_path(api_path) |>
httr2::req_perform() |>
httr2::resp_body_json()
}

#' @rdname initiate_model_download
#' @export
get_download_filetree <- function(folder) {
api_path <- glue::glue('/api/download/file-tree/{folder}')
OpenMpp$API$build_request() |>
httr2::req_url_path(api_path) |>
httr2::req_perform() |>
httr2::resp_body_json()
}
80 changes: 0 additions & 80 deletions R/Model.R

This file was deleted.

8 changes: 4 additions & 4 deletions R/ModelExtras.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' @export
get_model_lang_list <- function(model) {
api_path <- glue::glue('api/model/{model}/lang-list')
httr2::request(api_url()) |>
OpenMpp$API$build_request() |>
httr2::req_url_path(api_path) |>
httr2::req_perform() |>
httr2::resp_body_json()
Expand All @@ -21,7 +21,7 @@ get_model_lang_list <- function(model) {
#' @export
get_model_word_list <- function(model) {
api_path <- glue::glue('api/model/{model}/word-list')
httr2::request(api_url()) |>
OpenMpp$API$build_request() |>
httr2::req_url_path(api_path) |>
httr2::req_perform() |>
httr2::resp_body_json()
Expand All @@ -31,7 +31,7 @@ get_model_word_list <- function(model) {
#' @export
get_model_profile <- function(model, profile) {
api_path <- glue::glue('api/model/{model}/profile/{profile}')
httr2::request(api_url()) |>
OpenMpp$API$build_request() |>
httr2::req_url_path(api_path) |>
httr2::req_perform() |>
httr2::resp_body_json()
Expand All @@ -41,7 +41,7 @@ get_model_profile <- function(model, profile) {
#' @export
get_model_profile_list <- function(model) {
api_path <- glue::glue('api/model/{model}/profile-list')
httr2::request(api_url()) |>
OpenMpp$API$build_request() |>
httr2::req_url_path(api_path) |>
httr2::req_perform() |>
httr2::resp_body_json()
Expand Down
Loading

0 comments on commit 80ddc59

Please sign in to comment.