format_request.R 676 Bytes
Newer Older
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
1

Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
2
3
4
5
6
#' API query for SDMX output
#'
#' @param url url to query
#'
#' @return dataframe response
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
7
sdmx_request <- function(url) {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
8
  as.data.frame(rsdmx::readSDMX(url))
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
9
10
}

Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
11
12
13
14
15
#' API query for JSON output
#'
#' @param url url to query
#'
#' @return response: dataframe or list of dataframes
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
16
json_request <- function(url) {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
17
  jsonlite::fromJSON(rawToChar(httr::GET(url)$content))
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
18
19
}

Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
20
21
22
23
24
#' API query for CSV output
#'
#' @param url url to query
#'
#' @return dataframe response
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
25
26
csv_request <- function(url) {
  read.csv(url)
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
27
28
29
30
31
32
33
34
}

# Request function based on expected response
REQUEST_FUNCTION_MAPPING <- hash::hash(
  "SDMX-ML" = sdmx_request,
  "JSON" = json_request,
  "CSV" = csv_request
)