-
Notifications
You must be signed in to change notification settings - Fork 6
/
get_qx.R
97 lines (80 loc) · 3.25 KB
/
get_qx.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# =============================================================================
# Get a list of all the templates imported on the server
# =============================================================================
# NOTE: This will save the information about templates
# currently on the server as a data frame
# Args:
# API_URL: URL for API
# user: API user ID
# password: password for API user
#
# Returns:
# A data frame that has all the information about the imported
# questionnaires on the server. This is a prettified version of the JSON response.
# By default, this puts the questionnaire list onto the global
get_qx <- function(server, user, password, put_global=TRUE) {
load_pkg <- function(x) {
if (!require(x, character.only = TRUE)) {
install.packages(x, repos = 'https://cloud.r-project.org/', dep = TRUE)
}
library(x, character.only = TRUE)
}
# require packages
load_pkg("httr")
load_pkg("jsonlite")
load_pkg("dplyr")
server <- tolower(trimws(server))
# check server exists
server_url <- paste0("https://", server, ".mysurvey.solutions")
# Check server exists
tryCatch(httr::http_error(server_url),
error=function(err) {
err$message <- paste(server, "is not a valid server.")
stop(err)
})
# build base URL for API
api_url <- paste0(server_url, "/api/v1")
# build query
query <- paste0(api_url, "/questionnaires")
# Send GET request to API
data <- httr::GET(query, authenticate(user, password),
query = list(limit = 40, offset = 1))
# If response code is 200, request was succesffuly processed
if (httr::status_code(data)==200) {
# save the list of imported templates from the API as a data frame
qnrList <- jsonlite::fromJSON(content(data, as = "text"), flatten = TRUE)
qnrList_temp <- as.data.frame(qnrList$Questionnaires)
if (qnrList$TotalCount <= 40) {
# if 40 questionnaires or less, then do not need to call again
# Extract information about questionnaires on server
qnrList_all <- dplyr::arrange(qnrList_temp, Title, Version)
} else {
quest_more <- list(qnrList_temp)
# If more than 40 questionnaires, run query again to get the rest
nquery <- ceiling(qnrList$TotalCount/40)
# send query for more questionnaires
for(i in 2:nquery){
data2 <- httr::GET(query, authenticate(user, password),
query = list(limit = 40, offset = i))
qnrList_more <- jsonlite::fromJSON(content(data2, as = "text"),
flatten = TRUE)
questList_more <- as.data.frame(qnrList_more$Questionnaires)
# append loop df to list
quest_more[[i]] <- questList_more
}
qnrList_temp <- dplyr::bind_rows(quest_more)
qnrList_all <- dplyr::arrange(qnrList_temp, Title, Version)
}
if (put_global==TRUE){
# assign to global environment for access by other functions
assign('qnrList_all', qnrList_all, envir = .GlobalEnv)
} else {
return(qnrList_all)
}
} else if (httr::status_code(data) == 401) { # login error
stop("Incorrect username or password.")
} else {
# Issue error message
stop("Encountered issue with status code ", status_code(data))
}
}