meileur version des fonction
This commit is contained in:
parent
2911f96dd0
commit
cea45ba9f9
1 changed files with 36 additions and 41 deletions
|
|
@ -42,52 +42,47 @@ getStationInfo <- function(station,headers=headers){
|
||||||
} else print("Failed to retrieve data from the API")
|
} else print("Failed to retrieve data from the API")
|
||||||
}
|
}
|
||||||
|
|
||||||
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))
|
||||||
|
if (response$status_code == 202) {
|
||||||
|
cmd_url <- modify_url(base, path = paste0(dpclim, "commande/fichier"), query = list("id-cmde" = content(response)[[1]][[1]]))
|
||||||
|
|
||||||
# Create the complete URL with parameters
|
repeat {
|
||||||
url_with_params <- modify_url(url, query = parameters)
|
response <- GET(cmd_url, add_headers(.headers = headers))
|
||||||
|
if (response$status_code == 201) {
|
||||||
# Add headers
|
return(read.csv(text = content(response, "text"), sep = ";", header = TRUE, dec = ","))
|
||||||
|
} else if (response$status_code == 204) {
|
||||||
# Make the GET request
|
message("File not finished, retrying in 5 seconds...")
|
||||||
response <- GET(url_with_params, headersLIM)
|
Sys.sleep(5)
|
||||||
if(response$status_code == 202){
|
} else {
|
||||||
|
stop(paste("Failed with", http_status(response)$message))
|
||||||
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 = ","))
|
|
||||||
}
|
}
|
||||||
|
} 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"){
|
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) {
|
|
||||||
# Get the content of the response
|
if (response$status_code == 200) {
|
||||||
return( read.csv(text=content(response, as = "text"),sep=";",header=T))
|
# Successfully retrieved the data
|
||||||
} else print("Failed to retrieve data from the API")
|
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
|
# Test the function with dates and station ID
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue