Skip to content

Commit 429ed54

Browse files
committed
add try_request
1 parent dbb41bf commit 429ed54

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

DESCRIPTION

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Description: A wrapper around the 'COVID Tracking Project API'
1111
in the US.
1212
License: MIT + file LICENSE
1313
Imports:
14+
curl (>= 4.3),
1415
dplyr (>= 0.8.3),
1516
glue (>= 1.3.1),
1617
httr (>= 1.4.1),

R/get.R

+22-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ get_states_daily <- function(state = "all", date = "all") {
6363
#' get_states_info()
6464
#' }
6565
get_states_info <- function() {
66-
get("states/info") %>%
66+
tbl <- get("states/info")
67+
68+
if (nrow(tbl) == 0) {
69+
return(tbl)
70+
}
71+
72+
tbl %>%
6773
select(
6874
state, name,
6975
everything()
@@ -95,7 +101,14 @@ get_us_current <- function() {
95101
#' get_us_daily()
96102
#' }
97103
get_us_daily <- function() {
98-
get("us/daily") %>%
104+
105+
tbl <- get("us/daily")
106+
107+
if (nrow(tbl) == 0) {
108+
return(tbl)
109+
}
110+
111+
tbl %>%
99112
rename(
100113
n_states = states
101114
) %>%
@@ -129,7 +142,13 @@ get_counties_info <- function() {
129142
#' get_tracker_urls()
130143
#' }
131144
get_tracker_urls <- function() {
132-
get("urls") %>%
145+
tbl <- get("urls")
146+
147+
if (nrow(tbl) == 0) {
148+
return(tbl)
149+
}
150+
151+
tbl %>%
133152
rename(
134153
state_name = name
135154
) %>%

R/utils.R

+15-1
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,23 @@ request <- function(url) {
6363
)
6464
}
6565

66+
try_request <- purrr::possibly(
67+
request,
68+
otherwise = tibble(),
69+
quiet = FALSE
70+
)
71+
6672
get <- function(endpoint, query = "") {
6773
url <- glue::glue("{base_url}{endpoint}{query}")
68-
request(url)
74+
75+
have_internet <- curl::has_internet()
76+
77+
if (!have_internet) {
78+
message("No internet connection.")
79+
return(tibble())
80+
}
81+
82+
try_request(url)
6983
}
7084

7185
replace_null <- function(x) {

0 commit comments

Comments
 (0)