You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
opensensmapR/R/api.R

50 lines
1.5 KiB
R

# ==============================================================================
# getters for the opensensemap API.
# the awesome httr library does all the curling, query and response parsing.
# for CSV responses (get_measurements) the readr package is a hidden dependency
# ==============================================================================
get_boxes_ = function (..., endpoint) {
response = httr::GET(endpoint, path = c('boxes'), query = list(...)) %>%
httr::content() %>%
osem_remote_error()
if (length(response) == 0) {
warning('no boxes found for this query')
return(response)
}
# parse each list element as sensebox & combine them to a single data.frame
boxesList = lapply(response, parse_senseboxdata)
df = dplyr::bind_rows(boxesList)
df$exposure = df$exposure %>% as.factor()
df$model = df$model %>% as.factor()
df$grouptag = df$grouptag %>% as.factor()
df
}
get_box_ = function (..., endpoint) {
httr::GET(endpoint, path = c('boxes', ...)) %>%
httr::content() %>%
osem_remote_error() %>%
parse_senseboxdata()
}
get_measurements_ = function (..., endpoint) {
result = httr::GET(endpoint, path = c('boxes', 'data'), query = list(...)) %>%
httr::content(encoding = 'UTF-8') %>%
osem_remote_error()
class(result) = c('osem_measurements', class(result))
result
}
get_stats_ = function (endpoint) {
result = httr::GET(endpoint, path = c('stats')) %>%
httr::content() %>%
osem_remote_error()
names(result) = c('boxes', 'measurements', 'measurements_per_minute')
result
}