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


35
36
37
#' Get one level of a nomenclature
#'
#' @param identifier nomenclature's identifier
38
39
#' @param environment environment on which to query API
#' Available are 'PRD', 'ABN', 'TEST', 'QA' and 'DEV'.
40
41
42
43
44
45
46
47
48
#' @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
49
get_nomenclature_one_level <- function(identifier,
50
                                       environment = "PRD",
51
                                       filters = list(),
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
52
                                       level_number = 1,
53
                                       language = "all",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
54
                                       annotations = FALSE) {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
55
  parameters <- glue::glue(
56
    "level={level_number}",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
57
    "&annotations={tolower(annotations)}",
58
    "&{list_to_string(filters)}"
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
59
  )
60
61
  api <- api_class(
    api_type = "nomenclature_one_level",
62
    environment = environment,
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
63
    id = identifier,
64
    language = language,
65
    parameters = parameters,
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
66
    export_format = "CSV"
67
68
69
  )
  api$get_response()
}
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
70
71


72
73
74
#' Get multiple levels of a nomenclature (from `level_from` to `level_to`)
#'
#' @param identifier nomenclature's identifier
75
76
#' @param environment environment on which to query API
#' Available are 'PRD', 'ABN', 'TEST', 'QA' and 'DEV'.
77
78
79
80
81
82
83
84
85
86
#' @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
get_nomenclature_multiple_levels <- function(identifier,
88
                                             environment = "PRD",
89
                                             filters = list(),
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
90
91
                                             level_from = 1,
                                             level_to = 2,
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
92
                                             language = "fr",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
93
                                             annotations = FALSE) {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
94
95
96
97
98
  parameters <- glue::glue(
    "language={language}",
    "&levelFrom={level_from}",
    "&levelTo={level_to}",
    "&annotations={tolower(annotations)}",
99
    "&{list_to_string(filters)}"
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
100
  )
101
102
  api <- api_class(
    api_type = "nomenclature_multiple_levels",
103
    environment = environment,
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
104
    id = identifier,
105
    parameters = parameters,
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
106
    export_format = "CSV"
107
  )
108
  res <- api$get_response()
109
}