Skip to content

Commit dd0970b

Browse files
authored
Merge pull request #99 from cmu-delphi/ds/fetch
Consolidate `fetch` interfaces
2 parents 9655cd9 + 9e97e72 commit dd0970b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+731
-500
lines changed

DESCRIPTION

+9-2
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,18 @@ Imports:
2727
readr,
2828
MMWRweek,
2929
rlang,
30-
magrittr
30+
magrittr,
31+
tibble,
32+
xml2
3133
RoxygenNote: 7.2.3
3234
Suggests:
35+
dplyr,
3336
knitr,
37+
mockery,
38+
mockr,
3439
rmarkdown,
35-
testthat (>= 3.1.5)
40+
testthat (>= 3.1.5),
41+
withr
3642
VignetteBuilder: knitr
3743
Language: en-US
44+
Config/testthat/edition: 3

NAMESPACE

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ export(delphi)
1818
export(dengue_nowcast)
1919
export(ecdc_ili)
2020
export(epirange)
21-
export(fetch_classic)
22-
export(fetch_csv)
23-
export(fetch_json)
24-
export(fetch_tbl)
21+
export(fetch)
2522
export(flusurv)
2623
export(fluview)
2724
export(fluview_clinical)
@@ -34,7 +31,6 @@ export(nidss_dengue)
3431
export(nidss_flu)
3532
export(nowcast)
3633
export(paho_dengue)
37-
export(parse_timeset_input)
3834
export(pvt_cdc)
3935
export(pvt_dengue_sensors)
4036
export(pvt_ght)
@@ -43,7 +39,6 @@ export(pvt_norostat)
4339
export(pvt_quidel)
4440
export(pvt_sensors)
4541
export(pvt_twitter)
46-
export(request_url)
4742
export(wiki)
4843
export(with_base_url)
4944
importFrom(MMWRweek,MMWRweek2Date)
@@ -64,9 +59,14 @@ importFrom(checkmate,test_list)
6459
importFrom(httr,RETRY)
6560
importFrom(httr,content)
6661
importFrom(httr,http_error)
62+
importFrom(httr,http_type)
6763
importFrom(httr,modify_url)
6864
importFrom(httr,stop_for_status)
6965
importFrom(jsonlite,fromJSON)
7066
importFrom(magrittr,"%>%")
7167
importFrom(readr,read_csv)
7268
importFrom(rlang,abort)
69+
importFrom(tibble,as_tibble)
70+
importFrom(xml2,read_html)
71+
importFrom(xml2,xml_find_all)
72+
importFrom(xml2,xml_text)

R/auth.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#' Get the API key
22
#'
3-
#' Get the API key from the environment variable DELPHI_EPIDATA_KEY or getOption("delphi.epidata.key")
3+
#' Get the API key from the environment variable `DELPHI_EPIDATA_KEY` or
4+
#' `getOption("delphi.epidata.key")`.
45
#'
5-
#' @param verbose Will print the source of the key if TRUE (e.g. options, environment, or config file)
66
#' @return The API key
77
#'
88
#' @export

R/check.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ assert_date_param <- function(name, value, len = NULL, required = TRUE) {
2828
)
2929
}
3030

31-
#' Allows a timeset param: a date vector, a character vector, an integer-like vector, or a single EpiRange
31+
#' Allows a timeset param: a date vector, a character vector, an integer-like
32+
#' vector, or a single EpiRange
3233
#' @importFrom checkmate assert check_character check_date check_integerish check_class check_list check_names
3334
assert_timeset_param <- function(name, value, len = NULL, required = TRUE) {
3435
null.ok <- !required

R/constants.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
version <- "1.0.0"
1+
version <- "0.5.0"
22
http_headers <- httr::add_headers("User-Agent" = paste0("epidatr/", version), "Accept-Encoding" = "gzip")
33
global_base_url <- "https://api.delphi.cmu.edu/epidata/"

R/covidcast.R

+19-6
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,33 @@ print.covidcast_data_source <- function(source, ...) {
100100
#' print(smoothed_cli)
101101
#' df <- smoothed_cli$call("nation", "us", epirange(20210405, 20210410))
102102
#' @param base_url optional alternative API base url
103-
#' @importFrom httr RETRY stop_for_status content
103+
#' @importFrom httr stop_for_status content http_type
104104
#' @importFrom jsonlite fromJSON
105+
#' @importFrom xml2 read_html xml_find_all xml_text
105106
#' @return an instance of covidcast_epidata
106107
#'
107108
#' @export
108109
covidcast_epidata <- function(base_url = global_base_url) {
109110
url <- join_url(base_url, "covidcast/meta")
110-
res <- do_request(url, list())
111+
response <- do_request(url, list())
111112

112-
httr::stop_for_status(res)
113-
r <- httr::content(res, "text", encoding = "UTF-8")
114-
meta <- jsonlite::fromJSON(r, simplifyVector = FALSE)
113+
if (response$status_code != 200) {
114+
# 500, 429, 401 are possible
115+
msg <- "fetch data from API"
116+
if (httr::http_type(response) == "text/html") {
117+
# grab the error information out of the returned HTML document
118+
msg <- paste(msg, ":", xml2::xml_text(xml2::xml_find_all(
119+
xml2::read_html(content(response, "text")),
120+
"//p"
121+
)))
122+
}
123+
httr::stop_for_status(response, task = msg)
124+
}
125+
126+
response_content <- httr::content(response, "text", encoding = "UTF-8")
127+
response_content <- jsonlite::fromJSON(response_content, simplifyVector = FALSE)
115128

116-
sources <- do.call(c, lapply(meta, parse_source, base_url = base_url))
129+
sources <- do.call(c, lapply(response_content, parse_source, base_url = base_url))
117130
class(sources) <- c("covidcast_data_source_list", class(sources))
118131

119132
all_signals <- do.call(c, lapply(sources, function(x) {

0 commit comments

Comments
 (0)