Commit ead2b9b5 authored by Dorchies David's avatar Dorchies David
Browse files

fix(get_hydrometrie_observations_tr): Error on empty response

Fix #13
parent f146a9e4
No related merge requests found
Pipeline #27664 failed with stages
in 1 minute and 25 seconds
Showing with 28 additions and 19 deletions
+28 -19
...@@ -88,8 +88,10 @@ doApiQuery <- function(api, ...@@ -88,8 +88,10 @@ doApiQuery <- function(api,
if (as.numeric(l$count) > 20000) { if (as.numeric(l$count) > 20000) {
stop( stop(
"The request reach the API limitation of 20000 records.\n", "The request reach the API limitation of 20000 records.\n",
"Use filter arguments to reduce the number of records of your request." "Use filter arguments to reduce the number of records of your query."
) )
} else if(as.numeric(l$count) == 0) {
return(NULL)
} }
data <- c(data, l$data) data <- c(data, l$data)
if (resp$status_code == 206) { if (resp$status_code == 206) {
......
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
#' @param entities 1-[character] string filtering the rows of the returned value, possible values are: "station" for filtering on station rows, "site" for filtering on site rows, "both" for keeping all the rows #' @param entities 1-[character] string filtering the rows of the returned value, possible values are: "station" for filtering on station rows, "site" for filtering on site rows, "both" for keeping all the rows
#' #'
#' @return a [tibble::tibble] with all available parameters in columns and one row by time step and by station. #' @return a [tibble::tibble] with all available parameters in columns and one row by time step and by station.
#' If the query returns no records then the returned value is [NULL].
#'
#' @export #' @export
#' #'
#' @examples #' @examples
...@@ -16,27 +18,33 @@ get_hydrometrie_observations_tr <- function(params, ...@@ -16,27 +18,33 @@ get_hydrometrie_observations_tr <- function(params,
entities = "station", entities = "station",
cfg = config::get(file = system.file("config.yml", cfg = config::get(file = system.file("config.yml",
package = "hubeau"))) { package = "hubeau"))) {
# Checks
if(!entities %in% c("station", "site", "both")) {
stop("Argument 'entities' must be one of these values: 'station', 'site', 'both'")
}
l <- doApiQuery( l <- doApiQuery(
api = "hydrometrie", api = "hydrometrie",
operation = "observations_tr", operation = "observations_tr",
params = params, params = params,
cfg = cfg cfg = cfg
) )
l <- lapply(l, function(x) { if(!is.null(l)) {
x$geometry <- NULL l <- lapply(l, function(x) {
if (entities == "station") { x$geometry <- NULL
if (is.null(x$code_station)) { if (entities == "station") {
return(NULL) if (is.null(x$code_station)) {
} return(NULL)
} else if (entities == "site") { }
if (!is.null(x$code_station)) { } else if (entities == "site") {
return(NULL) if (!is.null(x$code_station)) {
return(NULL)
}
} }
} else if (entities != "both") { return(x)
stop("Argument 'entities' must be one of these values: 'station', 'site', 'both'") })
} l[sapply(l, is.null)] <- NULL
return(x) l <- convert_list_to_tibble(l)
}) }
l[sapply(l, is.null)] <- NULL return(l)
convert_list_to_tibble(l)
} }
params <- list(code_entite = "H0203020", params <- list(bbox = "1.6,47.79,1.8,47.99",
date_debut_obs = format(Sys.Date()-3), date_debut_obs = format(Sys.Date()-3),
grandeur_hydro = "Q") grandeur_hydro = "Q")
...@@ -9,7 +9,6 @@ test_that("entities not in ('station', 'site', 'both') should throw an error", { ...@@ -9,7 +9,6 @@ test_that("entities not in ('station', 'site', 'both') should throw an error", {
) )
}) })
test_that("`entities = 'station'` => 'code_station' must be always not NA", { test_that("`entities = 'station'` => 'code_station' must be always not NA", {
skip_on_cran() skip_on_cran()
df <- get_hydrometrie_observations_tr(params, entities = "station") df <- get_hydrometrie_observations_tr(params, entities = "station")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment