Newer
Older
#' API query for SDMX output
#'
#' @param url url to query
#'
#' @return dataframe response
#' API query for JSON output
#'
#' @param url url to query
#'
#' @return response: dataframe or list of dataframes
jsonlite::fromJSON(rawToChar(httr::GET(url)$content))
#' API query for CSV output
#'
#' @param url url to query
#'
#' @return dataframe response
csv_request <- function(url) {
read.csv(url)
}
# Request function based on expected response
REQUEST_FUNCTION_MAPPING <- hash::hash(
"SDMX-ML" = sdmx_request,
"JSON" = json_request,
"CSV" = csv_request
)
#' Transform a hash object into a string of parameters
#'
#' @param filters hash object
#'
#' @return formatted string of parameters
#' @examples
#' filters <- hash::hash("a" = list("1"), "b" = list("2", "3"))
#' hash_to_string(filters) -> "a=1&b=2&b=3"
hash_to_string <- function(filters) {
string <- ""
for (prop in ls(filters)) {
for (value in filters[[prop]]) {
if (string == "") {
string <- glue::glue("{prop}={value}")
} else {
string <- paste(string, glue::glue("{prop}={value}"), sep = "&")
}
}
}
string
}