My R code (see below) generates these errors in some cases:
[1] "2023-08-12 16:47:37.463"
Error in curl::curl_fetch_memory(url, handle = handle): Could not resolve host: api.abc.com
Request failed [ERROR]. Retrying in 1.3 seconds...
Error in curl::curl_fetch_memory(url, handle = handle): Could not resolve host: api.abc.com
Request failed [ERROR]. Retrying in 1 seconds...
Error in curl::curl_fetch_memory(url, handle = handle):
Could not resolve host: api.abc.com
api.abc.com is not the original API I use. I use a commercial API which noticed me their server was not down at the particular moment above. In some cases when the server was down it returned http-code 503.
I have two questions:
- what can be the cause of these errors?
- how can I keep my script below continue running in cases with these errors? Currently it breaks after these error messages. I was not expecting this since I use
RETRY
in my code withGET
.
My code below is called every 10 seconds with the scheduler tclTaskSchedule
(see end of code). In this examplecode I have used a free API (universities.hipolabs.com) as example.
library(httr) # accessing API's'
library(jsonlite) # JSON parsing
library(dplyr)
library(readr)
library(purrr)
library(tidyr)
library(stringr)
library(tibble)
library(tcltk2)
library(lubridate)
run_api_once <- function() {
mydatalist <- list() #create an empty list
my_next_page_with_number <- "http://universities.hipolabs.com/search?country=United+States"
mydata1 <- RETRY("GET", my_next_page_with_number)
if(mydata1$status_code != 200){
print(mydata1$status_code)
http_responses <<- append(http_responses, paste(mydata1$status_code, Sys.time()))
has_more_pages <- FALSE
} else {
rawdata <- rawToChar(mydata1$content)
mydata2 <- fromJSON(rawdata, flatten = FALSE, simplifyVector = FALSE)
mydata <- mydata2
mydatalist <- c(mydatalist, mydata)
}
y <- Sys.time()
y <- format(y, "%Y-%m-%d %H:%M")
print(y)
users <- tibble(user = mydatalist)
myvar <<- users %>% unnest_wider(user)
return(myvar)
}
# call function every 10 seconds:
tclTaskSchedule(10000, run_api_once(), id = "run_api_once", redo = TRUE)
# end session:
tclTaskDelete(NULL)
I suppose it is irrelevant, although for completeness: I stream the content of myvar to a local server on my pc with Plumber. See code below:
# stream df myvar to local api at port 8405:
library(plumber)
pr("D:/plumber_universities2test.R") %>%
# pr("C:/plumber_universities2test.R") %>%
pr_run(port=8405)
Which calls this script:
library(plumber)
library(dplyr)
#* @param symbol Ticker symbol (just to input something in the function)
#* @get /return
#* @serializer json list(na="string")
universities_data <- function(symbol) {
data <- myvar
data
}
Thanks a lot!