@ -12,7 +12,7 @@ osem_endpoint = function() {
}
get_boxes_ = function ( ... , endpoint ) {
response = osem_ request_ ( endpoint , path = c ( ' boxes' ) , ... )
response = osem_ get_resource ( endpoint , path = c ( ' boxes' ) , ... )
if ( length ( response ) == 0 ) {
warning ( ' no senseBoxes found for this query' )
@ -29,13 +29,13 @@ get_boxes_ = function (..., endpoint) {
df
}
get_box_ = function ( boxId , endpoint ) {
osem_ request_ ( endpoint , path = c ( ' boxes' , boxId ) , progress = F ) %>%
get_box_ = function ( boxId , endpoint , ... ) {
osem_ get_resource ( endpoint , path = c ( ' boxes' , boxId ) , ... , progress = FALSE ) %>%
parse_senseboxdata ( )
}
get_measurements_ = function ( ... , endpoint ) {
result = osem_ request_ ( endpoint , c ( ' boxes' , ' data' ) , ... , type = ' text' )
result = osem_ get_resource ( endpoint , c ( ' boxes' , ' data' ) , ... , type = ' text' )
# parse the CSV response manually & mute readr
suppressWarnings ( {
@ -53,15 +53,67 @@ get_measurements_ = function (..., endpoint) {
osem_as_measurements ( result )
}
get_stats_ = function ( endpoint ) {
result = osem_ request_ ( endpoint , path = c ( ' stats' ) , progress = F )
get_stats_ = function ( endpoint , cache ) {
result = osem_ get_resource ( endpoint , path = c ( ' stats' ) , progress = FALSE , cache = cache )
names ( result ) = c ( ' boxes' , ' measurements' , ' measurements_per_minute' )
result
}
osem_request_ = function ( host , path , ... , type = ' parsed' , progress ) {
#' Get any resource from openSenseMap API, possibly cache the response
#'
#' @param host API host
#' @param path resource URL
#' @param ... All other parameters interpreted as request query parameters
#' @param type Passed to httr; 'parsed' to return an R object from the response, 'text for a raw response
#' @param progress Boolean whether to print download progress information
#' @param cache Optional path to a directory were responses will be cached. If not NA, no requests will be made when a request for the given is already cached.
#' @return Result of a Request to openSenseMap API
#' @noRd
osem_get_resource = function ( host , path , ... , type = ' parsed' , progress = T , cache = NA ) {
query = list ( ... )
if ( ! is.na ( cache ) ) {
filename = osem_cache_filename ( path , query , host ) %>% paste ( cache , ., sep = ' /' )
if ( file.exists ( filename ) )
return ( readRDS ( filename ) )
}
res = osem_request_ ( host , path , query , type , progress )
if ( ! is.na ( cache ) ) saveRDS ( res , filename )
res
}
osem_cache_filename = function ( path , query = list ( ) , host = osem_endpoint ( ) ) {
httr :: modify_url ( url = host , path = path , query = query ) %>%
digest :: digest ( algo = ' sha1' ) %>%
paste ( ' osemcache' , ., ' rds' , sep = ' .' )
}
#' Purge cached responses from the given cache directory
#'
#' @param location A path to the cache directory, defaults to the
#' sessions' \code{tempdir()}
#' @return Boolean whether the deletion was successful
#'
#' @export
#' @examples
#' \donttest{
#' osem_boxes(cache = tempdir())
#' osem_clear_cache()
#'
#' cachedir = paste(getwd(), 'osemcache', sep = '/')
#' osem_boxes(cache = cachedir)
#' osem_clear_cache(cachedir)
#' }
osem_clear_cache = function ( location = tempdir ( ) ) {
list.files ( location , pattern = ' osemcache\\..*\\.rds' ) %>%
lapply ( function ( f ) file.remove ( paste ( location , f , sep = ' /' ) ) ) %>%
unlist ( ) %>%
all ( )
}
osem_request_ = function ( host , path , query = list ( ) , type = ' parsed' , progress = TRUE ) {
progress = if ( progress && ! is_non_interactive ( ) ) httr :: progress ( ) else NULL
res = httr :: GET ( host , progress , path = path , query = list ( ... ) )
res = httr :: GET ( host , progress , path = path , query = query )
if ( httr :: http_error ( res ) ) {
content = httr :: content ( res , ' parsed' , encoding = ' UTF-8' )