Commit cabda009 authored by Pauline Maury Laribière's avatar Pauline Maury Laribière
Browse files

adding possibility for multi languages

parent 02b8543d
Pipeline #325547 passed with stage
in 14 seconds
#' Get a codelist based on an identifier #' Get a codelist based on an identifier
#' #'
#' @param identifier the codelist's identifier #' @param identifier the codelist's identifier
#' @param language string for language to return. #' @param language string for language(s) to return.
#' Available are 'fr', 'de', 'it', 'en'. #' Available are 'all', 'fr', 'de', 'it', 'en'.
#' If 'all' (default), all languages are returned.
#' @param export_format the export's format #' @param export_format the export's format
#' Available are CSV, XLSX, SDMX-ML or JSON. #' Available are CSV, XLSX, SDMX-ML or JSON.
#' @param version_format the export format's version #' @param version_format the export format's version
...@@ -12,19 +13,18 @@ ...@@ -12,19 +13,18 @@
#' @return response based on the export format #' @return response based on the export format
#' @export #' @export
get_codelist <- function(identifier, get_codelist <- function(identifier,
language = "fr", language = "all",
export_format = "SDMX-ML", export_format = "SDMX-ML",
version_format = 2.1, version_format = 2.1,
annotations = FALSE) { annotations = FALSE) {
api <- api_class( api <- api_class(
api_type = "codelist", api_type = "codelist",
id = identifier,
language = language,
export_format = export_format, export_format = export_format,
parameters = glue::glue("annotations={tolower(annotations)}"), parameters = glue::glue("annotations={tolower(annotations)}")
id = identifier
)
dplyr::select(
api$get_response(), dplyr::contains(language) | dplyr::matches("id")
) )
api$get_response()
} }
...@@ -43,18 +43,17 @@ get_codelist <- function(identifier, ...@@ -43,18 +43,17 @@ get_codelist <- function(identifier,
get_nomenclature_one_level <- function(identifier, get_nomenclature_one_level <- function(identifier,
filters = list(), filters = list(),
level_number = 1, level_number = 1,
language = "fr", language = "all",
annotations = FALSE) { annotations = FALSE) {
parameters <- glue::glue( parameters <- glue::glue(
"language={language}", "level={level_number}",
"&level={level_number}",
"&annotations={tolower(annotations)}", "&annotations={tolower(annotations)}",
"&{list_to_string(filters)}" "&{list_to_string(filters)}"
) )
print(parameters)
api <- api_class( api <- api_class(
api_type = "nomenclature_one_level", api_type = "nomenclature_one_level",
id = identifier, id = identifier,
language = language,
parameters = parameters, parameters = parameters,
export_format = "CSV" export_format = "CSV"
) )
......
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
#' Available are CSV, XLSX, SDMX-ML and JSON #' Available are CSV, XLSX, SDMX-ML and JSON
#' @field parameters character. Additional request parameters #' @field parameters character. Additional request parameters
#' @field id character. The identifier or id of the request's object #' @field id character. The identifier or id of the request's object
#' @field language character (default = "en"). Language of the response data. #' @field language character (default = "all"). Language of the response data.
#' Available are 'fr', 'de', 'it', 'en' #' Available are 'all', 'fr', 'de', 'it', 'en'
#' @field version_format numeric (default = 2.1). The export format's version #' @field version_format numeric (default = 2.1). The export format's version
#' (2.0 or 2.1 when format is SDMX-ML) #' (2.0 or 2.1 when format is SDMX-ML)
#' (for 'codelist') #' (for 'codelist')
...@@ -30,7 +30,7 @@ api_class <- setRefClass( ...@@ -30,7 +30,7 @@ api_class <- setRefClass(
export_format = "JSON", export_format = "JSON",
parameters = "", parameters = "",
id = "", id = "",
language = "en", language = "all",
version_format = 2.1) { version_format = 2.1) {
callSuper( callSuper(
..., ...,
...@@ -43,15 +43,27 @@ api_class <- setRefClass( ...@@ -43,15 +43,27 @@ api_class <- setRefClass(
get_url(id, export_format, version_format, language) get_url(id, export_format, version_format, language)
}, },
get_response = function() { get_response = function() {
# Select type of call to API
request_function <- REQUEST_FUNCTION_MAPPING[[export_format]] request_function <- REQUEST_FUNCTION_MAPPING[[export_format]]
if (parameters == "") { if (parameters == "") {
url <- glue::glue("{BASE_URL}/api/{api_url}") url <- glue::glue("{BASE_URL}/api/{api_url}")
} else { } else {
url <- glue::glue("{BASE_URL}/api/{api_url}?{parameters}") url <- glue::glue("{BASE_URL}/api/{api_url}?{parameters}")
} }
request_function(url)
# 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) { get_url = function(id, export_format, version_format, language) {
# Map function names to specific API URL
url_mapping <- list( url_mapping <- list(
"codelist" = "codelist" =
glue::glue("CodeLists/{id}/exports/{export_format}/{version_format}"), glue::glue("CodeLists/{id}/exports/{export_format}/{version_format}"),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment