Skip to content

Commit

Permalink
refactor: rm usethis dep (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle authored Feb 2, 2023
1 parent f72406f commit f620c07
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 41 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Depends:
R (>= 3.6.0)
Imports:
checkmate,
cli,
curl,
httr,
janitor,
Expand All @@ -33,7 +34,6 @@ Imports:
sf,
tibble,
tidyr,
usethis,
utils
Suggests:
covr,
Expand Down
45 changes: 26 additions & 19 deletions R/client.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ emodnet_init_wfs_client <- function(service, service_version = "2.0.0", logger =
)

check_wfs(wfs)
usethis::ui_done("WFS client created successfully")
usethis::ui_info("Service: {usethis::ui_value(wfs$getUrl())}")
usethis::ui_info("Version: {usethis::ui_value(wfs$getVersion())}")
cli_alert_success("WFS client created successfully")
cli_alert_info("Service: {.val {wfs$getUrl()}}")
cli_alert_info("Version: {.val {wfs$getVersion()}}")

wfs
}
Expand Down Expand Up @@ -67,8 +67,8 @@ get_service_name <- function(service_url) {

# Checks if there is internet and performs an HTTP GET request
perform_http_request <- function(service_url) {
usethis::ui_oops("WFS client creation failed.")
usethis::ui_info("Service: {usethis::ui_value(service_url)}")
cli_alert_danger("WFS client creation failed.")
cli_alert_info("Service: {.val {service_url}}")

has_internet <- function() {
if (nzchar(Sys.getenv("NO_INTERNET_TEST_EMODNET"))) {
Expand All @@ -78,7 +78,7 @@ perform_http_request <- function(service_url) {
}

if (!has_internet()) {
usethis::ui_info("Reason: There is no internet connection")
cli_alert_info("Reason: There is no internet connection")
return(NULL)
}

Expand All @@ -90,25 +90,32 @@ perform_http_request <- function(service_url) {
# Checks if there is internet connection and HTTP status of the service
check_service <- function(request) {
if (is.null(request)) {
rlang::abort("WFS client creation failed.")
cli::cli_abort("WFS client creation failed.")
}

if (httr::http_error(request)) {
usethis::ui_info("HTTP Status: {crayon::red(httr::http_status(request)$message)}")
usethis::ui_line()
if (httr::http_error(request)) {
cli_alert_danger("HTTP Status: {httr::http_status(request)$message}")

is_monitor_up <- !is.null(curl::nslookup("monitor.emodnet.eu", error = FALSE))
if (interactive() && is_monitor_up) {
if (usethis::ui_yeah("Browse the EMODnet OGC monitor?")) {
utils::browseURL("https://monitor.emodnet.eu/resources?lang=en&resource_type=OGC:WFS")
}
}
is_monitor_up <- !is.null(curl::nslookup("monitor.emodnet.eu", error = FALSE))
if (interactive() && is_monitor_up) {
browse_monitor <- utils::askYesNo("Browse the EMODnet OGC monitor?", FALSE, prompts = "yes/no/cancel")
if (is.na(browse_monitor)) browse_monitor <- FALSE
if (browse_monitor) {
utils::browseURL("https://monitor.emodnet.eu/resources?lang=en&resource_type=OGC:WFS")
}
}

rlang::abort("Service creation failed")
cli::cli_abort("Service creation failed")

# If no HTTP status, something else is wrong
} else if (!httr::http_error(request)) {
usethis::ui_info("HTTP Status: {crayon::green(httr::http_status(request)$message)}")
usethis::ui_stop("An exception has occurred. Please raise an issue in {packageDescription('EMODnetWFS')$BugReports}")
cli_alert_info("HTTP Status: {.val {httr::http_status(request)$message}}")

cli::cli_abort(
c(
"An exception has occurred.",
i = "Please raise an issue in {packageDescription('EMODnetWFS')$BugReports}"
)
)
}
}
10 changes: 7 additions & 3 deletions R/info.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@
emodnet_get_layer_info <- memoise::memoise(.emodnet_get_layer_info)

.emodnet_get_wfs_info <- function(wfs = NULL, service = NULL, service_version = "2.0.0") {
if (is.null(wfs) && is.null(service)) {
usethis::ui_stop("Please provide a valid {usethis::ui_field('service')} name or {usethis::ui_field('wfs')} object.
Both cannot be {usethis::ui_value('NULL')}")
if (is.null(wfs) && is.null(service)) {
cli::cli_abort(
c(
"Please provide a valid {.field service} name or {.field wfs} object.",
x = "Both cannot be {.val NULL} at the same time."
)
)
}

wfs <- wfs %||% emodnet_init_wfs_client(service, service_version)
Expand Down
36 changes: 22 additions & 14 deletions R/layers.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ emodnet_get_layers <- function(wfs = NULL, service = NULL, service_version = "2.
# check wfs ----------------------------------------------------------------

if (is.null(wfs) && is.null(service)) {
usethis::ui_stop(
"Please provide a valid {usethis::ui_field('service')} name or {usethis::ui_field('wfs')} object.
Both cannot be {usethis::ui_value('NULL')} at the same time."
)
cli::cli_abort(
c(
"Please provide a valid {.field service} name or {.field wfs} object.",
x = "Both cannot be {.val NULL} at the same time."
)
)
}

wfs <- wfs %||% emodnet_init_wfs_client(service, service_version)
Expand All @@ -93,10 +95,10 @@ emodnet_get_layers <- function(wfs = NULL, service = NULL, service_version = "2.

formats <- purrr::map_chr(layers, get_layer_format, wfs)
if (any(formats != "sf") && reduce_layers) {
rlang::abort(
cli::cli_abort(
c(
"Can't reduce layers when one is a data.frame",
i = sprintf("data.frame layer(s): %s", toString(layers[formats == "data.frame"]))
i = 'data.frame layer(s): {.val {toString(layers[formats == "data.frame"])}}'
)
)
}
Expand All @@ -107,7 +109,9 @@ emodnet_get_layers <- function(wfs = NULL, service = NULL, service_version = "2.

if (length(cql_filter) == 1L && length(layers) > 1L) {
cql_filter <- rep(cql_filter, times = length(layers))
usethis::ui_info('{usethis::ui_field("cql_filter")} {usethis::ui_code(cql_filter)} recycled across all layers')
cli_alert_info(
"{.field cql_filter} {.code {cql_filter}} recycled across all layers"
)
}

if (checkmate::test_named(cql_filter)) {
Expand Down Expand Up @@ -137,8 +141,12 @@ emodnet_get_layers <- function(wfs = NULL, service = NULL, service_version = "2.
tryCatch(
out <- purrr::reduce(out, rbind),
error = function(e) {
usethis::ui_stop("Cannot reduce layers.
Try again with {usethis::ui_code('reduce_layers = FALSE')}")
cli::cli_abort(
c(
"Cannot reduce layers.",
i = "Try again with {.code reduce_layers = FALSE}"
)
)
}
)
}
Expand Down Expand Up @@ -184,16 +192,16 @@ checkmate_crs <- function(sf, crs = NULL) {
}

if (is.na(sf::st_crs(sf)) || is.null(sf::st_crs(sf))) {
usethis::ui_warn("{usethis::ui_field('crs')} missing from `sf` object.")
cli::cli_warn("{.field crs} missing from `sf` object.")

if (!is.null(crs)) {
sf::st_crs(sf) <- crs
usethis::ui_info("{{usethis::ui_field('crs')} set to user specified CRS: {usethis::ui_value(crs)}.")
cli_alert_info("{.field crs} set to user specified CRS: {.val {crs}}.")
}
} else {
if (!is.null(crs)) {
sf <- sf::st_transform(sf, crs)
usethis::ui_info("{usethis::ui_field('crs')} transformed to {usethis::ui_value(crs)}.")
cli_alert_info("{.field crs} transformed to {.val {crs}}.")
}
}
return(sf)
Expand Down Expand Up @@ -227,7 +235,7 @@ ews_get_layer <- function(x, wfs, suppress_warnings = FALSE, cql_filter = NULL,
}
},
error = function(e) {
usethis::ui_warn("Download of layer {usethis::ui_value(x)} failed: {usethis::ui_field(e)}")
cli::cli_warn("Download of layer {.val {x}} failed: {.field {e}}")
}
)
} else {
Expand All @@ -241,7 +249,7 @@ ews_get_layer <- function(x, wfs, suppress_warnings = FALSE, cql_filter = NULL,
}
},
error = function(e) {
usethis::ui_warn("Download of layer {usethis::ui_value(x)} failed: {usethis::ui_field(e)}")
cli::cli_warn("Download of layer {.val {x}} failed: {.field {e}}")
}
)
}
Expand Down
17 changes: 17 additions & 0 deletions R/ui.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cli_alert_success <- function(...) {
if (!getOption("EMODnetWFS.quiet", FALSE)) {
cli::cli_alert_success(...)
}
}

cli_alert_info <- function(...) {
if (!getOption("EMODnetWFS.quiet", FALSE)) {
cli::cli_alert_info(...)
}
}

cli_alert_danger <- function(...) {
if (!getOption("EMODnetWFS.quiet", FALSE)) {
cli::cli_alert_danger(...)
}
}
3 changes: 2 additions & 1 deletion tests/testthat/_snaps/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

---

An exception has occurred. Please raise an issue in https://github.com/EMODnet/EMODnetWFS/issues
An exception has occurred.
i Please raise an issue in https://github.com/EMODnet/EMODnetWFS/issues

# No internet challenge

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/layers.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# emodnet_get_layers errors well when no service nor wfs

Please provide a valid service name or wfs object.
Both cannot be 'NULL' at the same time.
x Both cannot be "NULL" at the same time.

# emodnet_get_layers errors well when bad wfs

Expand All @@ -14,5 +14,5 @@
# works when data.frame layer

Can't reduce layers when one is a data.frame
i data.frame layer(s): OOPS_summaries, OOPS_metadata
i data.frame layer(s): "OOPS_summaries, OOPS_metadata"

3 changes: 2 additions & 1 deletion tests/testthat/setup.R
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pre_test_options <- options(usethis.quiet = TRUE)
pre_test_options <- options(EMODnetWFS.quiet = TRUE)

0 comments on commit f620c07

Please sign in to comment.