-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.R
60 lines (50 loc) · 1.37 KB
/
utils.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
#' HTTP Request Function
#'
#' Internal function for making http requests.
#' @param url character. Base url.
#' @param path character
#' @param args query argument list
#' @param accept character. One of \code{c("csv", "json")}
#' @param opts curl options to crul::HttpClient. Must be a list.
#'
#' @return Parsed json content or csv
#' @importFrom crul HttpClient
#' @keywords internal
#' @noRd
get_download <- function(url,
path,
args = list(),
accept = "csv",
opts = list()) {
if(accept == "csv") {
headers = list(Accept = "text/csv")
} else {
if(accept == "json") {
headers = list(Accept = "application/json")
}
}
cli <- crul::HttpClient$new(url = url,
headers = headers,
opts = opts)
res <- cli$get(path,
query = args)
errs(res)
content <- res$parse("UTF-8")
attr(content, 'url') <- res$url
return(content)
}
#' Gracefully return http errors
#'
#' Internal function for returning http error message when making http requests.
#' @param x http request
#'
#' @return error message or nothing
#' @keywords internal
#' @noRd
#' @importFrom fauxpas find_error_class
errs <- function(x) {
if (x$status_code > 201) {
fun <- fauxpas::find_error_class(x$status_code)$new()
fun$do_verbose(x)
}
}