From 78c8c13ecabe55918521989ebfcd05fff66bd61e Mon Sep 17 00:00:00 2001
From: Dorchies David <david.dorchies@inrae.fr>
Date: Thu, 5 Aug 2021 16:08:17 +0200
Subject: [PATCH] refactor: use internal function convert_list_to_tibble

---
 R/doApiQuery.R                           | 27 ++++++++++++++++++++++--
 R/get_indicateurs_services_communes.R    |  3 +--
 R/get_indicateurs_services_indicateurs.R |  6 ++----
 R/get_prelevements_chroniques.R          |  3 +--
 R/get_prelevements_ouvrages.R            |  3 +--
 R/get_prelevements_points_prelevement.R  |  3 +--
 man/get_prelevements_chroniques.Rd       |  4 ++--
 7 files changed, 33 insertions(+), 16 deletions(-)

diff --git a/R/doApiQuery.R b/R/doApiQuery.R
index 63eae68..d3f58ae 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 eb744a2..7cb17c8 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 b8497a0..675a06a 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 dd526e0..9a7983f 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 f6826fa..46c7e02 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 d7fa074..b23b314 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 9d76894..3fac8cd 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
-- 
GitLab