forked from resource-watch/resource-watch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.js
166 lines (158 loc) · 5.65 KB
/
graph.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
import WRISerializer from 'wri-json-api-serializer';
// utils
import { WRIAPI } from 'utils/axios';
import { logger } from 'utils/logs';
/**
* Get all tags.
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#list-concepts|here}
* @param {Object} params Request parameters to API.
*/
export const fetchAllTags = (params = {}) => {
logger.info('Fetch all tags');
return WRIAPI.get('graph/query/list-concepts',
{
headers: { 'Upgrade-Insecure-Requests': 1 },
params: {
env: process.env.API_ENV,
application: process.env.APPLICATIONS,
...params
}
})
.then(response => response.data.data)
.catch((response) => {
const { status, statusText } = response;
logger.error(`Error fetching all tags: ${status}: ${statusText}`);
throw new Error(`Error fetching all tags: ${status}: ${statusText}`);
});
};
/**
* Get inferred tags.
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#get-inferred-concepts|here}
* @param {Object} params Request parameters to API.
*/
export const fetchInferredTags = (params = {}) => {
logger.info('Fetch inferred tags');
return WRIAPI.get('graph/query/concepts-inferred',
{
headers: { 'Upgrade-Insecure-Requests': 1 },
params: {
env: process.env.API_ENV,
application: process.env.APPLICATIONS,
...params
}
})
.then(response => response.data.data)
.catch((response) => {
const { status, statusText } = response;
logger.error(`Error fetching inferred tags ${status}: ${statusText}`);
throw new Error(`Error inferred tags ${status}: ${statusText}`);
});
};
/**
* Send a request to count a view to the dataset.
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#count-dataset-view|here}
* @param {String} datasetId Dataset ID
* @param {String} token User token
* @param {Object} params Request parameters to API.
*/
export const countDatasetView = (datasetId, token, params = {}) => {
logger.info('Count dataset view');
return WRIAPI.post(`graph/dataset/${datasetId}/visited`,
{},
{
headers: { Authorization: token },
params: {
env: process.env.API_ENV,
application: process.env.APPLICATIONS,
...params
}
})
.catch((response) => {
const { status, statusText } = response;
logger.error(`Error in count dataset view ${datasetId}: ${status}: ${statusText}`);
throw new Error(`Error in count dataset view ${datasetId}: ${status}: ${statusText}`);
});
};
/**
* Get the list of most viewed datasets.
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#most-viewed-datasets|here}
* @param {Object} params Request parameters to API.
* @returns {Promise<string[]>} List of sorted ids
*/
export const fetchMostViewedDatasets = (params = {}) => {
logger.info('Fetch most viewed datasets');
return WRIAPI.get('graph/query/most-viewed',
{
params: {
env: process.env.API_ENV,
application: process.env.APPLICATIONS,
...params
},
headers: { 'Upgrade-Insecure-Requests': 1 }
})
.then(response => WRISerializer(response.data))
.catch((response) => {
const { status, statusText } = response;
logger.error(`Error fetching most viewed datasets: ${status}: ${statusText}`);
throw new Error(`Error fetching most viewed datasets: ${status}: ${statusText}`);
});
};
/**
* Get the list of most favourited datasets.
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#most-liked-datasets|here}
* @param {Object} params Request parameters to API.
*/
export const fetchMostFavoritedDatasets = (params = {}) => {
logger.info('Fetch most favorited datasets');
return WRIAPI.get('graph/query/most-liked-datasets',
{
params: {
env: process.env.API_ENV,
application: process.env.APPLICATIONS,
...params
},
headers: { 'Upgrade-Insecure-Requests': 1 }
})
.then(response => WRISerializer(response.data))
.catch((response) => {
const { status, statusText } = response;
logger.error(`Error fetching most favorited datasets: ${status}: ${statusText}`);
throw new Error(`Error fetching most favorited datasets: ${status}: ${statusText}`);
});
};
/**
* Fetch similar datasets.
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#similar-datasets-including-ancestors|here}
* @param {Object} params Request parameters to API.
* @param {boolean} withAncestors Flag indicating whether tags' ancestors
* should be considered or not
*/
export const fetchSimilarDatasets = (params = {}, withAncestors = true) => {
logger.info('Fetch similar datasets');
const endpoint = withAncestors ? 'similar-dataset-including-descendent' : 'similar-dataset';
return WRIAPI.get(
`graph/query/${endpoint}`,
{
params: {
env: process.env.API_ENV,
application: process.env.APPLICATIONS,
...params
},
headers: { 'Upgrade-Insecure-Requests': 1 }
}
)
.then(response => response.data.data)
.catch((response) => {
const { status, statusText } = response;
logger.error(`Error fetching similart datasets ${status}: ${statusText}`);
throw new Error(`Error fetching similart datasets ${status}: ${statusText}`);
});
};
export default {
fetchMostViewedDatasets,
fetchMostFavoritedDatasets,
fetchSimilarDatasets,
countDatasetView,
fetchInferredTags,
fetchAllTags
};