api_call.R 3.16 KB
Newer Older
1
2
3
#' Get a codelist based on an identifier
#'
#' @param identifier  the codelist's identifier
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
4
#' @param language string for language to return.
5
#' Available are 'fr', 'de', 'it', 'en'.
6
7
8
9
10
11
12
13
#' @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
14
get_codelist <- function(identifier,
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
15
                         language = "fr",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
16
17
18
                         export_format = "SDMX-ML",
                         version_format = 2.1,
                         annotations = FALSE) {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
19
20
21
  api <- api_class(
    api_type = "codelist",
    export_format = export_format,
22
    parameters = glue::glue("annotations={tolower(annotations)}"),
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
23
24
    id = identifier
  )
25
26
  dplyr::select(
    api$get_response(), dplyr::contains(language) | dplyr::matches("id")
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
27
28
29
30
  )
}


31
32
33
34
35
36
37
38
39
40
41
42
#' 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
43
get_nomenclature_one_level <- function(identifier,
44
                                       filters = list(),
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
45
                                       level_number = 1,
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
46
                                       language = "fr",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
47
                                       annotations = FALSE) {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
48
49
50
51
  parameters <- glue::glue(
    "language={language}",
    "&level={level_number}",
    "&annotations={tolower(annotations)}",
52
    "&{list_to_string(filters)}"
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
53
  )
54
55
56
  print(parameters)
  api <- api_class(
    api_type = "nomenclature_one_level",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
57
    id = identifier,
58
    parameters = parameters,
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
59
    export_format = "CSV"
60
61
62
  )
  api$get_response()
}
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
63
64


65
66
67
68
69
70
71
72
73
74
75
76
77
#' 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
78
get_nomenclature_multiple_levels <- function(identifier,
79
                                             filters = list(),
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
80
81
                                             level_from = 1,
                                             level_to = 2,
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
82
                                             language = "fr",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
83
                                             annotations = FALSE) {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
84
85
86
87
88
  parameters <- glue::glue(
    "language={language}",
    "&levelFrom={level_from}",
    "&levelTo={level_to}",
    "&annotations={tolower(annotations)}",
89
    "&{list_to_string(filters)}"
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
90
  )
91
92
  api <- api_class(
    api_type = "nomenclature_multiple_levels",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
93
    id = identifier,
94
    parameters = parameters,
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
95
    export_format = "CSV"
96
  )
97
  res <- api$get_response()
98
}