You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
approve_interview <- function(
server,
user,
password,
interviewId,
status,
comment
) {
# load necessary libraries
# packages needed for this program
packagesNeeded <- c(
"httr", # to talk with the server
"dplyr", # to do basic data wrangling
"readr" # to write failure info to disk
)
# identify and install those packages that are not already installed
packagesToInstall <- packagesNeeded[!(packagesNeeded %in% installed.packages()[,"Package"])]
if(length(packagesToInstall))
install.packages(packagesToInstall, quiet = TRUE,
repos = 'https://cloud.r-project.org/', dep = TRUE)
# load all needed packages
lapply(packagesNeeded, library, character.only = TRUE)
# move to next interview if not in "approvable" status
if (!status %in% c(100, 120)) {
next
}
# if comment is NA, convert to an empty string for API call
if (is.na(comment)) {
comment = ""
}
# formulate API call
call <- paste0(
"https://", server, ".mysurvey.solutions",
"/api/v1/interviews/", interviewId,
case_when(
status == 100 ~ "/approve", # ... if Completed
status == 120 ~ "/hqapprove" # ... if ApprovedBySupervisor
),
"?comment=", curlPercentEncode(comment))
# make request
request <- PATCH(url = call, config = authenticate(user = user, password = password))
# make a second, modified request if interview was initially Completed and first request successful
if (status == 100 & httr::status_code(request) == 200) {
call_next <- str_replace(call, pattern = "/approve", replacement = "/hqapprove")
request <- PATCH(url = call_next, config = authenticate(user = user, password = password))
}
# check that request worked
# if so, indicate success
if (httr::status_code(request) == 200) {
print(paste0("Success approving interview: ", interviewId))
# if no, indicate failure, capture reason, and write reason to disk
} else {
print(paste0("Problem approving interview ", interviewId))
# extract interview info and reason for failure
approve_failed <- data.frame(
interview__id = interviewId,
interview__status = status,
status_code = httr::status_code(request),
reason = content(request)$Message,
stringsAsFactors = FALSE
)
# write failure info to disk
readr::write_csv(
approve_failed,
path = paste0(project_folder, "failed_approvals.csv"),
append = TRUE
)
}
}
The text was updated successfully, but these errors were encountered:
Code from an older effort:
The text was updated successfully, but these errors were encountered: