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

Redirect 413 #425

Merged
merged 10 commits into from
Dec 20, 2024
1 change: 0 additions & 1 deletion .github/workflows/r-cmd-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ jobs:
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel-1'}
- {os: ubuntu-latest, r: 'oldrel-2'}
- {os: ubuntu-latest, r: 'oldrel-3'}

env:
R_KEEP_PKG_SOURCE: yes
Expand Down
3 changes: 2 additions & 1 deletion R/curl.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
fields = cfg$fields,
options = modifyList(list(timeout_ms = 3e5,
useragent = .curlDefaultUa(),
post = TRUE),
post = TRUE,
followlocation = FALSE),
cfg$options),
auth_token = token,
output = structure(list(), class = c("write_memory", "write_function"))
Expand Down
156 changes: 81 additions & 75 deletions R/exportPDF.R
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
#' @name exportPdf
#' @title Export PDF file of Data Collection Instruments
#'
#' @description These methods allow the user to download PDF files of
#' data collection instruments. The download may be with or without
#' collected data; and may return a single record, multiple records,
#'
#' @description These methods allow the user to download PDF files of
#' data collection instruments. The download may be with or without
#' collected data; and may return a single record, multiple records,
#' or all records.
#'
#' @inheritParams common-rcon-arg
#' @inheritParams common-dot-args
#' @inheritParams common-api-args
#' @param dir `character(1)`. The directory into which the file
#' @param dir `character(1)`. The directory into which the file
#' should be saved.
#' @param filename `character(1)`. The base of the file name. When
#' @param filename `character(1)`. The base of the file name. When
#' `record = NULL`, it will be appended with `"_blank.pdf"`. When
#' `record` has a value, it will be appended with `"_record_[record id].pdf"`
#' @param record `character(1)`, `integerish(1)`, or `NULL`.
#' The record id for which forms should be downloaded.
#' `record` has a value, it will be appended with `"_record_[record id].pdf"`
#' @param record `character(1)`, `integerish(1)`, or `NULL`.
#' The record id for which forms should be downloaded.
#' @param events `character`. The events for which forms should be downloaded
#' @param instruments `character`. The instruments for which forms
#' @param instruments `character`. The instruments for which forms
#' should be downloaded
#' @param all_records `logical(1)`. When `TRUE` forms for all records
#' @param all_records `logical(1)`. When `TRUE` forms for all records
#' are downloaded. When `TRUE`, this overrides the `records` argument.
#'
#'
#' @details
#' These methods mimics the behavior of "Download PDF of Instruments" button on the
#' REDCap user interface. They permit the user to export a PDF file for:
#'
#'
#' 1. A single collection instrument (blank)c
#' 2. All instruments (blank)
#' 3. A single instrument (with data from a single record)c
#' 4. All instruments (with data from a single record)
#' 5. All instruments (with data from all records)
#'
#'
#' @return
#' `exportPdf` invisibly returns the location on the local system
#' @return
#' `exportPdf` invisibly returns the location on the local system
#' to whihc the files is saved.
#'
#' @seealso
Expand All @@ -44,124 +44,130 @@
#' [exportInstruments()],\cr
#' [exportMappings()],\cr
#' [importMappings()]
#'
#' @export

exportPdf <- function(rcon,
dir,
filename = "redcap_forms_download",
record = NULL,
events = NULL,
instruments = NULL,
all_records = FALSE,
exportPdf <- function(rcon,
dir,
filename = "redcap_forms_download",
record = NULL,
events = NULL,
instruments = NULL,
all_records = FALSE,
...){
UseMethod("exportPdf")
}

#' @rdname exportPdf
#' @export

exportPdf.redcapApiConnection <- function(rcon,
dir,
exportPdf.redcapApiConnection <- function(rcon,
dir,
filename = "redcap_forms_download",
record = NULL,
record = NULL,
events = NULL,
instruments = NULL,
all_records = FALSE,
instruments = NULL,
all_records = FALSE,
...)
{
if (is.numeric(record)) record <- as.character(record)


dots <- list(...)
config <- if("config" %in% names(dots)) dots$config else list()

##################################################################
# Argument Validation

coll <- checkmate::makeAssertCollection()

checkmate::assert_class(x = rcon,
classes = "redcapApiConnection",
add = coll)
checkmate::assert_character(x = dir,
len = 1,

checkmate::assert_character(x = dir,
len = 1,
any.missing = FALSE,
add = coll)
checkmate::assert_character(x = filename,
len = 1,
any.missing = FALSE,

checkmate::assert_character(x = filename,
len = 1,
any.missing = FALSE,
add = coll)
checkmate::assert_character(x = record,
len = 1,

checkmate::assert_character(x = record,
len = 1,
any.missing = FALSE,
null.ok = TRUE,
add = coll)

checkmate::assert_character(x = events,
len = 1,
len = 1,
any.missing = FALSE,
null.ok = TRUE,
add = coll)

checkmate::assert_character(x = instruments,
len = 1,
any.missing = FALSE,
any.missing = FALSE,
null.ok = TRUE,
add = coll)

checkmate::assert_logical(x = all_records,
len = 1,
any.missing = FALSE,
add = coll)

checkmate::assert_list(x = config,
names = "named",
add = coll)

checkmate::reportAssertions(coll)
checkmate::assert_directory_exists(x = dir,

checkmate::assert_directory_exists(x = dir,
add = coll)


Events <- rcon$events()

Instruments <- rcon$instruments()
checkmate::assert_subset(x = events,

checkmate::assert_subset(x = events,
choices = Events$unique_event_name,
add = coll)

checkmate::assert_subset(x = instruments,
checkmate::assert_subset(x = instruments,
choices = Instruments$instrument_name,
add = coll)

checkmate::reportAssertions(coll)

##################################################################
# Make the Body List
body <- list(content = 'pdf',
returnFormat = 'csv',
allRecords = if (all_records) as.numeric(all_records) else NULL,
record = record,
event = events,

body <- list(content = 'pdf',
returnFormat = 'csv',
allRecords = if (all_records) as.numeric(all_records) else NULL,
record = record,
event = events,
instrument = instruments)

##################################################################
# Call the API

response <- makeApiCall(rcon, body, ...)
filename <-

config[['options']] <- list(followlocation=TRUE) # Necessary for this call

response <- makeApiCall(rcon, body, config=config, ...)
filename <-
if (all_records)
paste0(filename, "_all_records.pdf")
else if (is.null(record))
else if (is.null(record))
paste0(filename, "_blank.pdf")
else
else
paste0(filename, "_record_", record, ".pdf")
reconstituteFileFromExport(response,
dir = dir,
dir_create = FALSE,
file_prefix = "",

reconstituteFileFromExport(response,
dir = dir,
dir_create = FALSE,
file_prefix = "",
filename = filename)

invisible(file.path(dir, filename))
}

Loading
Loading