beaumontmeteo/R/getStationsList.R
2026-04-11 09:24:56 +01:00

18 lines
830 B
R

getStations <- function(headers = headers,dpobs = "public/DPObs/v1/") {
# Ensure dpobs is defined externally or provide a default/argument for it
url <- httr::modify_url(base, path = paste0(dpobs, "liste-stations"))
response <- httr::GET(url, httr::add_headers(.headers = headers))
# Better error handling
if (httr::http_status(response)$category == "Success") {
# Directly parse the content as a CSV
data <- httr::content(response, as = "text")
# Return the data frame
return(read.csv(text = data, sep = ";", header = TRUE))
} else {
# Optionally: return an informative error message or an empty data frame
# stop("Failed to retrieve data from the API"), or
print("Failed to retrieve data from the API")
return(data.frame()) # Returns an empty data frame as a graceful fallback
}
}