api_class.R 2.8 KB
Newer Older
1
2
3
4
library(hash)
library(methods)
library(glue)

Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
5
api_class <- setRefClass(
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  "Api", 
  
  fields = list(
    api_type = "character", 
    export_format = "character", 
    root_url = "character",
    parameters = "character",
    id = "character",
    language = "character",
    version_format="numeric",
    path = "character",
    api_url = "character"
  ),
  
  methods = list(
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    initialize = function(
      ..., 
      export_format = "JSON", 
      root_url = BASE_URL, 
      parameters = "", 
      id = "", 
      language = "en", 
      version_format=2.1, 
      path = "."
    ){
      callSuper(
        ..., 
        export_format = export_format, 
        root_url = root_url, 
        parameters = parameters, 
        id = id, 
        language = language, 
        version_format=version_format, 
        path = path
      )
      get_url()
    },
    
    get_response = function(){
45
      request_function <- REQUEST_FUNCTION_MAPPING[[export_format]]
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
46
47
48
49
50
51
52
53
      print(request_function)
      if(parameters==""){
        url = glue("{root_url}/api/{api_url}")
      } else {
        url = glue("{root_url}/api/{api_url}?{parameters}")
      }
      print(url)
      request_function(url)
54
    }, 
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
55
    
56
57
58
59
60
61
62
63
64
    get_url = function() {
      url_mapping <- hash(
        # i14y
        'codelist'=
          glue('CodeLists/{id}/exports/{export_format}/{version_format}'),
        'content_configuration' =
          glue('ContentConfigurations'),
        'content_configuration_identifier' =
          glue('ContentConfigurations/{id}'),
Pauline Maury Laribière's avatar
Pauline Maury Laribière committed
65
66
        'dcat_data_structure' =
          glue('DataStructures/{id}/{language}'),
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
        'dcat_dataset_description' =
          glue('Datasets/{id}/{language}/description'),
        'dcat_dataset_information' =
          glue('Datasets/{id}/{language}/distributions'),
        'nomenclature_path_nodes' =
          glue('Nomenclatures/Childnodes/{id}/{language}/{path}'),
        'nomenclature_one_level' =
          glue('Nomenclatures/{id}/levelexport/CSV'),
        'nomenclature_multiple_levels' =
          glue('Nomenclatures/{id}/multiplelevels/CSV'),
        'nomenclature_search' =
          glue('Nomenclatures/{id}/search'),
        
        # dcat
        'agents_list' =
          glue('Agent'),
        'agent_id' =
          glue('Agent/{id}'),
        'dataset_list' =
          glue('Dataset'),
        'dataset_id_distributions' =
          glue('Dataset/{id}/distributions'),
        'dataset_id' =
          glue('Dataset/{id}'),
        'dataset_identifier' =
          glue('Datataset/identifier/{id}'),
        'dataset_identifier_distributions' =
          glue('Datataset/identifier/{id}/distributions'),
        'distributions_list' =
          glue('Distribution'),
        'distribution_id' = 
          glue('Distribution/{id}')
      )
      api_url <<- url_mapping[[api_type]]
    }
  )
)