api_class.R 3.06 KB
Newer Older
1

Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
2
3
4
#' Api class to make appropriate request based on parameters
#'
#' @field api_type character. The name of the api to call (see url_mapping)
5
6
#' @field environment environment on which to query API
#' Available are 'PRD', 'ABN', 'TEST', 'QA' and 'DEV'.
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
7
8
9
10
#' @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
11
12
#' @field language character (default = "all"). Language of the response data.
#' Available are 'all', 'fr', 'de', 'it', 'en'
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
13
#' @field version_format numeric (default = 2.1). The export format's version
14
#' (2.0 or 2.1 when format is SDMX-ML)
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
15
16
#' (for 'codelist')
#' @field api_url character. The url to make the request to.
17
#'
18
#' @importFrom methods new
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
19
api_class <- setRefClass(
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
20
  "Api",
21
  fields = list(
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
22
    api_type = "character",
23
    environment = "character",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
24
    export_format = "character",
25
26
27
    parameters = "character",
    id = "character",
    language = "character",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
28
    version_format = "numeric",
29
30
31
    api_url = "character"
  ),
  methods = list(
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
32
    initialize = function(...,
33
                          environment = "PRD",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
34
35
36
                          export_format = "JSON",
                          parameters = "",
                          id = "",
37
                          language = "all",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
38
                          version_format = 2.1) {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
39
      callSuper(
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
40
        ...,
41
        environment = environment,
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
42
43
44
45
        export_format = export_format,
        parameters = parameters,
        id = id,
        language = language,
46
        version_format = version_format
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
47
      )
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
48
      get_url(id, export_format, version_format, language)
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
49
    },
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
50
    get_url = function(id, export_format, version_format, language) {
51
      # Map function names to specific API URL
52
      url_mapping <- list(
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
53
54
55
56
57
        "codelist" =
          glue::glue("CodeLists/{id}/exports/{export_format}/{version_format}"),
        "nomenclature_one_level" =
          glue::glue("Nomenclatures/{id}/levelexport/CSV"),
        "nomenclature_multiple_levels" =
58
          glue::glue("Nomenclatures/{id}/multiplelevels/CSV")
59
60
      )
      api_url <<- url_mapping[[api_type]]
61
62
63
    },
    get_response = function() {
      # API call to url
64
      url <- glue::glue("{ENVIRONMENTS[[environment]]}/api/{api_url}?{parameters}")
65
      res <- REQUEST_FUNCTION_MAPPING[[export_format]](url)
66

67
      # If specified language, keep only language specific columns
68
69
70
71
      if (language != "all") {
        res <- remove_other_languages(res, language)
      }

72
      res
73
74
75
    }
  )
)
76
77
78
79
80
81
82
83
84
85
86
87


#' Remove columns that belong to other languages than the selected one
#'
#' @param df data.frame returned from API with columns for all languages
#' @param language language to keep
#'
#' @return dataframe with columns relevant to selected language only
remove_other_languages <- function(df, language) {
  to_remove <- glue::glue(
    "[.|_]+({paste(LANGUAGES[LANGUAGES != language], collapse = '|')})$"
  )
88
89
  df[, !names(df) %in% names(df)[stringr::str_detect(names(df), to_remove)]]
}