diff --git a/DESCRIPTION b/DESCRIPTION index 5946555..a0a1b3f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: pallas Title: Construct Web Ontology Language (OWL) and query SPARQL endpoints from R -Version: 0.0.0.9000 +Version: 0.0.0.9001 Authors@R: c( person("Thomaz", "Bastiaanssen", email = "thomazbastiaanssen@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0001-6891-734X")), @@ -14,7 +14,8 @@ Imports: rlang, stats, httr, - BiocFileCache + BiocFileCache, + digest License: GPL (>= 3) Encoding: UTF-8 Roxygen: list(markdown = TRUE) diff --git a/NAMESPACE b/NAMESPACE index ad4c905..d0a38bf 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -32,6 +32,7 @@ importFrom(S7,new_class) importFrom(S7,new_generic) importFrom(S7,new_object) importFrom(S7,new_property) +importFrom(digest,digest) importFrom(httr,GET) importFrom(httr,accept) importFrom(httr,content) @@ -39,7 +40,6 @@ importFrom(httr,parse_url) importFrom(rlang,"fn_fmls<-") importFrom(rlang,as_label) importFrom(rlang,enexpr) -importFrom(rlang,hash) importFrom(stats,reformulate) importFrom(tools,R_user_dir) importFrom(utils,.DollarNames) diff --git a/R/send_query.R b/R/send_query.R index 0cf97a0..74b2cc0 100644 --- a/R/send_query.R +++ b/R/send_query.R @@ -42,14 +42,25 @@ send_query <- function( url$query <- list(query = I(query)) url <- httr::build_url(url) - - result <- httr::content( - httr::GET( - url, - httr::accept(out_format) + # Check if cache for key already exists + cache.path <- .getCache(url) + # If cache exists, read it + if (file.exists(cache.path)) { + result <- read.csv(cache.path, stringsAsFactors = TRUE) + # If cache does not exist, make GET request + } else { + result <- httr::content( + httr::GET( + url, + httr::accept(out_format) ), as = "text", encoding = "UTF-8" - ) - utils::read.csv(textConnection(result), stringsAsFactors = TRUE) + ) + # Read result from connection + result <- read.csv(textConnection(result), stringsAsFactors = TRUE) + # Save result to cache + write.csv(result, file = cache.path, row.names = FALSE) + } + return(result) } @@ -68,24 +79,18 @@ send_query <- function( # Adapted from UniProt.ws utilities.R #' @importFrom BiocFileCache BiocFileCache bfcneedsupdate bfcrpath bfcdownload #' @importFrom tools R_user_dir -#' @importFrom rlang hash +#' @importFrom digest digest .getCache <- function(url) { cache <- tools::R_user_dir("pallas", "cache") - bfc <- BiocFileCache::BiocFileCache(cache, ask = FALSE) - # url_hash <- rlang::hash(url) - + # Generate a hash as a unique cache key + key <- digest(url, algo = "sha256") + rpath <- BiocFileCache::bfcrpath( - bfc, rnames = url, exact = TRUE, download = TRUE, rtype = "web" + bfc, rnames = key, exact = TRUE, download = FALSE, rtype = "web" ) - to.update <- BiocFileCache::bfcneedsupdate(bfc, names(rpath)) - - if( to.update ){ - BiocFileCache::bfcdownload(bfc, names(rpath), ask = FALSE) - } - return(rpath) }