## ----include=FALSE------------------------------------------------------------
hab_available <- tryCatch({
  httr::status_code(httr::GET("https://www.marinespecies.org/hab/",
                               httr::timeout(5))) < 400
}, error = function(e) FALSE)

toxin_reachable <- function(insecure = FALSE) {
  cfg <- if (insecure) httr::config(ssl_verifypeer = FALSE) else httr::config()
  tryCatch({
    httr::status_code(httr::GET("https://toxins.hais.ioc-unesco.org/",
                                 cfg, httr::timeout(5))) < 400
  }, error = function(e) FALSE)
}

# The toxins.hais.ioc-unesco.org TLS certificate may occasionally lapse. Fall
# back to an unverified request so the vignette can still build during such
# periods.
toxin_available <- toxin_reachable()
toxin_insecure <- FALSE
if (!toxin_available) {
  toxin_available <- toxin_reachable(insecure = TRUE)
  toxin_insecure <- toxin_available
}

## ----eval=FALSE---------------------------------------------------------------
# install.packages("SHARK4R")

## -----------------------------------------------------------------------------
library(SHARK4R)

## ----include=FALSE, eval=hab_available----------------------------------------
hab_list <- try(get_hab_list(), silent = TRUE)
if (inherits(hab_list, "try-error")) hab_available <- FALSE

## ----eval=FALSE---------------------------------------------------------------
# # Retrieve complete HAB list
# hab_list <- get_hab_list()
# 
# # Print result
# print(hab_list)

## ----echo=FALSE, eval=hab_available-------------------------------------------
print(hab_list)

## ----include=FALSE, eval=hab_available----------------------------------------
hab_non_toxic_list <- try(
  get_hab_list(harmful_non_toxic_only = TRUE, verbose = FALSE),
  silent = TRUE
)
if (inherits(hab_non_toxic_list, "try-error")) hab_available <- FALSE

## ----eval=FALSE---------------------------------------------------------------
# # Retrieve complete Harmful non-toxic list
# hab_non_toxic_list <- get_hab_list(harmful_non_toxic_only = TRUE,
#                                    verbose = FALSE)
# 
# # Print result
# print(hab_non_toxic_list)

## ----echo=FALSE, eval=hab_available-------------------------------------------
print(hab_non_toxic_list)

## ----include=FALSE, eval=toxin_available--------------------------------------
toxin_list <- try(get_toxin_list(insecure = toxin_insecure), silent = TRUE)
if (inherits(toxin_list, "try-error")) toxin_available <- FALSE

## ----eval=FALSE---------------------------------------------------------------
# # Retrieve complete toxin list
# toxin_list <- get_toxin_list()
# 
# # Print result
# print(toxin_list)

## ----echo=FALSE, eval=toxin_available-----------------------------------------
print(toxin_list)

## ----echo=FALSE---------------------------------------------------------------
# Print citation
citation("SHARK4R")

## ----echo=FALSE---------------------------------------------------------------
clean_shark4r_cache(0, clear_perm_cache = TRUE, verbose = FALSE)

