Skip to content
api_call.R 3.45 KiB
Newer Older
#' Get a codelist based on an identifier
#'
#' @param identifier  the codelist's identifier
#' @param export_format the export's format
#' Available are CSV, XLSX, SDMX-ML or JSON.
#' @param version_format the export format's version
#' (2.0 or 2.1 when format is SDMX-ML).
#' @param annotations flag to include annotations
#'
#' @return response based on the export format
#' @export
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
get_codelist <- function(identifier,
                         language = "fr",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
                         export_format = "SDMX-ML",
                         version_format = 2.1,
                         annotations = FALSE) {
  api <- api_class(
    api_type = "codelist",
    export_format = export_format,
    parameters = glue::glue("annotations={tolower(annotations)}"),
    id = identifier
  )
  dplyr::select(api$get_response(), contains(language) | matches("id"))
#' Get the data structure
#'
#' @param identifier the dataset's identifier
#' @param language  the language of the response data
#' Available are 'fr', 'de', 'it', 'en'.
#'
#' @return data structure
#' @export
get_data_structure <- function(identifier, language = "fr") {
  api <- api_class(
    api_type = "dcat_data_structure",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
    id = identifier,
    language = language
  )
  api$get_response()
}


#' Get one level of a nomenclature
#'
#' @param identifier nomenclature's identifier
#' @param filters additionnal filters
#' @param level_number level to export
#' @param language  the language of the response data
#' Available are 'fr', 'de', 'it', 'en'.
#' @param annotations flag to include annotations
#'
#' @return dataframe with 3 columns
#' (Code, Parent and Name in the selected language)
#' @export
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
get_nomenclature_one_level <- function(identifier,
                                       filters=hash::hash(),
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
                                       level_number = 1,
                                       language = "fr",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
                                       annotations = FALSE) {
  parameters <- glue::glue(
    "language={language}",
    "&level={level_number}",
    "&annotations={tolower(annotations)}",
    "&{hash_to_string(filters)}"
  )
  print(parameters)
  api <- api_class(
    api_type = "nomenclature_one_level",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
    id = identifier,
    parameters = parameters,
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
    export_format = "CSV"
  )
  api$get_response()
}
#' Get multiple levels of a nomenclature (from `level_from` to `level_to`)
#'
#' @param identifier nomenclature's identifier
#' @param filters additionnal filters
#' @param level_from the 1st level to include
#' @param level_to the last level to include
#' @param language  the language of the response data
#' Available are 'fr', 'de', 'it', 'en'.
#' @param annotations flag to include annotations
#'
#' @return dataframe columns
#' from `level_from` to `level_to` codes
#' @export
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
get_nomenclature_multiple_levels <- function(identifier,
                                             filters=hash::hash(),
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
                                             level_from = 1,
                                             level_to = 2,
                                             language = "fr",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
                                             annotations = FALSE) {
  parameters <- glue::glue(
    "language={language}",
    "&levelFrom={level_from}",
    "&levelTo={level_to}",
    "&annotations={tolower(annotations)}",
    "&{hash_to_string(filters)}"
  )
  api <- api_class(
    api_type = "nomenclature_multiple_levels",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
    id = identifier,
    parameters = parameters,
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
    export_format = "CSV"