forked from resource-watch/resource-watch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.js
337 lines (304 loc) · 11.3 KB
/
dataset.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import WRISerializer from 'wri-json-api-serializer';
// utils
import { WRIAPI } from 'utils/axios';
import { logger } from 'utils/logs';
// API docs: https://resource-watch.github.io/doc-api/index-rw.html#dataset
/**
* Fetchs datasets according to params.
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#getting-all-datasets|here}
* @param {Object} params Request paremeters.
* @param {Object} headers Request headers.
* @param {boolean} _meta Boolean flag indicating whether the meta object should
* @returns {Array} Array of serialized datasets.
* be included in the response or not.
*/
export const fetchDatasets = (params = {}, headers = {}, _meta = false) => {
logger.info('Fetch datasets');
return WRIAPI.get('/dataset', {
headers: {
...WRIAPI.defaults.headers,
// TO-DO: forces the API to not cache, this should be removed at some point
'Upgrade-Insecure-Requests': 1,
...headers
},
params: {
env: process.env.API_ENV,
application: process.env.APPLICATIONS,
...params
},
transformResponse: [].concat(
WRIAPI.defaults.transformResponse,
(({ data, meta }) => ({ datasets: data, meta }))
)
})
.then((response) => {
const { status, statusText, data } = response;
const { datasets, meta } = data;
if (status >= 300) {
logger.error('Error fetching datasets:', `${status}: ${statusText}`);
throw new Error(statusText);
}
if (_meta) {
return {
datasets: WRISerializer({ data: datasets }),
meta
};
}
return WRISerializer({ data: datasets });
})
.catch((response) => {
const { status, statusText } = response;
logger.error(`Error fetching datasets: ${status}: ${statusText}`);
throw new Error(`Error fetching datasets: ${status}: ${statusText}`);
});
};
/**
* Fetches a dataset by id.
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#how-to-get-a-dataset-by-id|here}
* @param {String} id - dataset id.
* @param {Object} params - params sent to the API.
* @returns {Object} serialized specified dataset.
*/
export const fetchDataset = (id, params = {}) => {
if (!id) throw Error('dataset id is mandatory to perform this fetching.');
logger.info(`Fetch dataset: ${id}`);
return WRIAPI.get(`/dataset/${id}`, {
headers: {
...WRIAPI.defaults.headers,
// TO-DO: forces the API to not cache, this should be removed at some point
'Upgrade-Insecure-Requests': 1
},
params: { ...params }
})
.then((response) => {
const { status, statusText, data } = response;
if (status >= 300) {
if (status === 404) {
logger.debug(`Dataset '${id}' not found, ${status}: ${statusText}`);
} else {
logger.error(`Error fetching dataset: ${id}: ${status}: ${statusText}`);
}
throw new Error(statusText);
}
return WRISerializer(data);
})
.catch(({ response }) => {
const { status, statusText } = response;
logger.error(`Error fetching dataset ${id}: ${status}: ${statusText}`);
throw new Error(`Error fetching dataset ${id}: ${status}: ${statusText}`);
});
};
/**
* Get dataset tags.
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#getting-vocabularies-associated-to-a-resource|here}
* @param {String} datasetId - dataset id.
* @param {Object} params - params sent to the API.
* @returns {Object}
*/
export const fetchDatasetTags = (datasetId, params = {}) => {
logger.info(`Fetch dataset tags: ${datasetId}`);
return WRIAPI.get(`dataset/${datasetId}/vocabulary`,
{
headers: { 'Upgrade-Insecure-Requests': 1 },
params: { ...params }
})
.then(response => WRISerializer(response.data))
.catch((response) => {
const { status, statusText } = response;
logger.error(`Error fetching dataset tags ${datasetId}: ${status}: ${statusText}`);
throw new Error(`Error fetching dataset tags ${datasetId}: ${status}: ${statusText}`);
});
};
/**
* Deletes a specified dataset.
* This fetch needs authentication.
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#deleting-a-dataset|here}
* @param {String} id - dataset ID to be deleted.
* @param {String} token - user's token.
* @returns {Object} fetch response.
*/
export const deleteDataset = (id, token) => {
logger.info(`Delete dataset: ${id}`);
return WRIAPI.delete(`/dataset/${id}`, {
headers: {
...WRIAPI.defaults.headers,
Authorization: token
}
})
.then((response) => {
const { status, statusText } = response;
if (status >= 300) {
if (status === 404) {
logger.debug(`Dataset '${id}' not found, ${status}: ${statusText}`);
} else {
logger.error(`Error deleting dataset: ${id}: ${status}: ${statusText}`);
}
throw new Error(statusText);
}
return response;
})
.catch(({ response }) => {
const { status, statusText } = response;
logger.error(`Error deleting dataset ${id}: ${status}: ${statusText}`);
throw new Error(`Error deleting dataset ${id}: ${status}: ${statusText}`);
});
};
/**
* Create a Dataset.
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#creating-a-dataset|here}
* @param {String} token - user's token.
* @param {Object} params - params sent to the API.
* @param {Object} headers - headers sent to the API.
* @returns {Object}
*/
export const createDataset = (token, params = {}, headers) => {
logger.info('Create dataset');
return WRIAPI.post('dataset',
params,
{ headers: { Authorization: token, ...headers } })
.then(response => WRISerializer(response.data))
.catch(({ response }) => {
const { status, statusText } = response;
logger.error(`Error creating dataset ${status}: ${statusText}`);
throw new Error(`Error creating dataset ${status}: ${statusText}`);
});
};
/**
* Update a Dataset.
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#updating-a-dataset|here}
* @param {String} id - dataset id.
* @param {String} token - user's token.
* @param {Object} params - params sent to the API.
* @returns {Object}
*/
export const updateDataset = (id, token, params = {}) => {
logger.info(`Update dataset: ${id}`);
return WRIAPI.patch(`dataset/${id}`, params, { headers: { Authorization: token } })
.then(response => WRISerializer(response.data))
.catch(({ response }) => {
const { status, statusText } = response;
logger.error(`Error updating dataset ${id}: ${status}: ${statusText}`);
throw new Error(`Error updating dataset ${id}: ${status}: ${statusText}`);
});
};
/**
* Update dataset tags.
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#updating-an-existing-vocabulary-resource-relationship|here}
* @param {String} datasetId - dataset id.
* @param {Object[]} tags - user's token.
* @param {String} token - user's token.
* @param {boolean} usePatch - user's token.
* @returns {Object}
*/
export const updateDatasetTags = (datasetId, tags, token, usePatch = false) => {
logger.info(`Update dataset tags: ${datasetId}`);
if (usePatch) {
return WRIAPI.patch(`dataset/${datasetId}/vocabulary/knowledge_graph`,
{
tags,
application: process.env.APPLICATIONS,
env: process.env.API_ENV
},
{ headers: { Authorization: token } })
.then(response => WRISerializer(response.data))
.catch((response) => {
const { status, statusText } = response;
logger.error(`Error updating dataset tags ${datasetId}: ${status}: ${statusText}`);
throw new Error(`Error updating dataset tags ${datasetId}: ${status}: ${statusText}`);
});
}
if (tags.length > 0) {
return WRIAPI.post(`dataset/${datasetId}/vocabulary`,
{
knowledge_graph: {
tags,
application: process.env.APPLICATIONS,
env: process.env.API_ENV
}
},
{ headers: { Authorization: token } })
.then(response => WRISerializer(response.data))
.catch((response) => {
const { status, statusText } = response;
logger.error(`Error updating dataset tags ${datasetId}: ${status}: ${statusText}`);
throw new Error(`Error updating dataset tags ${datasetId}: ${status}: ${statusText}`);
});
}
return WRIAPI.delete(`dataset/${datasetId}/vocabulary/knowledge_graph`,
{
headers: { Authorization: token },
params: {
application: process.env.APPLICATIONS,
env: process.env.API_ENV
}
})
.then(response => WRISerializer(response.data))
.catch((response) => {
const { status, statusText } = response;
logger.error(`Error updating dataset tags ${datasetId}: ${status}: ${statusText}`);
throw new Error(`Error updating dataset tags ${datasetId}: ${status}: ${statusText}`);
});
};
/**
* Creates a metadata object in the specified dataset.
* This methods requires authentication.
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#creating-a-metadata-object|here}
* @param {String} datasetId - dataset ID where the metadata will be attached
* @param {Object} params - metadata object
* @param {String} token - user's token.
* @returns {Object} serialized metadata object.
*/
export const createMetadata = (datasetId, params = {}, token, headers = {}) => {
logger.info(`Create metadata for dataset ${datasetId}`);
return WRIAPI.post(`dataset/${datasetId}/metadata`,
params,
{
headers: {
Authorization: token,
...headers
}
})
.then(({ data }) => WRISerializer(data))
.catch(({ response }) => {
const { status, statusText } = response;
logger.error(`Error creating metadata ${status}: ${statusText}`);
throw new Error(`Error creating metadata ${status}: ${statusText}`);
});
};
/**
* Updates a metadata object in the specified dataset.
* This methods requires authentication.
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#updating-a-metadata|here}
* @param {String} datasetId - dataset ID where the metadata will be attached
* @param {Object} params - metadata object
* @param {String} token - user's token.
* @returns {Object} serialized metadata object.
*/
export const updateMetadata = (datasetId, params = {}, token, headers = {}) => {
logger.info(`Update metadata for dataset ${datasetId}`);
return WRIAPI.patch(`dataset/${datasetId}/metadata`,
params,
{
headers: {
Authorization: token,
...headers
}
})
.then(({ data }) => WRISerializer(data))
.catch(({ response }) => {
const { status, statusText } = response;
logger.error(`Error updating metadata for dataset: ${datasetId}. ${status}: ${statusText}`);
throw new Error(`Error updating metadata for dataset: ${datasetId}. ${status}: ${statusText}`);
});
};
export default {
fetchDatasets,
fetchDataset,
fetchDatasetTags,
updateDatasetTags,
deleteDataset,
createDataset,
updateDataset,
createMetadata,
updateMetadata
};