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

encapsulate kernel of API interaction #213

Closed
wibeasley opened this issue May 29, 2018 · 0 comments
Closed

encapsulate kernel of API interaction #213

wibeasley opened this issue May 29, 2018 · 0 comments
Assignees

Comments

@wibeasley
Copy link
Member

Building on #210, I'm refactoring some of the functions so their important piece that interacts with REDCap over curl/httr can be refactored into a reusable function that looks something like:

core <- function( redcap_uri, post_body, config_options ) 

  start_time <- Sys.time()

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

  status_code           <- result$status
  success               <- (status_code==200L)
  raw_text              <- httr::content(result, "text")
  raw_text              <- gsub("\r\n", "\n", raw_text) # Convert all line-ending to linux-style
  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+$"

  success     <- (success & !any(grepl(regex_cannot_connect, raw_text)) & !any(grepl(regex_empty, raw_text)))

  return( list(
    status_code,
    success,
    raw_text,
    elapsed_seconds
  ) )
}

@nutterb (or someone else who pays attention to this package and contributes ideas & code --sorry I forgot who) suggested at least a year ago to pursue this encapsulation. His/her argument was it (a) produces more reusable & testable code & (b) makes it easier for us to adapt to httr interface changes, of if we need to switch to a new curl-ish package entirely.

Closes #210, because it's included in this issue.

@wibeasley wibeasley self-assigned this May 29, 2018
wibeasley added a commit that referenced this issue May 29, 2018
wibeasley added a commit that referenced this issue May 29, 2018
wibeasley added a commit that referenced this issue May 29, 2018
wibeasley added a commit that referenced this issue May 29, 2018
ref #213
wibeasley added a commit that referenced this issue May 29, 2018
wibeasley added a commit that referenced this issue May 29, 2018
wibeasley added a commit that referenced this issue May 29, 2018
ref #213
wibeasley added a commit that referenced this issue May 29, 2018
wibeasley added a commit that referenced this issue May 29, 2018
wibeasley added a commit that referenced this issue May 29, 2018
wibeasley added a commit that referenced this issue May 30, 2018
outside of `kernel_api()`.  The only exceptions are (a) `httr::upload_file()` and (b) testing structure.

ref #213
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant