meileur version des fonction

This commit is contained in:
Simon 2024-08-16 10:09:35 +02:00
parent 2911f96dd0
commit cea45ba9f9

View file

@ -43,51 +43,46 @@ getStationInfo <- function(station,headers=headers){
} }
getStationData <- function(start_date, end_date, station_id, headers = headers) { getStationData <- function(start_date, end_date, station_id, headers = headers) {
start_date <- as.Date(start_date) url <- modify_url(base,
end_date <- as.Date(end_date) path = paste0(dpclim, "commande-station/infrahoraire-6m"),
url <- modify_url(base,path = paste0(dpclim,"commande-station/infrahoraire-6m")) query = list("id-station" = station_id,
# Format dates in ISO8601 format "date-deb-periode" = format(as.Date(start_date), "%Y-%m-%dT00:00:00Z", tz = "GMT"),
formatted_start_date <- format(start_date, "%Y-%m-%dT00:00:00Z", tz = "GMT") "date-fin-periode" = format(as.Date(end_date), "%Y-%m-%dT00:00:00Z", tz = "GMT")))
formatted_end_date <- format(end_date, "%Y-%m-%dT00:00:00Z", tz = "GMT")
parameters <- list("id-station" = station_id, "date-deb-periode" = formatted_start_date, "date-fin-periode" = formatted_end_date) response <- GET(url, add_headers(.headers = headers))
# Create the complete URL with parameters
url_with_params <- modify_url(url, query = parameters)
# Add headers
# Make the GET request
response <- GET(url_with_params, headersLIM)
if (response$status_code == 202) { if (response$status_code == 202) {
cmd_url <- modify_url(base, path = paste0(dpclim, "commande/fichier"), query = list("id-cmde" = content(response)[[1]][[1]]))
url <- modify_url(base,path = paste0(dpclim,"commande/fichier")) repeat {
parameters <- list("id-cmde" = content(response)[[1]][[1]] ) response <- GET(cmd_url, add_headers(.headers = headers))
# Create the complete URL with parameters
url_with_params <- modify_url(url, query = parameters)
response <- GET(url_with_params, headersLIM)
while(response$status_code == 204){
response <- GET(url_with_params, headersLIM)
print("file not finisehed")
Sys.sleep(5)
}
if (response$status_code == 201) { if (response$status_code == 201) {
return(read.csv(text=content(response,as="text")[[1]],sep=";",header=T,dec = ",")) return(read.csv(text = content(response, "text"), sep = ";", header = TRUE, dec = ","))
} else if (response$status_code == 204) {
message("File not finished, retrying in 5 seconds...")
Sys.sleep(5)
} else {
stop(paste("Failed with", http_status(response)$message))
} }
} }
else print(paste("Failed with",content(response))) } else {
stop(paste("Failed to initiate data retrieval with status code:", response$status_code))
}
} }
getStationPaquet <- function(headers=headersPaquet,id_station,format="csv"){ getStationPaquet <- function(id_station, format = "csv", headers = headersPaquet) {
url <- modify_url(base,path = paste0(dpPaquetObs,"infrahoraire-6m")) base_url <- modify_url(base, path = paste0(dpPaquetObs, "infrahoraire-6m"))
url_with_params <- modify_url(url, query = list("id_station" = id_station ,"format"=format )) url_with_params <- modify_url(base_url, query = list(id_station = id_station, format = format))
response <- GET(url_with_params, headersPaquet)
# Check if the request was successful response <- GET(url_with_params, add_headers(.headers = headers))
if (response$status_code == 200) { if (response$status_code == 200) {
# Get the content of the response # Successfully retrieved the data
return( read.csv(text=content(response, as = "text"),sep=";",header=T)) return(read.csv(text = content(response, "text"), sep = ";", header = TRUE))
} else print("Failed to retrieve data from the API") } else {
# If the request failed, it's useful to provide the status code in the error message for debugging
stop(sprintf("Failed to retrieve data from the API. Status code: %s", response$status_code))
}
} }
# Test the function with dates and station ID # Test the function with dates and station ID