#' Retrieve encrypted instance files of a form
#'
#' @param instanceRegistry Instance registry dataframe generated by getInstanceRegistry
#' @param writeToPath Path to directory where to save encrypted instance files
#' @param baseDownloadUrl Optional, base download url from API
#'
#' @return Instance registry dataframe with paths to individual encrypted instances files
#' @import httr
#' @export
#'
#' @examples
getEncryptedInstances <- function(instanceRegistry, writeToPath, baseDownloadUrl = "https://api.ona.io"){
if(!is.null(instanceRegistry)){
if(dir.exists(writeToPath)){
# Check missing urls
if(sum(is.na(instanceRegistry$attachments_download_url)) > 0){
warning("The following instances lack a download url, please check encryption:")
print(instanceRegistry[which(is.na(instanceRegistry$attachments_download_url)),]$uuid)
}
# Get files and write to directory
instanceRegistry$encrypted_file_path <- NA_character_
imax <- nrow(instanceRegistry)
for(i in 1:imax){
url <- instanceRegistry[i,]$attachments_download_url
uuid <- instanceRegistry[i,]$uuid
filename <- file.path(writeToPath, paste0(uuid, ".xml.enc"))
success <- F
messageLead <- paste0(sprintf(paste0("%0",nchar(imax),"d"), i), "/", imax)
if(!is.na(url) & !is.na(uuid)){
if(!file.exists(filename)){
d <- GET(paste0(baseDownloadUrl, url))
writeBin(content(d, "raw"), filename)
if(file.exists(filename)) {
message(paste0(messageLead, " - ", uuid, " instance file download success"))
success <- T
}
}
else{
message(paste0(messageLead, " - ", uuid, " instance file already exists"))
success <- T
}
}
if(success){
instanceRegistry[i,]$encrypted_file_path <- filename
}
else {
message(paste0(lead, " - ", uuid, " instance file download failure"))
}
}
# Check that all files have been downloaded
downloadedIds <- sub(x = dir(writeToPath), pattern = "^([a-z0-9\\-]+)\\.xml\\.enc$","\\1")
missingIds <- setdiff(instanceRegistry$uuid, downloadedIds)
if(length(missingIds) > 0){
message("The following instances were not downloaded:")
print(missingIds)
}
else {
message("All instances were downloaded sucessfully")
}
}
else {
warning("The specified directory does not exist.")
}
}
else {
message("Form is empty")
}
return(instanceRegistry)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.