api_class.R 3.94 KB
Newer Older
1

Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#' 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")
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
22
api_class <- setRefClass(
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
23
  "Api",
24
  fields = list(
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
25
26
    api_type = "character",
    export_format = "character",
27
28
29
30
    root_url = "character",
    parameters = "character",
    id = "character",
    language = "character",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
31
    version_format = "numeric",
32
33
34
35
    path = "character",
    api_url = "character"
  ),
  methods = list(
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
36
37
38
39
40
41
42
43
    initialize = function(...,
                          export_format = "JSON",
                          root_url = BASE_URL,
                          parameters = "",
                          id = "",
                          language = "en",
                          version_format = 2.1,
                          path = ".") {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
44
      callSuper(
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
45
46
47
48
49
50
51
        ...,
        export_format = export_format,
        root_url = root_url,
        parameters = parameters,
        id = id,
        language = language,
        version_format = version_format,
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
52
53
54
55
        path = path
      )
      get_url()
    },
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
56
    get_response = function() {
57
      request_function <- REQUEST_FUNCTION_MAPPING[[export_format]]
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
58
59
      if (parameters == "") {
        url <- glue::glue("{root_url}/api/{api_url}")
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
60
      } else {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
61
        url <- glue::glue("{root_url}/api/{api_url}?{parameters}")
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
62
63
      }
      request_function(url)
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
64
    },
65
    get_url = function() {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
66
      url_mapping <- hash::hash(
67
        # i14y
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        "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"),

89
        # dcat
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
        "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}")
108
109
110
111
112
      )
      api_url <<- url_mapping[[api_type]]
    }
  )
)