-
Notifications
You must be signed in to change notification settings - Fork 633
/
Copy pathprocess.R
41 lines (36 loc) · 1.39 KB
/
process.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
# ----------------------------------------------------------------------------
# Methods for processing API responses
# ----------------------------------------------------------------------------
process <- function(resp) {
UseMethod("process")
}
process.clientresp <- function(resp) {
httr::stop_for_status(resp)
con <- from_JSON(httr::content(resp, as = "text"))
if (nchar(con$error) > 0) stop(con$error, call. = FALSE)
if (nchar(con$warning) > 0) warning(con$warning, call. = FALSE)
if (nchar(con$message) > 0) message(con$message, call. = FALSE)
con
}
process.image <- function(resp) {
httr::stop_for_status(resp)
# httr (should) know to call png::readPNG() which returns raster array
tryCatch(httr::content(resp, as = "parsed"),
error = function(e) httr::content(resp, as = "raw"))
}
process.figure <- function(resp) {
httr::stop_for_status(resp)
con <- from_JSON(content(resp, as = "text"))
fig <- con$payload$figure
fig$url <- sub("apigetfile/", "~", resp$url)
# any reasonable way to return a data frame?
hash_plot(data.frame(), fig)
}
process.signup <- function(resp) {
httr::stop_for_status(resp)
con <- from_JSON(content(resp, as = "text"))
if (nchar(con[["error"]]) > 0) stop(con$error, call. = FALSE)
# Relaying a message with a private key probably isn't a great idea --
# https://github.com/ropensci/plotly/pull/217#issuecomment-100381166
con
}