Skip to content
Merged
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
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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")),
Expand All @@ -14,7 +14,8 @@ Imports:
rlang,
stats,
httr,
BiocFileCache
BiocFileCache,
digest
License: GPL (>= 3)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ 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)
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)
Expand Down
41 changes: 23 additions & 18 deletions R/send_query.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}


Expand All @@ -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)
}
Loading