From cea45ba9f92c8656fabe2599dd07de7aca376169 Mon Sep 17 00:00:00 2001 From: Simon Date: Fri, 16 Aug 2024 10:09:35 +0200 Subject: [PATCH] meileur version des fonction --- meteofranceapi.R | 77 ++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 41 deletions(-) diff --git a/meteofranceapi.R b/meteofranceapi.R index c81604b..7df47b9 100644 --- a/meteofranceapi.R +++ b/meteofranceapi.R @@ -42,52 +42,47 @@ getStationInfo <- function(station,headers=headers){ } else print("Failed to retrieve data from the API") } -getStationData <- function(start_date, end_date, station_id,headers=headers) { - start_date <- as.Date(start_date) - end_date <- as.Date(end_date) - url <- modify_url(base,path = paste0(dpclim,"commande-station/infrahoraire-6m")) - # Format dates in ISO8601 format - formatted_start_date <- format(start_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) - - # 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){ - - url <- modify_url(base,path = paste0(dpclim,"commande/fichier")) - parameters <- list("id-cmde" = content(response)[[1]][[1]] ) - # 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){ - return(read.csv(text=content(response,as="text")[[1]],sep=";",header=T,dec = ",")) +getStationData <- function(start_date, end_date, station_id, headers = headers) { + url <- modify_url(base, + path = paste0(dpclim, "commande-station/infrahoraire-6m"), + query = list("id-station" = station_id, + "date-deb-periode" = format(as.Date(start_date), "%Y-%m-%dT00:00:00Z", tz = "GMT"), + "date-fin-periode" = format(as.Date(end_date), "%Y-%m-%dT00:00:00Z", tz = "GMT"))) + + response <- GET(url, add_headers(.headers = headers)) + if (response$status_code == 202) { + cmd_url <- modify_url(base, path = paste0(dpclim, "commande/fichier"), query = list("id-cmde" = content(response)[[1]][[1]])) + + repeat { + response <- GET(cmd_url, add_headers(.headers = headers)) + if (response$status_code == 201) { + 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 { + stop(paste("Failed to initiate data retrieval with status code:", response$status_code)) } - else print(paste("Failed with",content(response))) } -getStationPaquet <- function(headers=headersPaquet,id_station,format="csv"){ - url <- modify_url(base,path = paste0(dpPaquetObs,"infrahoraire-6m")) -url_with_params <- modify_url(url, query = list("id_station" = id_station ,"format"=format )) - response <- GET(url_with_params, headersPaquet) - # Check if the request was successful - if (response$status_code == 200) { - # Get the content of the response - return( read.csv(text=content(response, as = "text"),sep=";",header=T)) - } else print("Failed to retrieve data from the API") +getStationPaquet <- function(id_station, format = "csv", headers = headersPaquet) { + base_url <- modify_url(base, path = paste0(dpPaquetObs, "infrahoraire-6m")) + url_with_params <- modify_url(base_url, query = list(id_station = id_station, format = format)) + + response <- GET(url_with_params, add_headers(.headers = headers)) + + if (response$status_code == 200) { + # Successfully retrieved the data + return(read.csv(text = content(response, "text"), sep = ";", header = TRUE)) + } 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