I'm using R to make an API call to a weather data provider to download some weather forecasts. I'm using a free key that allows me to make no more than 10 calls per minute. I've tried using Sys.sleep()
to ensure I don't go over the threshold but the API resource monitor tells me that I've exceeded the number of calls.
For example, if I'm making 6 calls, a time interval of 10 seconds between the calls ought to be sufficient (not taking into account the time R would need).
dat <- list()
for(i in 1:6){
dat[[i]] <- getWeatherData(web_url, api_key, history_date, data_format)
Sys.sleep(10)
web_url <- gsub(i-1, i, url)
}
The getWeatherData
function does the following:
- makes the API call (only one API call is made each time the function is invoked. Uses
httr::GET()
to get the data), - parses the XML output to get desired variables (regulat expressions),
- performs some clean-up (for missing/garbage values),
- converts strings to R date-time objects (
POSIXct
), and - rounds values to the nearest hour (
lubridate::round_date()
).
Function inputs:
web_url
is a custom url,api_key
is my personal key,history_date
is a string (formatted as"%d/%m/%Y %H:%M:%S"
), anddata_format
specifies if I want an .XML or .json file as output.
I can not share the url/key for obvious reasons. As soon as I run this, I get a notification from the data provider that I've exceeded the allowable calls per minute (10). I don't get a notification every time - not sure why that is either.
Any help is appreciated!