api_call.R 10.9 KB
Newer Older
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
1
2
3
4
###############################################################
########################## i14y APIs ##########################
###############################################################

5
6
7
8
9
10
11
12
13
14
15
16
17
18
#' 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
#'
#' @examples
#' get_codelist(identifier='CL_NOGA_SECTION')
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
19
20
21
22
get_codelist <- function(identifier,
                         export_format = "SDMX-ML",
                         version_format = 2.1,
                         annotations = FALSE) {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
23
24
25
26
27
28
29
30
31
32
  api <- api_class(
    api_type = "codelist",
    export_format = export_format,
    parameters = glue("annotations={tolower(annotations)}"),
    id = identifier
  )
  api$get_response()
}


33
34
35
36
37
38
39
#' Get the display information for the available configured content
#'
#' @return the configured content's display information
#' @export
#'
#' @examples
#' get_content_configuration()
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
40
get_content_configuration <- function() {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
41
42
43
  api <- api_class(api_type = "content_configuration")
  api$get_response()
}
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
44
45


Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
46

47
48
49
50
51
52
53
54
55
#' Get a nomenclature information based on its identifier
#'
#' @param identifier the nomenclature's identifier
#'
#' @return  the nomenclature's information
#' @export
#'
#' @examples
#' get_identifier_content(identifier='HCL_CH_ISCO_19_PROF')
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
56
get_identifier_content <- function(identifier) {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
57
  api <- api_class(
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
58
59
    api_type = "content_configuration_identifier",
    id = identifier
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
60
61
62
  )
  api$get_response()
}
63

Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
64
65


Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
66

67
68
69
70
71
72
73
74
75
76
77
#' Get the dcat dataset description
#'
#' @param identifier the dataset's identifier
#' @param language the language of the response data
#' Available are 'fr', 'de', 'it', 'en'.
#'
#' @return dataset description
#' @export
#'
#' @examples
#' get_dataset_description(identifier='HCL_NOGA', language='de')
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
78
get_dataset_description <- function(identifier, language = "fr") {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
79
80
  api <- api_class(
    api_type = "dcat_dataset_description",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
81
    id = identifier,
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
82
83
84
85
    language = language
  )
  api$get_response()
}
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
86
87
88



89
90
91
92
93
94
95
96
97
98
99
#' Get the dcat dataset information
#'
#' @param identifier the dataset's identifier
#' @param language  the language of the response data
#' Available are 'fr', 'de', 'it', 'en'.
#'
#' @return dataset's information
#' @export
#'
#' @examples
#' get_dataset_information(identifier='HCL_NOGA', language='de')
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
100
get_dataset_information <- function(identifier, language = "en") {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
101
102
  api <- api_class(
    api_type = "dcat_dataset_information",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
103
    id = identifier,
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
104
    language = language
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
105
  )
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
106
  api$get_response()
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
107
}
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
108
109
110



111
112
113
114
115
116
117
118
119
120
121
#' 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
#'
#' @examples
#' get_data_structure(identifier='HCL_NOGA', language='it')
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
122
get_data_structure <- function(identifier, language = "en") {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
123
124
  api <- api_class(
    api_type = "dcat_data_structure",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
125
    id = identifier,
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
126
127
128
129
130
131
132
    language = language
  )
  api$get_response()
}



133
134
135
136
137
138
139
140
141
142
143
144
145
#' Get the nodes of a path within a nomenclature
#'
#' @param identifier the nomenclature's identifier
#' @param path the path leading to the nodes
#' @param filters the filters to apply
#' @param language  the language of the response data
#' Available are 'fr', 'de', 'it', 'en'.
#' 
#' @return nodes and their metadata
#' @export
#'
#' @examples
#' get_nomenclature_path_nodes(identifier='HCL_CH_ISCO_19_PROF', path='.', filters=my_filters)
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
146
147
148
149
get_nomenclature_path_nodes <- function(identifier,
                                        path = ".",
                                        filters = "",
                                        language = "en") {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
150
151
  api <- api_class(
    api_type = "nomenclature_path_nodes",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
152
    id = identifier,
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
153
154
155
156
157
158
159
160
    path = path,
    parameters = stringify(filters),
    language = language
  )
  api$get_response()
}


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#' 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
#'
#' @examples
#' get_nomenclature_one_level(identifier='HCL_CH_ISCO_19_PROF', filters=my_filters, level_number=2)
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
176
177
178
179
180
181
get_nomenclature_one_level <- function(identifier,
                                       filters,
                                       level_number = 1,
                                       language = "en",
                                       annotations = FALSE) {
  parameters <- glue::glue("language={language}&level={level_number}&annotations={tolower(annotations)}&{stringify(filters)}")
182
183
184
  print(parameters)
  api <- api_class(
    api_type = "nomenclature_one_level",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
185
    id = identifier,
186
    parameters = parameters,
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
187
    export_format = "CSV"
188
189
190
  )
  api$get_response()
}
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
191
192


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#' 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
#'
#' @examples
#' get_nomenclature_multiple_levels(identifier='HCL_CH_ISCO_19_PROF', filters=my_filters, level_from=2, level_to=5)
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
209
210
211
212
213
214
215
get_nomenclature_multiple_levels <- function(identifier,
                                             filters,
                                             level_from = 1,
                                             level_to = 2,
                                             language = "en",
                                             annotations = FALSE) {
  parameters <- glue::glue("language={language}&levelFrom={level_from}&levelTo={level_to}&annotations={tolower(annotations)}&{stringify(filters)}")
216
217
  api <- api_class(
    api_type = "nomenclature_multiple_levels",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
218
    id = identifier,
219
    parameters = parameters,
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
220
    export_format = "CSV"
221
222
223
224
225
226
227
228
  )
  api$get_response()
  ###############################################################
  # TODO: post processing in R
  ###############################################################
}


229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#' Search query within a nomenclature
#'
#' @param identifier nomenclature's identifier
#' @param query the search query
#' @param page_number the number of the result page to return
#' @param page_size the size of each page result
#' @param filters additionnal filters
#' @param language  the language of the response data
#' Available are 'fr', 'de', 'it', 'en'.
#'
#' @return the query result
#' @export
#'
#' @examples
#' query_nomenclature(identifier='HCL_CH_ISCO_19_PROF', query='SELECT *', page_number=5, page_size=2, filters=my_filters)
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
244
245
246
247
248
249
250
query_nomenclature <- function(identifier,
                               query,
                               page_number,
                               page_size,
                               filters,
                               language = "en") {
  parameters <- glue::glue("query={URLencode(query)}&page={page_number}&pageSize={page_size}&language={language}&{stringify(filters)}")
251
252
  api <- api_class(
    api_type = "nomenclature_search",
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
253
    id = identifier,
254
255
256
257
258
259
260
261
262
263
    parameters = parameters
  )
  api$get_response()
}



###############################################################
########################## dcat APIs ##########################
###############################################################
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
264

Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
265

266
267
268
269
270
271
272
#' List all agents
#'
#' @return all agents
#' @export
#'
#' @examples
#' dcat_list_all_agents()
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
273
dcat_list_all_agents <- function() {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
274
275
276
277
278
279
280
281
  api <- api_class(
    api_type = "agents_list",
    root_url = DCAT_URL
  )
  api$get_response()
}


282
283
284
285
286
287
288
289
290
#' Get the agent with the corresponding agent id
#'
#' @param agent_id agent's id
#'
#' @return agent with this id
#' @export
#'
#' @examples
#' dcat_get_agent_from_id(agent_id='xx')
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
291
dcat_get_agent_from_id <- function(agent_id) {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
292
293
294
295
296
297
298
299
300
  api <- api_class(
    api_type = "agent_id",
    root_url = DCAT_URL,
    id = agent_id
  )
  api$get_response()
}


301
302
303
304
305
306
307
#' List all datasets
#'
#' @return all datasets
#' @export
#'
#' @examples
#' dcat_list_all_datasets()
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
308
dcat_list_all_datasets <- function() {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
309
310
311
312
313
314
315
316
  api <- api_class(
    api_type = "dataset_list",
    root_url = DCAT_URL
  )
  api$get_response()
}


317
318
319
320
321
322
323
324
325
#' Get all distributions for the dataset with the corresponding dataset id
#'
#' @param dataset_id dataset's id
#'
#' @return distributions for the dataset with dataset's id
#' @export
#'
#' @examples
#' dcat_get_distributions_from_dataset_id(dataset_id='xx')
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
326
dcat_get_distributions_from_dataset_id <- function(dataset_id) {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
327
328
329
330
331
332
333
334
335
  api <- api_class(
    api_type = "dataset_id_distributions",
    root_url = DCAT_URL,
    id = dataset_id
  )
  api$get_response()
}


336
337
338
339
340
341
342
343
344
#' Get the dataset with the corresponding id
#'
#' @param dataset_id dataset's id
#'
#' @return the dataset
#' @export
#'
#' @examples
#' dcat_get_dataset_from_id(dataset_id='xx')
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
345
dcat_get_dataset_from_id <- function(dataset_id) {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
346
347
348
349
350
351
352
353
354
  api <- api_class(
    api_type = "dataset_id",
    root_url = DCAT_URL,
    id = dataset_id
  )
  api$get_response()
}


355
356
357
358
359
360
361
362
363
#' Get the dataset with the corresponding identifier
#'
#' @param identifier dataset's identifier
#'
#' @return the dataset
#' @export
#'
#' @examples
#' dcat_get_dataset_from_identifier(identifier='xx')
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
364
dcat_get_dataset_from_identifier <- function(identifier) {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
365
366
367
368
369
370
371
372
373
  api <- api_class(
    api_type = "dataset_identifier",
    root_url = DCAT_URL,
    id = identifier
  )
  api$get_response()
}


374
375
376
377
378
379
380
381
382
383
#' Get all distributions for the dataset with the corresponding identifier.
#'
#' @param identifier dataset's identifier
#'
#' @return all distributions for the dataset with 
#' the corresponding dataset identifier
#' @export
#'
#' @examples
#' dcat_get_distributions_from_dataset_identifier(identifier='xx')
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
384
dcat_get_distributions_from_dataset_identifier <- function(identifier) {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
385
386
387
388
389
390
391
392
393
  api <- api_class(
    api_type = "dataset_identifier_distributions",
    root_url = DCAT_URL,
    id = identifier
  )
  api$get_response()
}


394
395
396
397
398
399
400
#' List all distributions
#'
#' @return all distributions
#' @export
#'
#' @examples
#' dcat_list_all_distributions()
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
401
dcat_list_all_distributions <- function() {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
402
403
404
405
406
407
408
409
  api <- api_class(
    api_type = "distributions_list",
    root_url = DCAT_URL
  )
  api$get_response()
}


410
411
412
413
414
415
416
417
418
#' Get the distribution with the corresponding id
#'
#' @param distribution_id distribution's id
#'
#' @return the distribution
#' @export
#'
#' @examples
#' dcat_get_distribution_from_id(distribution_id='xx')
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
419
dcat_get_distribution_from_id <- function(distribution_id) {
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
420
421
422
423
424
425
426
  api <- api_class(
    api_type = "distribution_id",
    root_url = DCAT_URL,
    id = distribution_id
  )
  api$get_response()
}