api_class.R 2.67 KB
Newer Older
1

Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
2
3
4
5
6
7
8
#' 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
9
10
#' @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
11
#' @field version_format numeric (default = 2.1). The export format's version
12
#' (2.0 or 2.1 when format is SDMX-ML)
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
13
14
#' (for 'codelist')
#' @field api_url character. The url to make the request to.
15
#'
16
#' @importFrom methods new
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
17
api_class <- setRefClass(
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
18
  "Api",
19
  fields = list(
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
20
21
    api_type = "character",
    export_format = "character",
22
23
24
    parameters = "character",
    id = "character",
    language = "character",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
25
    version_format = "numeric",
26
27
28
    api_url = "character"
  ),
  methods = list(
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
29
30
31
32
    initialize = function(...,
                          export_format = "JSON",
                          parameters = "",
                          id = "",
33
                          language = "all",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
34
                          version_format = 2.1) {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
35
      callSuper(
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
36
37
38
39
40
        ...,
        export_format = export_format,
        parameters = parameters,
        id = id,
        language = language,
41
        version_format = version_format
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
42
      )
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
43
      get_url(id, export_format, version_format, language)
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
44
    },
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
45
    get_response = function() {
46
      # Select type of call to API
47
      request_function <- REQUEST_FUNCTION_MAPPING[[export_format]]
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
48
      if (parameters == "") {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
49
        url <- glue::glue("{BASE_URL}/api/{api_url}")
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
50
      } else {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
51
        url <- glue::glue("{BASE_URL}/api/{api_url}?{parameters}")
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
52
      }
53
54
55
56
57
58
59
60
61
62
63
      
      # 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
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
64
    },
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
65
    get_url = function(id, export_format, version_format, language) {
66
      # Map function names to specific API URL
67
      url_mapping <- list(
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
68
69
70
71
72
73
74
        "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" =
75
          glue::glue("Nomenclatures/{id}/multiplelevels/CSV")
76
77
78
79
80
      )
      api_url <<- url_mapping[[api_type]]
    }
  )
)