diff --git a/DESCRIPTION b/DESCRIPTION index c24fe5a..eed3b0e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -24,7 +24,8 @@ Imports: R6, ows4R, jsonlite, - magrittr + magrittr, + purrr Suggests: testthat, covr, diff --git a/R/meta.R b/R/meta.R index d43c99d..1565d8c 100644 --- a/R/meta.R +++ b/R/meta.R @@ -18,3 +18,18 @@ aur_meta <- function(force = FALSE) { names(meta)[names(meta) == "name"] <- "aurin_open_api_id" return(meta) } + +create_meta_table <- function() { + wfs_client <- create_aurinapi_wfs_client() + fts <- wfs_client$getFeatureTypes(pretty = FALSE) + do.call("rbind", lapply(fts, function(x) { + tibble::tibble( + aurin_open_api_id = x$getName(), + title = x$getTitle() %>% trimws(), + keywords = list(x$getKeywords() %>% trimws()), + abstract = x$getAbstract(), + bbox = list(x$getBoundingBox()), + features = list(x$getDescription() %>% purrr::map_chr(~ .x$getName())) + ) + })) +} \ No newline at end of file diff --git a/R/wfs-client.R b/R/wfs-client.R index 29301d6..5ec0583 100644 --- a/R/wfs-client.R +++ b/R/wfs-client.R @@ -4,10 +4,15 @@ #' #' This function creates a WFS client. #' +#' @param n_try maximum number of tries creating a WFS client. +#' @param sec_wait seconds wait before trying again. +#' #' @return a `ows4R::WFSClient` R6 object. #' @export -create_aurinapi_wfs_client <- function() { +create_aurinapi_wfs_client <- function(n_try = 5, sec_wait = 2) { stop_if_no_aurin_api_userpwd() + checkmate::assert_count(n_try, positive = TRUE) + checkmate::assert_count(sec_wait, positive = TRUE) if (checkmate::test_r6(get_aurinapi_wfs_client(), classes = "WFSClient")) { return(get_aurinapi_wfs_client()) } @@ -21,8 +26,25 @@ create_aurinapi_wfs_client <- function() { request <- httr::build_url(url) cli::cli_alert_info("Creating AURIN WFS Client...") client_wrapper <- get_aurinapi_wfs_client_wrapper() - client_wrapper$client <- ows4R::WFSClient$new(wfs, serviceVersion = "2.0.0") - return(get_aurinapi_wfs_client()) + n <- 1 + while (is.null(client_wrapper$client) && n_try >= n) { + cli::cli_alert_info("Try number {n} of {n_try}.") + try( + client_wrapper$client <- ows4R::WFSClient$new(wfs, serviceVersion = "2.0.0") + ) + if (is.null(client_wrapper$client)) { + cli::cli_alert_warning("Failed to create a WFS client :(.") + if (n_try == n) { + stop("Failed to create a WFS client.") + } + n <- n + 1 + cli::cli_alert_info("Stopping for {sec_wait} seconds before the next try..") + Sys.sleep(sec_wait) + } else { + cli::cli_alert_success("Success! 🦘") + } + } + invisible(get_aurinapi_wfs_client()) } @@ -36,4 +58,4 @@ get_aurinapi_wfs_client <- function() { #' @rdname create_aurinapi_wfs_client get_aurinapi_wfs_client_wrapper <- function() { get("aurinapi_wfs_client_wrapper", envir = parent.env(environment())) -} +} \ No newline at end of file diff --git a/man/create_aurinapi_wfs_client.Rd b/man/create_aurinapi_wfs_client.Rd index e81c69c..2f4049f 100644 --- a/man/create_aurinapi_wfs_client.Rd +++ b/man/create_aurinapi_wfs_client.Rd @@ -6,12 +6,17 @@ \alias{get_aurinapi_wfs_client_wrapper} \title{Create a AURIN WFS client object.} \usage{ -create_aurinapi_wfs_client() +create_aurinapi_wfs_client(n_try = 5, sec_wait = 2) get_aurinapi_wfs_client() get_aurinapi_wfs_client_wrapper() } +\arguments{ +\item{n_try}{maximum number of tries creating a WFS client.} + +\item{sec_wait}{seconds wait before trying again.} +} \value{ a \code{ows4R::WFSClient} R6 object. } diff --git a/tests/testthat/test-aur_meta.R b/tests/testthat/test-aur_meta.R index d0ead1d..7ed39d4 100644 --- a/tests/testthat/test-aur_meta.R +++ b/tests/testthat/test-aur_meta.R @@ -3,3 +3,7 @@ test_that("aur_meta works", { meta <- aur_meta() checkmate::expect_data_frame(meta, min.rows = 1000, ncols = 2) }) + +test_that("create_meta_table", { + checkmate::expect_data_frame(create_meta_table()) +}) \ No newline at end of file