Skip to content

Commit

Permalink
redcap_version() uses kernel
Browse files Browse the repository at this point in the history
ref #213
  • Loading branch information
wibeasley committed May 29, 2018
1 parent 44a0b81 commit 06582a6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
61 changes: 32 additions & 29 deletions R/redcap-version.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#' @param redcap_uri The URI (uniform resource identifier) of the REDCap project. Required.
#' @param token The user-specific string that serves as the password for a project. Required.
#' @param verbose A boolean value indicating if `message`s should be printed to the R console during the operation. The verbose output might contain sensitive information (*e.g.* PHI), so turn this off if the output might be visible somewhere public. Optional.
#' @param config_options A list of options to pass to `POST` method in the `httr` package. See the details below. Optional.
#'
#' @details If the API call is unsuccessful, a value of `base::package_version("0.0.0")` will be returned.
#' This ensures that a the function will always return an object of class [base::package_version].
Expand All @@ -19,9 +20,9 @@
#' token <- "9A81268476645C4E5F03428B8AC3AA7B"
#' redcap_version(redcap_uri=uri, token=token)

redcap_version <- function( redcap_uri, token, verbose=TRUE ) {
version_error=base::package_version("0.0.0")
start_time <- Sys.time()
redcap_version <- function( redcap_uri, token, verbose=TRUE, config_options=NULL ) {
version_error=base::package_version("0.0.0")
# start_time <- Sys.time()

checkmate::assert_character(redcap_uri , any.missing=F, len=1, pattern="^.{1,}$")
checkmate::assert_character(token , any.missing=F, len=1, pattern="^.{1,}$")
Expand All @@ -33,32 +34,34 @@ redcap_version <- function( redcap_uri, token, verbose=TRUE ) {
format = 'csv'
)

result <- httr::POST(
url = redcap_uri,
body = post_body
)

status_code <- result$status
success <- (status_code==200L)

raw_text <- httr::content(result, "text")
elapsed_seconds <- as.numeric(difftime(Sys.time(), start_time, units="secs"))
# This is the important line that communicates with the REDCap server.
kernel <- kernel_api(redcap_uri, post_body, config_options)

# raw_text <- "The hostname (redcap-db.hsc.net.ou.edu) / username (redcapsql) / password (XXXXXX) combination could not connect to the MySQL server. \r\n\t\tPlease check their values."
regex_cannot_connect <- "^The hostname \\((.+)\\) / username \\((.+)\\) / password \\((.+)\\) combination could not connect.+"
regex_empty <- "^\\s+$"

if(
any(grepl(regex_cannot_connect, raw_text)) |
any(grepl(regex_empty, raw_text))
) {
success <- FALSE
}
# result <- httr::POST(
# url = redcap_uri,
# body = post_body
# )
# status_code <- result$status
# success <- (status_code==200L)
#
# raw_text <- httr::content(result, "text")
# elapsed_seconds <- as.numeric(difftime(Sys.time(), start_time, units="secs"))
#
# # raw_text <- "The hostname (redcap-db.hsc.net.ou.edu) / username (redcapsql) / password (XXXXXX) combination could not connect to the MySQL server. \r\n\t\tPlease check their values."
# regex_cannot_connect <- "^The hostname \\((.+)\\) / username \\((.+)\\) / password \\((.+)\\) combination could not connect.+"
# regex_empty <- "^\\s+$"
#
# if(
# any(grepl(regex_cannot_connect, raw_text)) |
# any(grepl(regex_empty, raw_text))
# ) {
# success <- FALSE
# }

if( success ) {
if( kernel$success ) {
try (
{
version <- package_version(raw_text)
version <- package_version(kernel$raw_text)
},
silent = TRUE #Don't print the warning in the try block. Print it below, where it's under the control of the caller.
)
Expand All @@ -67,18 +70,18 @@ redcap_version <- function( redcap_uri, token, verbose=TRUE ) {
if( exists("version") & inherits(version, "package_version") ) {
outcome_message <- paste0(
"The REDCap version was successfully determined in ",
round(elapsed_seconds, 1), " seconds. The http status code was ",
status_code, ". It is ", version, "."
round(kernel$elapsed_seconds, 1), " seconds. The http status code was ",
kernel$status_code, ". It is ", version, "."
)

} else {
version <- version_error
outcome_message <- paste0("The REDCap version determination failed. The http status code was ", status_code, ". The 'raw_text' returned was '", raw_text, "'.")
outcome_message <- paste0("The REDCap version determination failed. The http status code was ", kernel$status_code, ". The 'raw_text' returned was '", kernel$raw_text, "'.")
}
}
else {
version <- version_error
outcome_message <- paste0("The REDCap version determination failed. The error message was:\n", raw_text)
outcome_message <- paste0("The REDCap version determination failed. The error message was:\n", kernel$raw_text)
}

if( verbose )
Expand Down
4 changes: 3 additions & 1 deletion man/redcap_version.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 06582a6

Please sign in to comment.