I am writing an R package as a client for an API (which doesn't require authentication and has no rate limits, so you are free to explore). The API works with POST requests, see. e.g. here: https://api.statbank.dk/console#data
The tricky input is the variables
parameter in the API call body. Here is an example:
{
"lang": "en",
"table": "folk1c",
"format": "CSV",
"valuePresentation": "Default",
"timeOrder": "Ascending",
"variables": [
{
"code": "IELAND",
"values": [
5100
]
},
{
"code": "KØN",
"values": [
"1",
"2"
]
},
{
"code": "tid",
"values": [
"*"
]
}
]
}
The variables
input can look in many different ways and can be very complex. A successful R implementation of the above call body is like this:
json_body <- list(table = "folk1c",
format = "CSV",
lang = "en",
delimiter = "Tab",
variables = list(list(code = "ieland", values = I(5100)),
list(code = "køn", values = c(1,2)),
list(code = "tid", values = I("*")))
)
response <- POST("https://api.statbank.dk/v1/data", body = json_body, encode = "json")
output <- content(response, type = "text/tab-separated-values")
My my main question is, what is from the package user perspective the most painless and elegant way (I am looking for some inspiration here) to request the variables
parameter as a function input as I cannot expect a user to have to provide such a complicated nested list object with I()
, etc.
Thanks