api_call.R 3.36 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#' Get a codelist based on an identifier
#'
#' @param identifier  the codelist's identifier
#' @param export_format the export's format
#' Available are CSV, XLSX, SDMX-ML or JSON.
#' @param version_format the export format's version
#' (2.0 or 2.1 when format is SDMX-ML).
#' @param annotations flag to include annotations
#'
#' @return response based on the export format
#' @export
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
12
get_codelist <- function(identifier,
13
                         language = "en",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
14
15
16
                         export_format = "SDMX-ML",
                         version_format = 2.1,
                         annotations = FALSE) {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
17
18
19
  api <- api_class(
    api_type = "codelist",
    export_format = export_format,
20
    parameters = glue::glue("annotations={tolower(annotations)}"),
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
21
22
    id = identifier
  )
23
  dplyr::select(api$get_response(), contains(language) | matches("id"))
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
24
25
26
}


27
28
29
30
31
32
33
34
#' Get the data structure
#'
#' @param identifier the dataset's identifier
#' @param language  the language of the response data
#' Available are 'fr', 'de', 'it', 'en'.
#'
#' @return data structure
#' @export
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
35
get_data_structure <- function(identifier, language = "en") {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
36
37
  api <- api_class(
    api_type = "dcat_data_structure",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
38
    id = identifier,
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
39
40
41
42
43
44
    language = language
  )
  api$get_response()
}


45
46
47
48
49
50
51
52
53
54
55
56
#' Get one level of a nomenclature
#'
#' @param identifier nomenclature's identifier
#' @param filters additionnal filters
#' @param level_number level to export
#' @param language  the language of the response data
#' Available are 'fr', 'de', 'it', 'en'.
#' @param annotations flag to include annotations
#'
#' @return dataframe with 3 columns
#' (Code, Parent and Name in the selected language)
#' @export
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
57
58
59
60
61
get_nomenclature_one_level <- function(identifier,
                                       filters,
                                       level_number = 1,
                                       language = "en",
                                       annotations = FALSE) {
62
  parameters <- glue::glue("language={language}&level={level_number}&annotations={tolower(annotations)}&{hash_to_string(filters)}")
63
64
65
  print(parameters)
  api <- api_class(
    api_type = "nomenclature_one_level",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
66
    id = identifier,
67
    parameters = parameters,
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
68
    export_format = "CSV"
69
70
71
  )
  api$get_response()
}
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
72
73


74
75
76
77
78
79
80
81
82
83
84
85
86
#' Get multiple levels of a nomenclature (from `level_from` to `level_to`)
#'
#' @param identifier nomenclature's identifier
#' @param filters additionnal filters
#' @param level_from the 1st level to include
#' @param level_to the last level to include
#' @param language  the language of the response data
#' Available are 'fr', 'de', 'it', 'en'.
#' @param annotations flag to include annotations
#'
#' @return dataframe columns
#' from `level_from` to `level_to` codes
#' @export
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
87
88
89
90
91
92
get_nomenclature_multiple_levels <- function(identifier,
                                             filters,
                                             level_from = 1,
                                             level_to = 2,
                                             language = "en",
                                             annotations = FALSE) {
93
  parameters <- glue::glue("language={language}&levelFrom={level_from}&levelTo={level_to}&annotations={tolower(annotations)}&{hash_to_string(filters)}")
94
95
  api <- api_class(
    api_type = "nomenclature_multiple_levels",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
96
    id = identifier,
97
    parameters = parameters,
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
98
    export_format = "CSV"
99
  )
100
  res <- api$get_response()
101
}