api_class.R 2.33 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
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
9
#' @field language character (default = "en"). Language of the response data.
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
10
11
#' Available are 'fr', 'de', 'it', 'en'
#' @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
33
    initialize = function(...,
                          export_format = "JSON",
                          parameters = "",
                          id = "",
                          language = "en",
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
      request_function <- REQUEST_FUNCTION_MAPPING[[export_format]]
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
47
      if (parameters == "") {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
48
        url <- glue::glue("{BASE_URL}/api/{api_url}")
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
49
      } else {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
50
        url <- glue::glue("{BASE_URL}/api/{api_url}?{parameters}")
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
51
52
      }
      request_function(url)
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
53
    },
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
54
    get_url = function(id, export_format, version_format, language) {
55
      url_mapping <- list(
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
56
57
58
59
60
61
62
        "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" =
63
          glue::glue("Nomenclatures/{id}/multiplelevels/CSV")
64
65
66
67
68
      )
      api_url <<- url_mapping[[api_type]]
    }
  )
)