-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathunarchive_asgmts.R
87 lines (72 loc) · 2.69 KB
/
unarchive_asgmts.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
unarchive_asgmts <- function(ids = NULL, # assignment ID, can be vector
server, # server prefix
user, # API user username
password) # password for API user
{
# -------------------------------------------------------------
# Load all necessary functions and require packages
# -------------------------------------------------------------
load_pkg <- function(x) {
if (!require(x, character.only = TRUE)) {
install.packages(x, repos = 'https://cloud.r-project.org/', dep = TRUE)
}
require(x, character.only = TRUE)
}
load_pkg('dplyr')
load_pkg('jsonlite')
load_pkg('httr')
# -------------------------------------------------------------
# CHECK ALL INPUTS
# -------------------------------------------------------------
# check that server, login, password, and data type are non-missing
for (x in c("server", "user", "password")) {
if (!is.character(get(x))) {
stop(x, "has to be a string.")
}
if (nchar(get(x)) == 0) {
stop(paste("The following parameter is not specified in the program:", x))
}
}
# check if assignment IDs were provided
if (is.null(ids)){
stop("Assignment IDs to unarchive need to be specified.")
}
# check if all assignment IDs are numeric
if (sum(sapply(suppressWarnings({as.numeric(ids)}), is.na)) > 0){
stop("Assignment IDs must be a number.")
} else {
# if all are numeric, convert to numeric vector
ids <- as.numeric(ids)
}
# -------------------------------------------------------------
# Send API request
# -------------------------------------------------------------
# build base URL for API
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")
# function archive one assignment
unarchive_id <- function(x, url=api_url){
# build api endpoint
endpoint <- paste0(url, "/assignments/", x, '/unarchive')
resp <- httr::PATCH(endpoint, authenticate(user, password))
if (httr::status_code(resp)==200){
message("Successfully unarchived assignment #", x)
} else if (httr::status_code(resp)==401){
stop("Invalid login or password.")
} else {
message("Error archiving assignment #", x)
}
}
# unarchive all assignments in list
# invisible to prevent sapply from printing to console
invisible(sapply(ids, unarchive_id))
}