diff --git a/R/doApiQuery.R b/R/doApiQuery.R index 63eae6895b415d594159fc9755b0ad48bc1ee96c..d3f58aee338536833810bf0bf7d36c0c6568d122 100644 --- a/R/doApiQuery.R +++ b/R/doApiQuery.R @@ -86,8 +86,10 @@ doApiQuery <- function(api, } else { l <- httr::content(resp, "parsed") if (as.numeric(l$count) > 20000) { - stop("The request reach the API limitation of 20000 records.\n", - "Use filter arguments to reduce the number of records of your request.") + stop( + "The request reach the API limitation of 20000 records.\n", + "Use filter arguments to reduce the number of records of your request." + ) } data <- c(data, l$data) if (resp$status_code == 206) { @@ -104,3 +106,24 @@ doApiQuery <- function(api, } return(data) } + + +#' Convert list provided by the API into a tibble +#' +#' @param l a [list] provided by the API +#' +#' @return a [tibble:tibble] with one row by list item and one column by list sub-item +#' @noRd +#' +convert_list_to_tibble <- function(l) { + l <- + lapply(l, function(row) { + lapply(row, function(cell) { + if (is.null(unlist(cell))) + NA + else + unlist(cell) + }) + }) + return(purrr::map_df(l, tibble::as_tibble)) +} diff --git a/R/get_indicateurs_services_communes.R b/R/get_indicateurs_services_communes.R index eb744a2c2de2f25fa6d0ecb79f2d7eb79a2c0a67..7cb17c8e07ba7f15bac3f96956c7fa686877b0eb 100644 --- a/R/get_indicateurs_services_communes.R +++ b/R/get_indicateurs_services_communes.R @@ -32,6 +32,5 @@ get_indicateurs_services_communes <- function(params, x$indicateurs <- NULL x }) - l <- lapply(l, function(row) {lapply(row, function(cell) { if(is.null(unlist(cell))) NA else unlist(cell) })}) - return(purrr::map_df(l, tibble::as_tibble)) + convert_list_to_tibble(l) } diff --git a/R/get_indicateurs_services_indicateurs.R b/R/get_indicateurs_services_indicateurs.R index b8497a0ab1f8252194e5931740e5e27e2055e982..675a06a5d07ff0fbd91ecfb6af754cf9478a1281 100644 --- a/R/get_indicateurs_services_indicateurs.R +++ b/R/get_indicateurs_services_indicateurs.R @@ -26,12 +26,10 @@ get_indicateurs_services_indicateurs <- function(params, params = params, cfg = cfg) - li <- lapply(l, function(x) { + l <- lapply(l, function(x) { x$codes_commune <- NULL x$noms_commune <- NULL x }) - li <- lapply(li, function(row) {lapply(row, function(cell) { if(is.null(unlist(cell))) NA else unlist(cell) })}) - df <- purrr::map_df(li, tibble::as_tibble) - return() + convert_list_to_tibble(l) } diff --git a/R/get_prelevements_chroniques.R b/R/get_prelevements_chroniques.R index dd526e02c4f248b315b189a83d8370a9941efbf7..9a7983f37fefebc54ae2b4fcd2ed6ef0aaac75e8 100644 --- a/R/get_prelevements_chroniques.R +++ b/R/get_prelevements_chroniques.R @@ -21,6 +21,5 @@ get_prelevements_chroniques <- function(params, cfg = config::get(file = system. x$geometry <- NULL x }) - l <- lapply(l, function(row) {lapply(row, function(cell) { if(is.null(unlist(cell))) NA else unlist(cell) })}) - return(purrr::map_df(l, tibble::as_tibble)) + convert_list_to_tibble(l) } diff --git a/R/get_prelevements_ouvrages.R b/R/get_prelevements_ouvrages.R index f6826fa9fd011d0ba4d44ec9a930bdabc83e7cfc..46c7e028add9b6f783e7709860e135db1d63d4c6 100644 --- a/R/get_prelevements_ouvrages.R +++ b/R/get_prelevements_ouvrages.R @@ -23,6 +23,5 @@ get_prelevements_ouvrages <- function(params, cfg = config::get(file = system.fi x }) - l <- lapply(l, function(row) {lapply(row, function(cell) { if(is.null(unlist(cell))) NA else unlist(cell) })}) - return(purrr::map_df(l, tibble::as_tibble)) + convert_list_to_tibble(l) } diff --git a/R/get_prelevements_points_prelevement.R b/R/get_prelevements_points_prelevement.R index d7fa0745f0901265f3cf88ce29dc83d58e913794..b23b31470a69a2572884b16d59d30d3458c212a6 100644 --- a/R/get_prelevements_points_prelevement.R +++ b/R/get_prelevements_points_prelevement.R @@ -18,6 +18,5 @@ get_prelevements_points_prelevement <- function(params, cfg = config::get(file = params = params, cfg = cfg) - l <- lapply(l, function(row) {lapply(row, function(cell) { if(is.null(unlist(cell))) NA else unlist(cell) })}) - return(purrr::map_df(l, tibble::as_tibble)) + convert_list_to_tibble(l) } diff --git a/man/get_prelevements_chroniques.Rd b/man/get_prelevements_chroniques.Rd index 9d76894d07141533f7991f1def7cb60c707edbec..3fac8cd0f6c3862087a17fe0c0cf814fae488f1d 100644 --- a/man/get_prelevements_chroniques.Rd +++ b/man/get_prelevements_chroniques.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/get_prelevements_chroniques.R \name{get_prelevements_chroniques} \alias{get_prelevements_chroniques} -\title{Retrieve time series of withdrawals from Hub'Eau API} +\title{Retrieve time series of withdrawals from API "Prélèvements en eau"} \usage{ get_prelevements_chroniques( params, @@ -19,7 +19,7 @@ configuration} a \link[tibble:tibble]{tibble::tibble} with all available parameters in columns and one row by device, year and usage. } \description{ -See the API documentation for available filter parameters: \url{https://hubeau.eaufrance.fr/page/api-prelevements-eau#/prelevements/chronique} +See the API documentation for available filter parameters: \url{https://hubeau.eaufrance.fr/page/api-prelevements-eau} } \examples{ # For retrieving the withdrawal time series of the devices located in Romilly-sur-Seine