Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try creating a detailed meta table #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ Imports:
R6,
ows4R,
jsonlite,
magrittr
magrittr,
purrr
Suggests:
testthat,
covr,
Expand Down
15 changes: 15 additions & 0 deletions R/meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
)
}))
}
30 changes: 26 additions & 4 deletions R/wfs-client.R
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand All @@ -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())
}


Expand All @@ -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()))
}
}
7 changes: 6 additions & 1 deletion man/create_aurinapi_wfs_client.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tests/testthat/test-aur_meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})