#' Api class to make appropriate request based on parameters #' #' @field api_type character. The name of the api to call (see url_mapping) #' @field export_format character (default = "JSON"). The export's format #' Available are CSV, XLSX, SDMX-ML and JSON #' @field parameters character. Additional request parameters #' @field id character. The identifier or id of the request's object #' @field language character (default = "all"). Language of the response data. #' Available are 'all', 'fr', 'de', 'it', 'en' #' @field version_format numeric (default = 2.1). The export format's version #' (2.0 or 2.1 when format is SDMX-ML) #' (for 'codelist') #' @field api_url character. The url to make the request to. #' #' @importFrom methods new api_class <- setRefClass( "Api", fields = list( api_type = "character", export_format = "character", parameters = "character", id = "character", language = "character", version_format = "numeric", api_url = "character" ), methods = list( initialize = function(..., export_format = "JSON", parameters = "", id = "", language = "all", version_format = 2.1) { callSuper( ..., export_format = export_format, parameters = parameters, id = id, language = language, version_format = version_format ) get_url(id, export_format, version_format, language) }, get_response = function() { # Select type of call to API request_function <- REQUEST_FUNCTION_MAPPING[[export_format]] if (parameters == "") { url <- glue::glue("{BASE_URL}/api/{api_url}") } else { url <- glue::glue("{BASE_URL}/api/{api_url}?{parameters}") } # API call res <- request_function(url) # If specified language, keep only language specific columns if(language != "all") { res <- dplyr::select( res, dplyr::contains(language) | dplyr::matches("id") ) } res }, get_url = function(id, export_format, version_format, language) { # Map function names to specific API URL url_mapping <- list( "codelist" = glue::glue("CodeLists/{id}/exports/{export_format}/{version_format}"), "dcat_data_structure" = glue::glue("DataStructures/{id}/{language}"), "nomenclature_one_level" = glue::glue("Nomenclatures/{id}/levelexport/CSV"), "nomenclature_multiple_levels" = glue::glue("Nomenclatures/{id}/multiplelevels/CSV") ) api_url <<- url_mapping[[api_type]] } ) )