-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrt.R
107 lines (92 loc) · 2.71 KB
/
rt.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
98
99
100
101
102
103
104
105
106
107
#' Generate an RT API URL
#'
#' Create an RT API URL based on the server URL and any arguments provided
#'
#' @param rt_base_url (character) The base URL that hosts RT for your organization
#' @param ... Other parameters
#'
rt_url <- function(rt_base_url, ...) {
stopifnot(nchar(rt_base_url) > 0)
paste(gsub("\\/$", "", rt_base_url), # Removes trailing slash from base URL just in case
"REST",
"1.0",
paste(c(...), collapse = "/"),
sep = "/")
}
#' Compact list.
#'
#' Remove all NULL entries from a list. From \code{plyr::compact()}.
#'
#' @param l list
compact <- function(l) Filter(Negate(is.null), l)
#' Parse an RT response
#'
#' Parse an RT response
#'
#' @param resp_cont (character) The RT ticket response content
#'
parse_ticket <- function(resp_cont) {
#clean/split response
resp_split <- resp_cont %>%
stringr::str_replace_all("^RT.*Ok\n|\n\n$", "") %>% #remove headers/footers
stringr::str_split("\\n--\\n") %>% #split if multiple tickets/etc displayed
unlist(recursive = FALSE)
resp_processed <- tryCatch(
lapply(resp_split,
function(.x){
tibble::tibble(fields = stringr::str_extract_all(.x, "\n[^: ]+:")[[1]] %>%
str_replace_all("\\n|:", "") %>%
trimws(),
values = stringr::str_split(.x, "\n[^: ]+:")[[1]][-1] %>%
trimws()) %>%
tidyr::spread(fields, values)
}),
error = function(e) {NULL})
out <- dplyr::bind_rows(resp_processed)
if(nrow(out) == 0){
return(resp_split)
warning("The response could not be parsed into a tabular format")
} else {
return(out)
}
}
#' Get an RT response
#'
#' Get an RT response and format it into an S3 object
#'
#' @param url (character) The full RT URL
#'
rt_GET <- function(url) {
resp <- httr::GET(url, httr::user_agent("http://github.com/nceas/rt"))
if (httr::http_type(resp) != "text/plain") {
stop("API did not return text/plain", call. = FALSE)
}
resp_cont <- httr::content(resp)
# Since API does not return failure codes; need to parse content strings to check for errors
if (!is.na(resp_cont) & stringr::str_detect(resp_cont, "does not exist|Invalid")) {
stop(
sprintf(
"RT API request failed [%s]\n%s",
httr::status_code(resp),
resp_cont
),
call. = FALSE
)
}
if(class(resp_cont) != "raw"){
resp_cont <- parse_ticket(resp_cont)
}
structure(
list(
content = resp_cont,
path = url,
response = resp
),
class = "rt_api"
)
}
print.rt_api <- function(x, ...) {
cat("<RT ", x$path, ">\n", sep = "")
utils::str(x$content) #is this better than print(x$content)?
invisible(x)
}