Skip to content
api_class.R 3.94 KiB
Newer Older
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
#' 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 root_url character (default = "BASE_URL"). Root url to call (for i14y or dcat)
#' @field parameters character. Additional request parameters
#' @field id character. The identifier or id of the request's object
#' @field language character (default = "en"). The language of the response data.
#' Available are '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 path character. The path leading to the nodes 
#' (for 'nomenclature_search')
#' @field api_url character. The url to make the request to.
#'
#' @examples
#' api <- api_class(api_type = "codelist", id = "CL_NOGA_SECTION")
#' api <- api_class(api_type = "content_configuration")
api_class <- setRefClass(
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
  "Api",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
    api_type = "character",
    export_format = "character",
    root_url = "character",
    parameters = "character",
    id = "character",
    language = "character",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
    version_format = "numeric",
    path = "character",
    api_url = "character"
  ),
  methods = list(
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
    initialize = function(...,
                          export_format = "JSON",
                          root_url = BASE_URL,
                          parameters = "",
                          id = "",
                          language = "en",
                          version_format = 2.1,
                          path = ".") {
      callSuper(
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
        ...,
        export_format = export_format,
        root_url = root_url,
        parameters = parameters,
        id = id,
        language = language,
        version_format = version_format,
        path = path
      )
      get_url()
    },
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
    get_response = function() {
      request_function <- REQUEST_FUNCTION_MAPPING[[export_format]]
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
      if (parameters == "") {
        url <- glue::glue("{root_url}/api/{api_url}")
      } else {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
        url <- glue::glue("{root_url}/api/{api_url}?{parameters}")
      }
      request_function(url)
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
    },
    get_url = function() {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
      url_mapping <- hash::hash(
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
        "codelist" =
          glue::glue("CodeLists/{id}/exports/{export_format}/{version_format}"),
        "content_configuration" =
          glue::glue("ContentConfigurations"),
        "content_configuration_identifier" =
          glue::glue("ContentConfigurations/{id}"),
        "dcat_data_structure" =
          glue::glue("DataStructures/{id}/{language}"),
        "dcat_dataset_description" =
          glue::glue("Datasets/{id}/{language}/description"),
        "dcat_dataset_information" =
          glue::glue("Datasets/{id}/{language}/distributions"),
        "nomenclature_path_nodes" =
          glue::glue("Nomenclatures/Childnodes/{id}/{language}/{path}"),
        "nomenclature_one_level" =
          glue::glue("Nomenclatures/{id}/levelexport/CSV"),
        "nomenclature_multiple_levels" =
          glue::glue("Nomenclatures/{id}/multiplelevels/CSV"),
        "nomenclature_search" =
          glue::glue("Nomenclatures/{id}/search"),

Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
        "agents_list" =
          glue::glue("Agent"),
        "agent_id" =
          glue::glue("Agent/{id}"),
        "dataset_list" =
          glue::glue("Dataset"),
        "dataset_id_distributions" =
          glue::glue("Dataset/{id}/distributions"),
        "dataset_id" =
          glue::glue("Dataset/{id}"),
        "dataset_identifier" =
          glue::glue("Datataset/identifier/{id}"),
        "dataset_identifier_distributions" =
          glue::glue("Datataset/identifier/{id}/distributions"),
        "distributions_list" =
          glue::glue("Distribution"),
        "distribution_id" =
          glue::glue("Distribution/{id}")
      )
      api_url <<- url_mapping[[api_type]]
    }
  )
)